strHeadTitle 表头 随便填,开启多线程读取sheet页
String[] strHeadTitle = {
getText("mat.export.excel.length"),
getText("mat.export.excel.type"),
getText("mat.export.excel.Version"),
getText("mat.export.excel.serviceType"),
getText("mat.export.excel.serviceID"),
getText("mat.export.excel.serialNumber"),
//getText("mat.export.excel.mateId"),//mateId
getText("mat.export.excel.No")
};
String fileName = DateTimeUtil.getCurrentTimeMillis() + ".xlsx";
List<List<MatDtl>> data= matManager.getListIncludeList(matDtlList,80000);
response.setHeader("ContentType","application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
response.setCharacterEncoding("UTF-8");
OutputStream out = response.getOutputStream();
XSSFWorkbook workbook = new XSSFWorkbook();
List<Thread> workers = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
final int x=i;
Thread worker = new Thread(new Runnable() {
@Override
public void run() {
try {
matDtlManager.exportExcel(workbook, x, "sheet"+(x+1), strHeadTitle, data.get(x), out);
} catch (Exception e) {
e.printStackTrace();
}
}
});
worker.start();
workers.add(worker);
}
for(int i = 0; i < data.size(); i++) {
workers.get(i).join();
}
workbook.write(out);
out.flush();
out.close();
export 工具方法
public void exportExcel(XSSFWorkbook workbook, int sheetNum,
String sheetTitle, String[] headers, List<MatDtl> result,
OutputStream out) throws Exception {
XSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(sheetNum, sheetTitle);
sheet.setDefaultColumnWidth((short) 20);
XSSFCellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 12);
style.setFont(font);
style.setWrapText(true);
style.setAlignment(HorizontalAlignment.CENTER);
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
XSSFCell cell = row.createCell((short) i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text.toString());
}
/**设置单元格格式为文本格式*/
XSSFCellStyle textStyle = workbook.createCellStyle();
XSSFDataFormat format = workbook.createDataFormat();
textStyle.setDataFormat(format.getFormat("@"));
textStyle.setAlignment(HorizontalAlignment.CENTER);
if (result != null) {
int index = 1;
for (MatDtl matDtl : result) {
row = sheet.createRow(index);
XSSFCell cell = row.createCell(0);
cell.setCellStyle(textStyle);
cell.setCellValue("16");//now: default "16" 2019-3-12 15:04:39
cell = row.createCell(1);
cell.setCellStyle(textStyle);
cell.setCellValue("11");//now: default "11" 2019-3-12 15:04:39
cell = row.createCell(2);
cell.setCellStyle(textStyle);
cell.setCellValue("01");//now: default "01" 2019-3-12 15:04:39
cell = row.createCell(3);
cell.setCellStyle(textStyle);
cell.setCellValue(StringUtils.defaultString(matDtl.getMat().getSvcTyp()));
cell = row.createCell(4);
cell.setCellStyle(textStyle);
cell.setCellValue(StringUtils.defaultString(Integer.toHexString(matDtl.getPrjSvcId().intValue()).toUpperCase()));
cell = row.createCell(5);
cell.setCellStyle(textStyle);
cell.setCellValue(StringUtils.defaultString(matDtl.getSerNum()));
cell = row.createCell(6);
cell.setCellStyle(textStyle);
cell.setCellValue(StringUtils.defaultString(String.valueOf(row.getRowNum())));
index++;
}
}
}