从数据库中取出数据表,导入并生成excel
生活随笔
收集整理的這篇文章主要介紹了
从数据库中取出数据表,导入并生成excel
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
@RequestMapping("/numericalStatement1")public void createExcel(HttpServletResponse resp) throws Exception{try {String path = "G:/test.xlsx";// 創(chuàng)建新的Excel 工作簿XSSFWorkbook workbook = new XSSFWorkbook();// 在Excel工作簿中建一工作表,其名為缺省值// 如要新建一名為"用戶表"的工作表,其語句為:XSSFSheet sheet = workbook.createSheet("成績(jī)表");// 在索引0的位置創(chuàng)建行(最頂端的行)XSSFRow row = sheet.createRow((short) 0);//在索引0的位置創(chuàng)建單元格(左上端)XSSFCell cell = row.createCell((short) 0);//創(chuàng)建單元格樣式CellStyle cellStyle = workbook.createCellStyle();// 設(shè)置這些樣式
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);// 在單元格中輸入一些內(nèi)容cell = row.createCell((short) 0);cell.setCellValue("成績(jī)編號(hào)");cell.setCellStyle(cellStyle);cell = row.createCell((short) 1);cell.setCellValue("組織架構(gòu)參數(shù)表編號(hào)");cell.setCellStyle(cellStyle);cell = row.createCell((short) 2);cell.setCellValue("試卷編號(hào)");cell.setCellStyle(cellStyle);cell = row.createCell((short) 3);cell.setCellValue("客觀題成績(jī)");cell.setCellStyle(cellStyle);cell = row.createCell((short) 4);cell.setCellValue("主觀題成績(jī)");cell.setCellStyle(cellStyle);cell = row.createCell((short) 5);cell.setCellValue("總成績(jī)");cell.setCellStyle(cellStyle);//查詢數(shù)據(jù)庫中所有的數(shù)據(jù)
// ResultMapper mapper = getMapper(ResultMapper.class);
// VtUserCriteria cri = new VtUserCriteria();
// cri.createCriteria().andUserEnabledEqualTo(1);
List<Result> list = resultService.selectAllResult();/*//第一個(gè)sheet第一行為標(biāo)題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);// 第四步,創(chuàng)建單元格,并設(shè)置值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); //調(diào)整第一列寬度(自適應(yīng)),只識(shí)別數(shù)字、字母sheet.autoSizeColumn((short) 1); //調(diào)整第二列寬度//調(diào)整第三列寬度,有中文,先判斷這一列的最長(zhǎng)字符串// int length = stu.getPaperId().getBytes().length;// sheet.setColumnWidth((short)2,(short)(length*2*256));sheet.autoSizeColumn((short) 2); //調(diào)整第三列寬度sheet.autoSizeColumn((short) 3); //調(diào)整第四列寬度sheet.autoSizeColumn((short) 4); //調(diào)整第五列寬度sheet.autoSizeColumn((short) 5); //調(diào)整第六列寬度/*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);// 把相應(yīng)的Excel 工作簿存盤
workbook.write(fOut);//清空緩沖區(qū)數(shù)據(jù)
fOut.flush();// 操作結(jié)束,關(guān)閉文件
fOut.close();System.out.println("文件生成...");} catch (Exception e) {System.out.println("已運(yùn)行 xlCreate() : " + e);}}
代碼引自https://www.cnblogs.com/zhxn/p/7016380.html
轉(zhuǎn)載于:https://www.cnblogs.com/minxiaofei/p/10081073.html
總結(jié)
以上是生活随笔為你收集整理的从数据库中取出数据表,导入并生成excel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj4919 大根堆
- 下一篇: python-mysql 基础知识记录