日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

从数据库中取出数据表,导入并生成excel

發布時間:2023/12/13 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从数据库中取出数据表,导入并生成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

轉載于:https://www.cnblogs.com/minxiaofei/p/10081073.html

總結

以上是生活随笔為你收集整理的从数据库中取出数据表,导入并生成excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。