编辑表格模板保存到某个文件夹下
创建表格填充方法
public static Workbook excelExport(String path,List<List> mapList) throws IOException {
//获取模板文件
InputStream is = new FileInputStream(path);
Workbook workbook = new XSSFWorkbook(is);
for (int n = 0; n < mapList.size(); n++) {
//获取Excel表格sheet
Sheet sheet = workbook.getSheetAt(n);
//获取表头
Row row = sheet.getRow(0);
SimpleDateFormat fm = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
List<Map<String, Object>> list = mapList.get(n);
//判断模板是否正确,如果没有表头不填充数据
if (row != null & mapList.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Map m = list.get(i);
//创建一行
sheet.createRow(i + 1);
row = sheet.getRow(i + 1);
row.createCell(0).setCellValue((String) m.get("taskId"));
row.createCell(1).setCellValue((String) m.get("type"));
row.createCell(2).setCellValue((String) m.get("description"));
row.createCell(3).setCellValue((String) m.get("address"));
row.createCell(4).setCellValue((String) fm.format((m.get("discovertime"))));
row.createCell(5).setCellValue((String) m.get("subexecutedeptname"));
row.createCell(6).setCellValue((String) m.get("statusname"));
}
}
}
return workbook;
}
调用excelExport()就可以把数据填充到模板文件中,并返回Workbook,然后生成.xslx文档或者通过浏览器下载就完成了