从数据库中取出数据表,导入并生成excel
@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