1、导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2、编写导出execl接口
@GetMapping("/export")
public void export(@ModelAttribute DevicePageParam pageParam ,HttpServletResponse response) {
List<Dispatch> deviceList = deviceFeignClient.getDeviceList(pageParam);
String[] heards = {"机构", "部门", "设备名称", "标签ID", "SIP", "MAC", "人员名称"};
String fileName = StrUtil.concat(false, "设备信息", RandomUtil.randomNumbers(10), ".xls");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("设备信息");
sheet.setColumnWidth(0, 20 * 256);
sheet.setColumnWidth(1, 20 * 256);
sheet.setColumnWidth(2, 40 * 256);
sheet.setColumnWidth(3, 20 * 256);
sheet.setColumnWidth(4, 15 * 256);
sheet.setColumnWidth(5, 40 * 256);
sheet.setColumnWidth(6, 15 * 256);
CellStyle titleCellStyle = createWorkBookCellStyle(workbook, IndexedColors.GREY_25_PERCENT.index, true);
Row titleRow = sheet.createRow(0);
for (int i = 0; i < heards.length; i++) {
Cell rowCell = titleRow.createCell(i);
rowCell.setCellStyle(titleCellStyle);
rowCell.setCellValue(heards[i]);
}
CellStyle bobyCellStyle = createWorkBookCellStyle(workbook, IndexedColors.LIGHT_CORNFLOWER_BLUE.index, true);
if (CollectionUtil.isNotEmpty(deviceList)) {
for (int i = 0; i < deviceList.size(); i++) {
Row row = sheet.createRow(i + 1);
Dispatch dispatch = deviceList.get(i);
Cell rowCell0 = row.createCell(0);
rowCell0.setCellValue(dispatch.getOrganizationName());
rowCell0.setCellStyle(bobyCellStyle);
Cell rowCell1 = row.createCell(1);
rowCell1.setCellValue(dispatch.getDepartmentName());
rowCell1.setCellStyle(bobyCellStyle);
Cell rowCell2 = row.createCell(2);
rowCell2.setCellValue(dispatch.getName());
rowCell2.setCellStyle(bobyCellStyle);
Cell rowCell3 = row.createCell(3);
rowCell3.setCellValue(dispatch.getTagId());
rowCell3.setCellStyle(bobyCellStyle);
Cell rowCell4 = row.createCell(4);
rowCell4.setCellValue(dispatch.getSip());
rowCell4.setCellStyle(bobyCellStyle);
Cell rowCell5 = row.createCell(5);
rowCell5.setCellValue(dispatch.getMac());
rowCell5.setCellStyle(bobyCellStyle);
Cell rowCell6 = row.createCell(6);
rowCell6.setCellValue(dispatch.getPersonnelName());
rowCell6.setCellStyle(bobyCellStyle);
}
}
try {
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", StrUtil.concat(false, "attachment;filename=", URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())));
workbook.write(response.getOutputStream());
workbook.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private CellStyle createWorkBookCellStyle(Workbook workbook, short fillForegroundColorIndex, Boolean showBorder) {
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(fillForegroundColorIndex);
if (showBorder) {
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
cellStyle.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
cellStyle.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
cellStyle.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
}
return cellStyle;
}