使用POI导出数据库大量数据,使用的主要jar包:poi-3.8-20120326.jar、poi-ooxml-3.8-20120326.jar
样例代码:
import java.io.FileOutputStream; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook; public class LotExport { public static void main(String[] args) { try { exportLot(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * 大批量导出 * @throws Exception */ public static void exportLot() throws Exception { String sql = "SELECT COUNT(*) FROM T_QR_B"; int count = DBUtil.count(sql); int limit = 60000; int pageNumber = (count%limit==0) ? (count/limit) : (count/limit+1); // 创建Excel的工作书册 Workbook,对应到一个excel文档 Workbook wb = new SXSSFWorkbook(1000); // keep 100 rows in memory, exceeding rows will be flushed to disk for(int p=0; p<pageNumber; p++) {//分页查询导出数据 sql = "SELECT TOP " + limit + " * FROM (SELECT * FROM T_QR_B) T1" +" WHERE ROW_ID NOT IN (SELECT TOP " + p*limit + " ROW_ID FROM (SELECT * FROM T_QR_B) T2 ORDER BY ROW_ID)" +" ORDER BY ROW_ID"; List list = DBUtil.execQuery(sql); // 创建Excel的工作sheet,对应到一个excel文档的tab Sheet sheet = wb.createSheet("sheet" + p); Row row = null; Cell cell = null; //设置标题 row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("序号"); cell = row.createCell(1); cell.setCellValue("QR"); cell = row.createCell(2); cell.setCellValue("OWNER_ID"); cell = row.createCell(3); cell.setCellValue(""); cell = row.createCell(4); cell.setCellValue("SKU_ID"); cell = row.createCell(5); cell.setCellValue("OWNER_PO_NO"); cell = row.createCell(6); cell.setCellValue("QTY"); cell = row.createCell(7); cell.setCellValue("EXTERNAL_LOT"); cell = row.createCell(8); cell.setCellValue("WORK_NO"); cell = row.createCell(9); cell.setCellValue("PRODUCE_DATE"); cell = row.createCell(10); cell.setCellValue("LASER1"); cell = row.createCell(11); cell.setCellValue("LASER2"); cell = row.createCell(12); cell.setCellValue("CHECK_CODE"); cell = row.createCell(13); cell.setCellValue("CARTON_NUMBER"); cell = row.createCell(14); cell.setCellValue("PALLET_NUMBER"); cell = row.createCell(15); cell.setCellValue("PACKING_LINE"); cell = row.createCell(16); cell.setCellValue("IS_ACTIVE"); cell = row.createCell(17); cell.setCellValue("INVENTORY_STATUS"); cell = row.createCell(18); cell.setCellValue("SCAN_DATE"); cell = row.createCell(19); cell.setCellValue("REMARK"); cell = row.createCell(20); cell.setCellValue("CREATED_BY"); cell = row.createCell(21); cell.setCellValue("CREATED_DATE"); cell = row.createCell(22); cell.setCellValue("UPDATED_BY"); cell = row.createCell(23); cell.setCellValue("UPDATED_DATE"); for(int i=0; i<list.size(); i++) { Map map = (Map) list.get(i); //System.out.println("p:"+p + ", i:" +i); row = sheet.createRow(i + 1); cell = row.createCell(0); cell.setCellValue((null==map.get("ROW_ID")) ? "" : map.get("ROW_ID").toString()); cell = row.createCell(1); cell.setCellValue((null==map.get("QR")) ? "" : map.get("QR").toString()); cell = row.createCell(2); cell.setCellValue((null==map.get("OWNER_ID")) ? "" : map.get("OWNER_ID").toString()); cell = row.createCell(3); cell.setCellValue((null==map.get("SKU_ID")) ? "" : map.get("SKU_ID").toString()); cell = row.createCell(4); cell.setCellValue((null==map.get("BATCH_NO")) ? "" : map.get("BATCH_NO").toString()); cell = row.createCell(5); cell.setCellValue((null==map.get("OWNER_PO_NO")) ? "" : map.get("OWNER_PO_NO").toString()); cell = row.createCell(6); cell.setCellValue((null==map.get("QTY")) ? "" : map.get("QTY").toString()); cell = row.createCell(7); cell.setCellValue((null==map.get("EXTERNAL_LOT")) ? "" : map.get("EXTERNAL_LOT").toString()); cell = row.createCell(8); cell.setCellValue((null==map.get("WORK_NO")) ? "" : map.get("WORK_NO").toString()); cell = row.createCell(9); cell.setCellValue((null==map.get("PRODUCE_DATE")) ? "" : map.get("PRODUCE_DATE").toString()); cell = row.createCell(10); cell.setCellValue((null==map.get("LASER1")) ? "" : map.get("LASER1").toString()); cell = row.createCell(11); cell.setCellValue((null==map.get("LASER2")) ? "" : map.get("LASER2").toString()); cell = row.createCell(12); cell.setCellValue((null==map.get("CHECK_CODE")) ? "" : map.get("CHECK_CODE").toString()); cell = row.createCell(13); cell.setCellValue((null==map.get("CARTON_NUMBER")) ? "" : map.get("CARTON_NUMBER").toString()); cell = row.createCell(14); cell.setCellValue((null==map.get("PALLET_NUMBER")) ? "" : map.get("PALLET_NUMBER").toString()); cell = row.createCell(15); cell.setCellValue((null==map.get("PACKING_LINE")) ? "" : map.get("PACKING_LINE").toString()); cell = row.createCell(16); cell.setCellValue((null==map.get("IS_ACTIVE")) ? "" : map.get("IS_ACTIVE").toString()); cell = row.createCell(17); cell.setCellValue((null==map.get("INVENTORY_STATUS")) ? "" : map.get("INVENTORY_STATUS").toString()); cell = row.createCell(18); cell.setCellValue((null==map.get("SCAN_DATE")) ? "" : map.get("SCAN_DATE").toString()); cell = row.createCell(19); cell.setCellValue((null==map.get("REMARK")) ? "" : map.get("REMARK").toString()); cell = row.createCell(20); cell.setCellValue((null==map.get("CREATED_BY")) ? "" : map.get("CREATED_BY").toString()); cell = row.createCell(21); cell.setCellValue((null==map.get("CREATED_DATE")) ? "" : map.get("CREATED_DATE").toString()); cell = row.createCell(22); cell.setCellValue((null==map.get("UPDATED_BY")) ? "" : map.get("UPDATED_BY").toString()); cell = row.createCell(23); cell.setCellValue((null==map.get("UPDATED_DATE")) ? "" : map.get("UPDATED_DATE").toString()); } System.out.println("第" + p + "页导出成功"); } FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xlsx"); wb.write(os); os.close(); System.out.println("导出成功"); } }
相关推荐
java poi导出大量数据到Excel
poi读取大量数据会造成gc内存溢出的报错,由于垃圾回收机制无法将大量的对象及时的回收,而这些对象又会保存在内存中,会导致内存不够用的情况,这时候我们就需要使用新的方法,读取为cvs即可.此解决方案可支持千万数据的...
轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,测试实现16500000条数据大概80秒左右;具体表里内容。
轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,csv大数据量导出(千万级别,不会内存溢出),多线程导出 ,生产环境已经很稳定的使用着
阿里巴巴easyExcel实现大数据导出!!
使用Easy POI的API实现POI对Excel的所有操作,并且它的封装类可以让你快速上手,无需编写大量的Excel格式设置等复杂代码,高效,完善,高并发
用开源 Apache POI 技术导出Excel,解决导出大数据出现OOM、栈溢出问题,此资源可实现百万级数据多线程分批导出Excel文件,不会内存溢出,生产环境已很稳定的使用者,所以用到的技术很核心、值得参考
使用POI、JXL导出大量的数据到Excel很容易造成内存溢出,而CSV采用流的方式将大数据分批并压缩导出
easyExcel 2.1.7 poi 3.17 springboot 2.2.5 lombok 1.18.12
poi 3.7 java导出excel表格 描述来自http://poi.apache.org Apache POI - 文本提取 概述 多年以来,Apache POI已经为所有项目支持的文件格式提供了基本的文本提取。另外,除了(简单)文本之外,这些文件还提供对与...
主要为大家详细介绍了Java实现excel大数据量导入,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
poi,excel导出或者下载工具类,先生成文件可以兼容大量数据,有需求的同学可以修改兼容百万以上数据
|英文|为什么选择AutoExcel? Excel的导入和导出在软件开发中非常普遍,只要您是程序员,就一定会遇到。...同时,实施顾问提供的Excel是导入和导出模板,除非添加了新的数据源或字段,否则模板更新不需要使用开发资源
系统在设计时充分考 虑了用户的使用习惯,提供大量的人性化设计,帮助用户快速定位所需要的功能、简便 操作,满足绩效考核大量数据导入、审核、汇总操作的的需要。 3设计思路 3.1采用基于J2EE的体系结构 系统采用...
11.使用jacob、jxl、apache poi的; 12.需要在Web中调用Word/Excel,并且追求系统运行稳定、可靠的; 13.最终用户可以浏览 Word/Excel/Ppt 文档内容,但需要禁止复制、粘贴、下载、另存的; 14.需要在html页面中在线...