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();  
            }  
        }
    
    }
  • 相关阅读:
    Hadoop使用实例
    hdfs操作命令
    Hadoop安装与HDFS体系结构
    Linux和MySQL的安装与基本操作
    Hadoop演进与Hadoop生态
    作业一
    Shader笔记——9.简单灰色效果+遮罩
    iOS——xcodeproj脚本
    iOS——KSAdSDK
    UnityPlugins——4.SignInWithApple
  • 原文地址:https://www.cnblogs.com/sunxun/p/5868233.html
Copyright © 2011-2022 走看看