- 导包
在项目中导入poi的jar包
- 在servlet中调用
//到处数据为xls格式
private static void createExcel(List<Duty> list,HttpServletResponse response) {
// 创建一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建一个工作表
HSSFSheet sheet = workbook.createSheet("考勤信息");
CellRangeAddress region = new CellRangeAddress(0, // first row
0, // last row
0, // first column
2 // last column
);
sheet.addMergedRegion(region);
HSSFRow hssfRow = sheet.createRow(0);
HSSFCell headCell = hssfRow.createCell(0);
headCell.setCellValue("考勤信息");
// 设置单元格格式居中
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headCell.setCellStyle(cellStyle);
// 添加表头行
hssfRow = sheet.createRow(1);
// 添加表头内容
headCell = hssfRow.createCell(0);
headCell.setCellValue("用户名");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(1);
headCell.setCellValue("真实姓名");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(2);
headCell.setCellValue("所属部门");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(3);
headCell.setCellValue("出勤日期");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(4);
headCell.setCellValue("签到时间");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(5);
headCell.setCellValue("签退时间");
headCell.setCellStyle(cellStyle);
// 添加数据内容
for (int i = 0; i < list.size(); i++) {
hssfRow = sheet.createRow((int) i + 2);
Duty duty = list.get(i);
// 创建单元格,并设置值
HSSFCell cell = hssfRow.createCell(0);
cell.setCellValue(duty.getEmp().getEmpId());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(1);
cell.setCellValue(duty.getEmp().getRealName());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(2);
cell.setCellValue(duty.getEmp().getDept().getDeptName());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(3);
cell.setCellValue(duty.getDtDate());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(4);
cell.setCellValue(duty.getSigninTime());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(5);
cell.setCellValue(duty.getSignoutTime());
cell.setCellStyle(cellStyle);
}
// 保存Excel文件
try {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=duty.xls");//附件形式下载,文件名叫duty.xls
//OutputStream outputStream = new FileOutputStream("D:/duty.xls");//保存到本地(服务器端)
OutputStream outputStream = response.getOutputStream(); //写到客户端
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3.读取数据
/**
* 读取Excel
*
* @return 数据集合
*/
private static List<Student> readExcel() {
List<Student> list = new ArrayList<Student>();
HSSFWorkbook workbook = null;
try {
// 读取Excel文件
InputStream inputStream = new FileInputStream("D:/students.xls");
workbook = new HSSFWorkbook(inputStream);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// 循环工作表
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = workbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行
for (int rowNum = 2; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
// 将单元格中的内容存入集合
Student student = new Student();
HSSFCell cell = hssfRow.getCell(0);
if (cell == null) {
continue;
}
student.setName(cell.getStringCellValue());
cell = hssfRow.getCell(1);
if (cell == null) {
continue;
}
student.setAge((int) cell.getNumericCellValue());
cell = hssfRow.getCell(2);
if (cell == null) {
continue;
}
student.setGrade(cell.getStringCellValue());
list.add(student);
}
}
return list;
}
}