问题遇到的现象和发生背景
如何遍历list<map>里的数据赋值给单元格
问题相关代码,请勿粘贴截图
List<Map<String, Object>> resultList = Service.selectInfoList(dataParams);
XSSFWorkbook workBook = null;
FileInputStream fis = null;
OutputStream ouputStream = null;
String classPath = this.getClass().getResource("/").getFile().toString();
String srcFilePath = classPath + "/file/模板.xlsx"; //模板
//创建Excel文件的输入流对象
fis = new FileInputStream(srcFilePath);
//根据模板创建excel工作簿
workBook = new XSSFWorkbook(fis);
//获取创建的工作簿第一页
XSSFSheet sheet = workBook.getSheetAt(0);
//给指定的sheet命名
workBook.setSheetName(0, "管理人员");
CellStyle style = workBook.createCellStyle(); //单元格样式设置
style.setBorderRight(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
for (int i = 2; i < resultList.size() + 2; i++) {
XSSFRow row = sheet.createRow(i); //建行
for (int j = 0; j < resultList.get(i).size(); j++) {
XSSFCell cell = row.createCell(j); //建单元格
for (String key : resultList.get(i).keySet()) {
cell.setCellValue(nvl(resultList.get(i).get(key))); //给单元格赋值
}
}
}
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + Encodes.urlEncode("市公司本部四级管理人员.xlsx"));
ouputStream = response.getOutputStream();
workBook.write(ouputStream);
//关闭流
ouputStream.flush();
ouputStream.close();
我的解答思路和尝试过的方法
我想要达到的结果