前一段時間,提了一個需求就是把好幾萬的人員信息都導出來 ,大家可以想象一下人員信息字段又這麼多,又要驗證肯定是很慢的,用HSSFWorkbook很多數據就卡了,然後從網上找了很久,有一篇文章是用XSSFWorkbook
寫的導出速度很快,然後別我借用了一下,由於距離時間太久了就找不到那篇文章了,不過找到了其中的一篇就是自動創建一個XSSFWorkbook 類型的excel。
地址如下:https://stackoverflow.com/questions/21992071/org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma
希望我的文章對大家有幫助
代碼如下:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = createSheet(wb, "Sheet 1", false);Row sizeRow = sh.createRow(10);
XSSFCellStyle ztStyle = (XSSFCellStyle) wb.createCellStyle();
Font ztFont = wb.createFont();
ztFont.setFontHeightInPoints((short)14); // 將字體大小設置爲18px
ztFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
ztFont.setFontName("宋體");
ztStyle.setFont(ztFont);
ztStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
ztStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
ztStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
ztStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下邊框
ztStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左邊框
ztStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上邊框
ztStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右邊框
Row row0=sh.createRow(0);
Cell cell0=row0.createCell(0);
cell0.setCellValue("序號");
Cell cell1=row0.createCell(1);
cell1.setCellValue("項目部名稱");
Cell cell2=row0.createCell(2);
cell2.setCellValue("姓名");
cell0.setCellStyle(ztStyle);
cell1.setCellStyle(ztStyle);
sh.autoSizeColumn((short)0); //調整第一列寬度
sh.autoSizeColumn((short)1);
sh.autoSizeColumn((short)2);
XSSFCellStyle Style = (XSSFCellStyle) wb.createCellStyle();
Style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下邊框
Style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左邊框
Style.setBorderTop(XSSFCellStyle.BORDER_THIN);//上邊框
Style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右邊框
// 居
// 查詢數據庫中所有的數據
list//查詢數據結果
for(int i = 0; i < list.size(); i++){
Row row = sh.createRow(i+1);/*for(int cellnum = 0; cellnum < 10; cellnum++){
Cell cell = row.createCell(cellnum);
String address = new CellReference(cell).formatAsString();
cell.setCellValue(address);
}*/
Cell cell00 = row.createCell(0);
cell00.setCellValue(i+1);
cell00.setCellStyle(Style);
if (list.get(i).get("officename") != null) {
Cell cell = row.createCell(1);
cell.setCellValue(list.get(i).get("officename").toString());
// 項目部名稱
cell.setCellStyle(Style);
}else{
Cell cell = row.createCell(1);
cell.setCellValue("");
cell.setCellStyle(Style);
}
if (list.get(i).get("name") != null) {
Cell cell = row.createCell(2);
cell.setCellValue(list.get(i).get("name").toString());
cell.setCellStyle(Style);
// 姓名
}else{
Cell cell = row.createCell(2);
cell.setCellValue("");
cell.setCellStyle(Style);
}
}
/*FileOutputStream out = new FileOutputStream("d:/aatemp.xls");
wb.write(out);
out.close();*/
OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=employeeinfo.xlsx");
response.setContentType("application/msexcel");
wb.write(output);
output.close();
private static XSSFSheet createSheet(XSSFWorkbook wb, String prefix, boolean isHidden) {
XSSFSheet sheet = null;
int count = 0;
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
String sName = wb.getSheetName(i);
if (sName.startsWith(prefix))
count++;
}
if (count > 0) {
sheet = wb.createSheet(prefix + count);
} else
sheet = wb.createSheet(prefix);
if (isHidden)
wb.setSheetHidden(wb.getNumberOfSheets() - 1, XSSFWorkbook.SHEET_STATE_VERY_HIDDEN);
return sheet;
}