zoukankan      html  css  js  c++  java
  • java POi excel 写入大批量数据

    直接贴代码:

    package jp.co.misumi.mdm.batch.common.jobrunner;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;
    
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.streaming.SXSSFSheet;
    import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class Demo16
    {
    
        public static void main(String[] args) throws InvalidFormatException
        {
            FileOutputStream out = null;  
            OPCPackage pkg;
            // TODO Auto-generated method stub
            
            XSSFWorkbook wbTemplate;
            try
            {
                pkg = OPCPackage.open(new File("F:/code/mdm-batch/target/classes/template/excel/TP.xlsx"));
                wbTemplate = new XSSFWorkbook(pkg);
                SXSSFWorkbook writeWB = new SXSSFWorkbook(wbTemplate, 5000, true, true);
                SXSSFSheet writeSheet = writeWB.getSheet("ProductMST");
                // Workbook writeWB = new SXSSFWorkbook(500);    
                 //org.apache.poi.ss.usermodel.Sheet writeSheet = writeWB.createSheet("1");  
                System.out.println(String.valueOf(new Date())); 
                try
                {
                    Thread.sleep(5000);
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                }
                for (int i=101; i<500000;i++) {
                     System.out.println(String.valueOf(i));
                     Row writeRow = writeSheet.createRow(i);        
                     for (int j = 32; j < 100; j++) {    
                         Cell cell = writeRow.createCell(j);
                         //cell.setCellValue("Hello");
                         cell.setCellValue(j+"Hello"+i);
                         cell.setCellValue(j+"Hello");
                        /* CellStyle cellStyle = cell.getCellStyle();
                         cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
                         cellStyle.setBorderTop(CellStyle.BORDER_THIN);
                         cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
                         cellStyle.setBorderRight(CellStyle.BORDER_THIN);
                         cell.setCellStyle(cellStyle);*/
                     } 
                 }    
                System.out.println(String.valueOf(new Date())); 
                 File file = new File("F:/code/mdm-batch/target/classes/template/excel/6666666666666555.xlsx");
                 out = new FileOutputStream(file);
                 writeWB.write(out);    
                 out.flush();  
                 out.close();  
                 System.out.println("测试成功!");  
            }
            catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }
    
    }
  • 相关阅读:
    AMR转换MP3 linuxCentOS 版(不管任何语言可以使用shell命令在linux执行转换语句)
    MySql 入门——日期计算
    javascript深度剖析之 【 var 关键字】。
    javascript动画浅析。
    javascript之this关键字浅析。
    javascript【AMD模块加载器】浅析V3(添加CSS加载功能,重构内部流程)
    html5 canvas 自制小游戏系列之 【贪吃蛇】。
    javascript设计模式简单介绍之【工厂模式】
    javascript【AMD模块加载器】浅析
    javascript【AMD模块加载器】浅析V2(整合DOM ready)
  • 原文地址:https://www.cnblogs.com/sunxun/p/5868233.html
Copyright © 2011-2022 走看看