Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet");// --------------------创建第一行---------------
// 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
Row row4 = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
Cell cell01 = row4.createCell(0);
Cell cell02=row4.createCell(7);
Cell cell03=row4.createCell(12);
Cell cell04=row4.createCell(18);
// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
// 设置单元格内容
cell01.setCellValue("任务状态");
// ----------------------------------------------
// 3. 创建一个行 (第一行)
Row row1 = sheet.createRow(1);
// 创建单元格(1,1)
Cell cell111 = row1.createCell(0);
// 单元格(1,1)写入内容
cell111.setCellValue("任务总数");
// 创建单元格(1,2)
Cell cell112 = row1.createCell(1);
// 单元格(1,2)写入内容
cell112.setCellValue("未开始");
Cell cell13 = row1.createCell(2);
cell13.setCellValue("执行中");
Cell cell14 = row1.createCell(3);
cell14.setCellValue("已完成");
//填写数据
for (int i =0; i < list.size(); i++) {
Map map = list.get(i);
Row row = sheet.createRow(i+2);
Cell cell1 = row.createCell(0);
cell1.setCellValue(map.get( "sumTotal").toString());
Cell cell2 = row.createCell(1);
cell2.setCellValue(map.get("unstart").toString());
Cell cell3 = row.createCell(2);
cell3.setCellValue( map.get("running").toString());
Cell cell4 = row.createCell(3);
cell4.setCellValue( map.get("finished").toString());
}
FileOutputStream fileOutputStream = null;
String fileName = "业务统计.xlsx";
// 刷新公式 (all)
XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
// 文件路径
String filePath = ReliFileUtils.getExcelFilePath(fileName);
// 文件流
fileOutputStream = new FileOutputStream(filePath);
// 写出文件
workbook.write(fileOutputStream);
// 设置单元格内容
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
cell.setCellStyle(cellStyle);