public static XSSFCellStyle createHeadStyle(XSSFWorkbook workbook) {
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new Color(87, 163, 250)));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
XSSFFont font = workbook.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short) 10);
style.setFont(font);
return style;
}
public static XSSFCellStyle createBodyStyle(XSSFWorkbook workbook) {
XSSFCellStyle style = workbook.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
XSSFFont font = workbook.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short) 9);
style.setFont(font);
return style;
}
@PostMapping("/export")
public R export(HttpServletResponse response,@RequestBody MainBodyVO mainBodyVO){
List<MainBody> mainBodyList = mainBodyService.list(Wrappers.<MainBody>lambdaQuery()
.like(StringUtils.isNotBlank(mainBodyVO.getMainTypes()),MainBody::getMainTypes,mainBodyVO.getMainTypes())
.like(StringUtils.isNotBlank(mainBodyVO.getIssuerName()),MainBody::getIssuerName,mainBodyVO.getIssuerName())
.like(StringUtils.isNotBlank(mainBodyVO.getInventoryLevel()),MainBody::getInventoryLevel,mainBodyVO.getInventoryLevel()));
XSSFWorkbook workbook = new XSSFWorkbook();
mainBodyList.stream().collect(Collectors.groupingBy(MainBody::getMainTypes)).forEach((k, v)->{
XSSFSheet sheet = workbook.createSheet(k);
sheet.setDefaultColumnWidth(15);
XSSFCellStyle headerStyle = createHeadStyle(workbook);
XSSFCellStyle bodyStyle = createBodyStyle(workbook);
int rowNum = 0;
boolean titleFlag = false;
for (MainBody mainBody : v) {
String[] excelHeader = {"列名", "列名"};
if (!titleFlag) {
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < excelHeader.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(headerStyle);
}
}
titleFlag = true;
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < excelHeader.length; i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(bodyStyle);
if (i == 0) {
if(mainBody.getIssuerName() != null){
cell.setCellValue(mainBody.getIssuerName());
}
} else if (i == 1) {
if(mainBody.getInventoryLevel() != null){
cell.setCellValue(mainBody.getInventoryLevel());
}
}
}
}
});
response.setHeader("content-Type", "application/vnd.ms-excel");
try {
String fileName = "表格名称";
String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+codedFileName + ".xlsx");
OutputStream stream = response.getOutputStream();
if (null != workbook && null != stream) {
workbook.write(stream);
workbook.close();
stream.close();
}
} catch (IOException e) {
log.error("导出失败",e.getMessage());
return R.failed("导出失败");
}
return R.ok(null,"导出成功");
}