@RequestMapping("/numericalStatement1")public void createExcel(HttpServletResponse resp) throws Exception{try {String path = "G:/test.xlsx";// 創建新的Excel 工作簿XSSFWorkbook workbook = new XSSFWorkbook();// 在Excel工作簿中建一工作表,其名為缺省值// 如要新建一名為"用戶表"的工作表,其語句為:XSSFSheet sheet = workbook.createSheet("成績表");// 在索引0的位置創建行(最頂端的行)XSSFRow row = sheet.createRow((short) 0);//在索引0的位置創建單元格(左上端)XSSFCell cell = row.createCell((short) 0);//創建單元格樣式CellStyle cellStyle = workbook.createCellStyle();// 設置這些樣式 cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 定義單元格為字符串類型 cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 在單元格中輸入一些內容cell = row.createCell((short) 0);cell.setCellValue("成績編號");cell.setCellStyle(cellStyle);cell = row.createCell((short) 1);cell.setCellValue("組織架構參數表編號");cell.setCellStyle(cellStyle);cell = row.createCell((short) 2);cell.setCellValue("試卷編號");cell.setCellStyle(cellStyle);cell = row.createCell((short) 3);cell.setCellValue("客觀題成績");cell.setCellStyle(cellStyle);cell = row.createCell((short) 4);cell.setCellValue("主觀題成績");cell.setCellStyle(cellStyle);cell = row.createCell((short) 5);cell.setCellValue("總成績");cell.setCellStyle(cellStyle);//查詢數據庫中所有的數據 // ResultMapper mapper = getMapper(ResultMapper.class); // VtUserCriteria cri = new VtUserCriteria(); // cri.createCriteria().andUserEnabledEqualTo(1); List<Result> list = resultService.selectAllResult();/*//第一個sheet第一行為標題XSSFRow rowFirst = sheet.createRow(0);rowFirst.setHeightInPoints(21.75f);*/for (int i = 0; i < list.size(); i++) {row = sheet.createRow((int) i + 1);Result stu = (Result) list.get(i);// 第四步,創建單元格,并設置值row.createCell((short) 0).setCellValue(stu.getId());row.createCell((short) 1).setCellValue(stu.getParaorgleadershipsId());row.createCell((short) 2).setCellValue(stu.getPaperId());row.createCell((short) 3).setCellValue(stu.getObjResult());row.createCell((short) 4).setCellValue(stu.getSubResult());row.createCell((short) 5).setCellValue(stu.getTotalResult());sheet.autoSizeColumn((short) 0); //調整第一列寬度(自適應),只識別數字、字母sheet.autoSizeColumn((short) 1); //調整第二列寬度//調整第三列寬度,有中文,先判斷這一列的最長字符串// int length = stu.getPaperId().getBytes().length;// sheet.setColumnWidth((short)2,(short)(length*2*256));sheet.autoSizeColumn((short) 2); //調整第三列寬度sheet.autoSizeColumn((short) 3); //調整第四列寬度sheet.autoSizeColumn((short) 4); //調整第五列寬度sheet.autoSizeColumn((short) 5); //調整第六列寬度/*Font font = workbook.createFont();font.setFontHeightInPoints((short)18); //字體大小sheet.setDefaultRowHeightInPoints(21.75f);font.setFontName("楷體");font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗體font.setColor(HSSFColor.GREEN.index); //綠字- 字體顏色*/}// 新建一輸出文件流FileOutputStream fOut = new FileOutputStream(path);// 把相應的Excel 工作簿存盤 workbook.write(fOut);//清空緩沖區數據 fOut.flush();// 操作結束,關閉文件 fOut.close();System.out.println("文件生成...");} catch (Exception e) {System.out.println("已運行 xlCreate() : " + e);}}
代碼引自https://www.cnblogs.com/zhxn/p/7016380.html