java 大数据 SXSSFWorkbook百万级数据写入到Excel

使用java实现大数据的写入,用到了XSSFWorkbook和SXSSFWorkbook

不多说。直接上图上代码。 

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class TestExcelWriter {
    public static void main(String[] args) {
        FileInputStream  in = null;
        BufferedOutputStream os = null;
        long a = System.currentTimeMillis();
        try
        {
            File file = new File("E:\\parkingrecordReport.xlsx");
            in = new FileInputStream(file);
            XSSFWorkbook wb1 = new XSSFWorkbook(in);
            SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 1000);
            CellStyle cellStyle = wb.createCellStyle();  
            cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //下边框    
            cellStyle.setBorderLeft(CellStyle.BORDER_THIN);//左边框    
            cellStyle.setBorderTop(CellStyle.BORDER_THIN);//上边框    
            cellStyle.setBorderRight(CellStyle.BORDER_THIN);//右边框 
            
            Font font = wb.createFont();    
            font.setFontName("微软雅黑");    
            font.setFontHeightInPoints((short) 10);//设置字体大小    
            cellStyle.setFont(font);
            
            //设置字体居中
            cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            
            //第一行标题
            Sheet sheet = wb.getSheetAt(0);
            Row row0 = sheet.createRow(0);
            //合并单元格
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,2));
            Cell parkNameCell = row0.createCell(0);
            parkNameCell.setCellStyle(cellStyle);
            parkNameCell.setCellValue("百万行数据写入");

            Row row2 = sheet.createRow(1);
            for(int cellnum = 0; cellnum < 3; cellnum++){
                Cell cell = row2.createCell(cellnum);
                cell.setCellStyle(cellStyle);
                if (cellnum == 0) {
                    cell.setCellValue("序号");
                } else if (cellnum == 1) {
                    cell.setCellValue("随机数");
                } else if (cellnum == 2) {
                    cell.setCellValue("时间");
                }
            }
            int sn = 1;
            String property = "";
            int startRow = 2;
            for (int i = 0; i < 1000000; i++)
            {
                Row row = sheet.createRow(startRow);
                for(int cellnum = 0; cellnum < 3; cellnum++){
                    Cell cell = row.createCell(cellnum);
                    cell.setCellStyle(cellStyle);
                    if (cellnum == 0) {
                        cell.setCellValue(sn);
                    } else if (cellnum == 1) {
                        property = String.valueOf(Math.random());
                        cell.setCellValue(property);
                    } else if (cellnum == 2) {
                        property = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                        cell.setCellValue(property);
                    } 
                }
                sn++;
                startRow++;
            }
            Row totalRow = sheet.createRow(startRow);
            Cell cell = totalRow.createCell(0);
            cell.setCellValue("统计所需时间(s):");
            cell.setCellStyle(cellStyle);
            cell = totalRow.createCell(1);
            cell.setCellStyle(cellStyle);
            cell = totalRow.createCell(2);
            cell.setCellStyle(cellStyle);
            cell.setCellValue((System.currentTimeMillis() - a)/1000.0);
            String tempPath ="E:\\millionRandom("+DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")+"_create).xlsx";
            os = new BufferedOutputStream(new FileOutputStream(tempPath));
            wb.write(os);
            os.close();
            System.out.println("所需时间(s):"+(System.currentTimeMillis() - a));
        }
        catch (Exception e)
        {
            System.out.println(ExceptionUtils.getStackTrace(e));
        }
        finally
        {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(os);
        }
    }
}

tips: File file = new File("E:\\parkingrecordReport.xlsx");中的Excel随便一个空的表格就行,里面不能有内容,SXSSFWorkbook是不支持读取的,里面有内容写的时候会报错

所需要的jar包如下:

java 大数据 SXSSFWorkbook百万级数据写入到Excel_第1张图片

你可能感兴趣的:(学习笔记)