1.導出數據成Excel方法代碼(生成的excel表格中可實現合並行列,按照特定的樣式排列展示數據)
/** * 導出excel * @param title 導出表的標題 * @param fileNames 生成excel文件的文件名 * @param response */ public static void exportExcel(String title, String fileNames, HttpServletResponse response) throws Exception{ String fileName= new String(fileNames.getBytes("UTF-8"),"iso-8859-1"); OutputStream output = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename="+fileName); response.setContentType("application/msexcel"); try { HSSFWorkbook workbook; // 創建工作簿對象 workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(title); // 創建工作表 // 如果這行沒有了,整個公式都不會有自動計算的效果的 sheet.setForceFormulaRecalculation(true); // 設置字體 HSSFFont font = workbook.createFont(); // 設置字體大小 font.setFontHeightInPoints((short) 11); // 設置字體名字 font.setFontName("Courier New"); // 設置樣式; HSSFCellStyle style = workbook.createCellStyle(); // 在樣式用應用設置的字體; style.setFont(font); // 設置自動換行; style.setWrapText(false); //橫向居中 style.setAlignment(HorizontalAlignment.CENTER); //縱向居中 style.setVerticalAlignment( VerticalAlignment.CENTER); // 設置底邊框; style.setBorderBottom(BorderStyle.THIN); // 設置左邊框; style.setBorderLeft(BorderStyle.THIN); // 設置右邊框; style.setBorderRight(BorderStyle.THIN); // 設置頂邊框; style.setBorderTop(BorderStyle.THIN); //設置列的樣式 sheet.setDefaultColumnStyle(0,style); sheet.setDefaultColumnStyle(1,style); sheet.setDefaultColumnStyle(2,style); sheet.setDefaultColumnStyle(3,style); sheet.setDefaultColumnStyle(4,style); sheet.setDefaultColumnStyle(5,style); //合並表格行,合並列數為列名的長度,第一個參數為起始行號,第二個參數為終止行號,第三個參數為起始列好,第四個參數為終止列號 sheet.addMergedRegion(new CellRangeAddress(0, 0, 1,2 )); sheet.addMergedRegion(new CellRangeAddress(0, 0, 3,5 )); sheet.addMergedRegion(new CellRangeAddress(1, 1, 1,2 )); sheet.addMergedRegion(new CellRangeAddress(1, 1, 3,5 )); //創建行,傳入參數0,表明創建的是第一行 HSSFRow row = sheet.createRow(0); //創建第二列,即第二個單元格 row.createCell(1).setCellValue("第一行第二列"); row.createCell(3).setCellValue("第一行第四列"); //創建第二行 HSSFRow row1 = sheet.createRow(1); //創建第二行的第一列 row1.createCell(0).setCellValue("第二行第一列"); row1.createCell(1).setCellValue("第二行第二列"); row1.createCell(3).setCellValue("第二行第四列"); //創建第三行 HSSFRow row2 = sheet.createRow(2); //創建第二行的第一列 row2.createCell(0).setCellValue("第三行第一列"); row2.createCell(1).setCellValue("第三行第二列"); row2.createCell(2).setCellValue("第三行第三列"); row2.createCell(3).setCellValue("第三行第四列"); row2.createCell(4).setCellValue("第三行第五列"); row2.createCell(5).setCellValue("第三行第六列"); //創建第四行 HSSFRow row3 = sheet.createRow(3); //創建第三行的第一列 row3.createCell(0).setCellValue("數據1"); row3.createCell(1).setCellValue("數據2"); row3.createCell(2).setCellValue("數據3"); row3.createCell(3).setCellValue("數據4"); row3.createCell(4).setCellValue("數據5"); row3.createCell(5).setCellValue("數據6"); // 讓列寬隨着導出的列長自動適應 for (int colNum = 0; colNum < 6; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; // 當前行未被使用過 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue() .getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if (colNum == 0) { sheet.setColumnWidth(colNum, (columnWidth - 2) * 256); } else { sheet.setColumnWidth(colNum, (columnWidth + 4) * 256); } } workbook.write(output); } catch (Exception e) { e.printStackTrace(); } if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.service層調用該方法
ExcelMBang.exportExcel("測試","測試表.xlsx",response);
3.結果如圖
4.使用的poi jar包版本
<poi.version>3.17</poi.version>