使用POI導(dǎo)出大批量數(shù)據(jù)到Excel操作步驟: 第一步: 你的自己準(zhǔn)備一個(gè)大批量的數(shù)據(jù) 最好能超過65536條以上 第二部: 開始編寫代碼,查詢所有的數(shù)據(jù),將結(jié)果集出傳遞給導(dǎo)出的工具類進(jìn)行導(dǎo)出 使用POI導(dǎo)出大批量數(shù)據(jù)到Excel操作步驟: 第一步: 你的自己準(zhǔn)備一個(gè)大批量的數(shù)據(jù) 最好能超過65536條以上 分享一個(gè)SQL文件 鏈接:https://pan.baidu.com/s/13sL7hATEWUTZCqrUHAbzJA 第二部: 開始編寫代碼,查詢所有的數(shù)據(jù),將結(jié)果集出傳遞給導(dǎo)出的工具類進(jìn)行導(dǎo)出 @RequestMapping("/export") public void exportBigDataToExcel(User user,HttpServletRequest request, HttpServletResponse respose) { // 創(chuàng)建時(shí)間格式變量后面要重新格式化日期時(shí)間 SimpleDateFormat sdf_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 設(shè)置文件名稱 String exportFileName = "用戶信息"; // 根據(jù)條件獲取數(shù)據(jù)庫(kù)中的所有數(shù)據(jù) List<User> bigDataList= userService.exportBigDataToExcel(user); // 獲取數(shù)據(jù)總行 int totalRowNum = memberIntegralDtoList.size(); try { //創(chuàng)建SXSSFWorkbook對(duì)象(excel的文檔對(duì)象) SXSSFWorkbook wb = new SXSSFWorkbook(); /* 設(shè)定單個(gè)sheet的最大數(shù)據(jù)行數(shù) 博主選的是2003 提醒: excel單個(gè)sheet的最大行 2003版:65536行 256列; 2007版:1048576行 16384列; 2010版:1048576行 16384列; 2013版:1048576行 16384列 */ int maxRowNum = 60000; // 根據(jù)查詢的數(shù)據(jù)總條數(shù)計(jì)算需要多少個(gè)sheet 來存儲(chǔ)數(shù)據(jù) int sheets = totalRowNum % 60000 == 0 ? (totalRowNum / maxRowNum) : (totalRowNum / maxRowNum + 1); // 循環(huán)創(chuàng)建sheet 并寫入數(shù)據(jù) for (int i = 0; i < sheets; i++) { // 創(chuàng)建SheetName SXSSFSheet sheet = wb.createSheet("用戶" + i); // 計(jì)算單sheet的數(shù)據(jù)起止范圍 int begin = (i - 1) * maxRowNum; int end = maxRowNum * i; // 此處需要進(jìn)行結(jié)束數(shù)據(jù)范圍的比對(duì) 當(dāng) i=3時(shí)數(shù)據(jù)截止是18W 但我們查詢出來的數(shù)據(jù)是15W 就會(huì)異常 所以需要進(jìn)行比對(duì) end = Math.min(end, totalRowNum); // 設(shè)定一個(gè)Excel的行數(shù)用來進(jìn)行Excel數(shù)據(jù)寫入換行的 int num = 0; // 最重要的 寫入數(shù)據(jù)到了 for (int j = begin; j < end; j++) { // 定義一個(gè)Excel的行對(duì)象 SXSSFRow rowContent = null; // 寫入表頭信息 if (num == 0) { //在sheet里創(chuàng)建第一行,參數(shù)為行索引(excel的行),可以是0~60000之間的任何一個(gè) SXSSFRow rowTitle = sheet.createRow(num); //創(chuàng)建單元格并設(shè)置單元格內(nèi)容 rowTitle.createCell(0).setCellValue("用戶編號(hào)"); rowTitle.createCell(1).setCellValue("用戶名稱"); rowTitle.createCell(2).setCellValue("用戶性別"); rowTitle.createCell(3).setCellValue("用戶電話"); rowTitle.createCell(4).setCellValue("用戶身份證"); rowTitle.createCell(5).setCellValue("家庭住址"); rowTitle.createCell(6).setCellValue("用戶昵稱"); rowTitle.createCell(7).setCellValue("用戶狀態(tài)"); } rowContent = sheet.createRow(++num); //在sheet里創(chuàng)建第三行 rowContent.createCell(0).setCellValue(bigDataList.get(j).getUserId() == null ? "未知" : bigDataList.get(j).getUserId()); rowContent.createCell(1).setCellValue(bigDataList.get(j).getUserName() == null ? "未知" : bigDataList.get(j).getUserName()); rowContent.createCell(2).setCellValue(bigDataList.get(j).getUserSex() == null ? "未知" : bigDataList.get(j).getUserSex()); rowContent.createCell(3).setCellValue(bigDataList.get(j).getUserPhone() == null ? "未知" : bigDataList.get(j).getUserPhone()); rowContent.createCell(4).setCellValue(bigDataList.get(j).getUserCard() == null ? "未知" : bigDataList.get(j).getUserCard()); rowContent.createCell(5).setCellValue(bigDataList.get(j).getAddress() == null ? "未知" : bigDataList.get(j).getAddress()); rowContent.createCell(6).setCellValue(bigDataList.get(j).getNickName() == null ? "未知" : bigDataList.get(j).getNickName()); rowContent.createCell(7).setCellValue(bigDataList.get(j).getUserState() == null ? "未知" : bigDataList.get(j).getUserState()); } } //輸出Excel文件 OutputStream output = response.getOutputStream(); response.reset(); // 設(shè)置輸出類型和文件名稱 response.setHeader("Content-disposition", "attachment; filename="+exportFileName+".xls"); response.setContentType("application/msexcel"); wb.write(output); output.close(); } catch (Exception e) { e.getMessage(); } logger.info("數(shù)據(jù)導(dǎo)出完成,共導(dǎo)出:"+totalRowNum+" 條數(shù)用戶信息") } ———————————————— 版權(quán)聲明:本文為CSDN博主「劉信晨」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/qq_36481052/article/details/105413145 |
|