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(value = "/export")
public R export(HttpServletResponse response,@RequestBody BondVO bondVO) throws IOException {
List<Bond> bondList = bondService.list(Wrappers.<Bond>lambdaQuery()
.like(StringUtils.isNotBlank(bondVO.getStockCode()),Bond::getStockCode,bondVO.getStockCode()));
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(byteOut);
OutputStream servletOut = response.getOutputStream();
try {
bondList.stream().collect(Collectors.groupingBy(Bond::getInventoryLevel)).forEach((k,v)->{
XSSFWorkbook workbook = null;
ByteArrayOutputStream bos = null;
try {
workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(k);
sheet.setDefaultColumnWidth(15);
XSSFCellStyle headerStyle = createHeadStyle(workbook);
XSSFCellStyle bodyStyle = createBodyStyle(workbook);
int rowNum = 0;
boolean titleFlag = false;
for (Bond bond : 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 (bond.getStockName() != null){
cell.setCellValue(bond.getStockName());
}
}else if (i == 1){
if (bond.getTradingMarketCode() != null){
cell.setCellValue(bond.getTradingMarketCode());
}
}
}
}
zip.putNextEntry(new ZipEntry("文件夹名称/"+ k +".xlsx"));
bos = new ByteArrayOutputStream();
workbook.write(bos);
bos.writeTo(zip);
zip.flush();
zip.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(workbook);
});
IOUtils.closeQuietly(zip);
byte[] data = byteOut.toByteArray();
String fileName = "压缩包名称";
String codedFileName = URLEncoder.encode(fileName, "UTF-8");
response.setCharacterEncoding("utf-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".zip");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, servletOut);
} catch (Exception e) {
log.error("导出失败", e);
return R.failed("导出失败");
} finally {
IOUtils.closeQuietly(byteOut);
IOUtils.closeQuietly(servletOut);
}
return R.ok(null,"导出成功");
}