操作excel的依赖的poi包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
导入文件的控制层:
/**
* @Description: 导入excel数据
* @author lcy
* @param file excel文件
* @return 执行结果
*/
@PostMapping(value = "/excel/import")
public String importExcel(@RequestParam("file") MultipartFile file) {
return service.importExcel(file);
}
这里使用了MultipartFile接口来接收文件。该相关会在另一篇博客中记录。
读取Excel的服务层:
public String importExcel(MultipartFile file) {
// 获取文件名
String originalFilename = file.getOriginalFilename();
Workbook workbook = null;
try {
if (originalFilename.endsWith(".xls")) {
workbook = new HSSFWorkbook(file.getInputStream());
} else if (originalFilename.endsWith(".xlsx")) {
workbook = new XSSFWorkbook(file.getInputStream());
}
} catch (Exception e) {
log.info("格式错误,获取的文件名为:{}", originalFilename);
e.printStackTrace();
return "格式错误";
}
try {
// 获取所有的工作表的的数量
int numOfSheet = workbook.getNumberOfSheets();
// 遍历这个这些表
for (int i = 0; i < numOfSheet; i++) {
// 获取一个sheet也就是一个工作簿
Sheet sheet = workbook.getSheetAt(i);
int lastRowNum = sheet.getLastRowNum();
// 从第一行开始第一行是标题
for (int j = 1; j <= lastRowNum; j++) {
Row row = sheet.getRow(j);
if (row == null) {
break;
}
/** 接下来就可以在这部分操作每列的字段 **/
Cell call0 = row.getCell(0); // 获取该行第int列
/** Call有很多获取内容的方法,根据实际情况进行选择 **/
}
}
} catch (Exception e) {
log.info("解析错误,当前已解析{}条数据", secuweakList.size());
e.printStackTrace();
return "解析错误";
}
/** 上面是操作excel获取内容,放入对应的VO类中,存入数据库或进行其他操作即可 **/
}
主要是要通过文件名称的后缀来区分是2003版以前的Excel还是2007版以后的,不同的版本使用的对象工具去操作。
2003版以前的是拓展名是.xls,使用HSSFWorkbook
2007版以后的拓展名是.xlsx,使用XSSFWorkbook
导出文件控的制层:
/**
* @Title: exportExcel
* @Description: 导出excel数据
* @author lcy
* @param ...
*/
@GetMapping(value = "/excel/export")
public void exportExcel(HttpServletRequest request, HttpServletResponse response,
...) {
/** 这里可以获取要导出的数据集合 **/
try {
HSSFWorkbook hssfwb = homePageService.createExcel(List);// 创建Excel对象
if (hssfwb != null) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/wnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String("安全加固数据".getBytes("gb2312"), "ISO8859-1") + ".xls");
ServletOutputStream outputStream = response.getOutputStream();
hssfwb.write(outputStream);
outputStream.flush();
outputStream.close();
hssfwb = null;
}
} catch (Exception e) {
log.info("导出安全加固数据的excel异常");
e.printStackTrace();
}
}
创建Excel的服务层:
public HSSFWorkbook createSecuweakExcel(List<ObjectVO> list) {
// 创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("安全加固");
// 生成标题行
HSSFRow row = sheet.createRow(0);
HSSFCell cell0 = row.createCell(0);
HSSFCell cell1 = row.createCell(1);
...
cell0.setCellValue("科室");
cell1.setCellValue("系统名称");
cell2.setCellValue("漏洞名称");
...
// 循环生成数据行
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);// 生成数据行
cell0 = row.createCell(0);
cell1 = row.createCell(1);
cell2 = row.createCell(2);
...
ObjectVO vo= list.get(i); // 自定义的VO对象
cell0.setCellValue(secuweak.getOffice());
cell1.setCellValue(secuweak.getSystemName());
cell2.setCellValue(secuweak.getBugName());
...
}
return workbook;
}