private CellStyle setWkCellstyle(SXSSFWorkbook workbook, String useArea) {
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
if ("titleAndColone".equals(useArea)) {
Font font = workbook.createFont();
font.setBold(true);
cellStyle.setFont(font);
}
return cellStyle;
}
private R exportSxssfExecl(List<Map> listMap, String sheetName) throws IOException {
OutputStream outputStream = null;
SXSSFWorkbook workbook = new SXSSFWorkbook(500);
SXSSFSheet sheet = workbook.createSheet(sheetName);
try {
SXSSFRow rowTitle = sheet.createRow(0);
SXSSFCell cellTitle = rowTitle.createCell(0);
cellTitle.setCellValue("数据统计表");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));
cellTitle.setCellType(CellType.STRING);
CellStyle csTitleAndOnecol = setWkCellstyle(workbook, "titleAndColone");
cellTitle.setCellStyle(csTitleAndOnecol);
sheet.setDefaultColumnWidth(20);
CellStyle csCellContent = setWkCellstyle(workbook, "cellContent");
......
String filename = encodingFilename(sheetName);
outputStream = new FileOutputStream(getAbsoluteFile(filename));
workbook.write(outputStream);
return R.ok(filename);
} catch (Exception e) {
log.error("导出Excel异常{}", e.getMessage());
throw new BusinessException("导出Excel失败,请联系网站管理员!");
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
private String encodingFilename(String filename) {
filename = UUID.randomUUID().toString() + "_" + filename + ".xlsx";
return filename;
}