public File exportExcel() throws IOException {
SXSSFWorkbook wb = new SXSSFWorkbook(-1);
Sheet sheetContent = wb.createSheet("采购清单导入模板");
HeaderInfo[] headers = new HeaderInfo[] {
new HeaderInfo().withTitle("资产名称").withWidth(30),
new HeaderInfo().withTitle("单价").withWidth(20),
new HeaderInfo().withTitle("数量(面积)").withWidth(20),
new HeaderInfo().withTitle("价值").withWidth(20),
new HeaderInfo().withTitle("采购组织形式").withWidth(20),
new HeaderInfo().withTitle("厂商").withWidth(30),
new HeaderInfo().withTitle("品牌").withWidth(30),
new HeaderInfo().withTitle("规格型号").withWidth(30),
new HeaderInfo().withTitle("国标分类").withWidth(30)
};
createHeaders(wb, sheetContent, headers);
UUID fileId = UUID.randomUUID();
File resultFile = TempFile.createTempFile(fileId, "xlsx");
try (OutputStream out = new FileOutputStream(resultFile)) {
wb.write(out);
}
wb.dispose();
return resultFile;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void createHeaders(SXSSFWorkbook wb, Sheet sheet, HeaderInfo[] headers) {
CreationHelper ch = wb.getCreationHelper();
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(30);
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
Font headerFont = wb.createFont();
headerFont.setFontName("黑体");
headerStyle.setFont(headerFont);
headerStyle.setBorderRight(CellStyle.BORDER_THIN);
headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
headerStyle.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headerStyle.setLocked(true);
for (int i = 0; i < headers.length; ++i) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(ch.createRichTextString(headers[i].getTitle()));
cell.setCellStyle(headerStyle);
if (headers[i].getWidth() > 0) {
sheet.setColumnWidth(i, headers[i].getWidth());
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class TempFile {
public static File createTempFile(UUID fileId, String type) throws IOException {
return File.createTempFile(fileId.toString(), "." + type);
}
public static File getTempFile(UUID fileId, String type) {
return new File(IOUtils.getTempDir(), fileId.toString() + "." + type);
}
}