org.fl.modules.excel.poi.exportExcel.multi.SXSSFWorkBookOperation.java Source code

Java tutorial

Introduction

Here is the source code for org.fl.modules.excel.poi.exportExcel.multi.SXSSFWorkBookOperation.java

Source

/*
 * Copyright (C) 2013 Open Source Project
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * # * SXSSFWorkBookUtil.java Create on 2013-11-18 ?08:57:43
 * # * project david-modules-excel
 * # * Copyright 2013 by .
 * #
 */
/**
 * ??SXSSFWorkBookUtil.java
 * ?
 * 2013-11-18
 * Copyright  Corporation 2013
 * ?
 */
package org.fl.modules.excel.poi.exportExcel.multi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.fl.modules.excel.poi.exportExcel.ISXSSFWorkBook;

/**
 * @author David.Yang
 * @version 1.0
 *          CreateDate2013-11-18 ?08:57:43
 *          
 */
/**
 * ??david-modules-excel
 * ??SXSSFWorkBookUtil
 * ??
 * David.Yang
 * 2013-11-18 ?08:57:43
 * David.Yang
 * 2013-11-18 ?08:57:43
 * 
 * 
 * @version
 */
public class SXSSFWorkBookOperation {

    private static Logger logger = Logger.getLogger(SXSSFWorkBookOperation.class);
    private int pageNo; // 

    private int pageSize; // ?

    private int rowaccess = 100;// ;

    private int sheet_num;// ?sheet_numSHEET

    private ISXSSFWorkBook sxIsxssfWorkBook;// excel??
    private int totalRows;// ?
    private SXSSFWorkbook wb;// poi workbook

    /**
     *  SXSSFWorkBookUtil.
     */
    public SXSSFWorkBookOperation() {
        // TODO Auto-generated constructor stub

        /* keep 100 rowsin memory,exceeding rows will be flushed to disk */
        wb = new SXSSFWorkbook(rowaccess);
        wb.setCompressTempFiles(false);
    }

    public void compressFiles2Zip() {
        org.apache.commons.io.output.ByteArrayOutputStream byteArrayOutputStream = new org.apache.commons.io.output.ByteArrayOutputStream();
        try {
            byteArrayOutputStream = write(byteArrayOutputStream);
            ZipArchiveOutputStream zaos = null;
            zaos = new ZipArchiveOutputStream(byteArrayOutputStream);
            // Use Zip64 extensions for all entries where they are required
            zaos.setUseZip64(Zip64Mode.AsNeeded);
            if (byteArrayOutputStream != null) {
                ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry("excel");
                zaos.putArchiveEntry(zipArchiveEntry);
                try {
                    byteArrayOutputStream.writeTo(zaos);
                    zaos.closeArchiveEntry();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * excel???
     * 
     * @param int rowSize 
     * @param ISXSSFWorkBook
     *            sxsWorkBook
     * @param List
     *            list ?
     **/

    public void excute(int pageSize, ISXSSFWorkBook sxsWorkBook, List list) throws IOException, RuntimeException {
        int rowCount = list.size();
        if (rowCount > pageSize) {
            throw new RuntimeException("??excel?");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("excute(int, ISXSSFWorkBook, List) - start");
        }

        long curr_time = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("excute(int, ISXSSFWorkBook, List) - " + new Date().toString());
        }
        Sheet sh = null;
        synchronized (wb) {
            sh = wb.createSheet();

            sxsWorkBook.doExecuteCreateTitle(sh);

            // ?SHEETrowCount ROW
            for (int rownum = 0; rownum < rowCount; rownum++) {
                Row row = sh.createRow(rownum + 1);

                Object object = list.get(rownum);
                sxsWorkBook.doExecute(row, object);

                // ??,?
                if (rownum % rowaccess == 0) {
                    ((SXSSFSheet) sh).flushRows();
                }
            }
        }
        /*  */
        if (logger.isDebugEnabled()) {
            logger.debug("excute(int, ISXSSFWorkBook, List) - :"
                    + (System.currentTimeMillis() - curr_time) / 1000 + "");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("excute(int, ISXSSFWorkBook, List) - " + new Date().toString());
        }

        if (logger.isDebugEnabled()) {
            logger.debug("excute(int, ISXSSFWorkBook, List) - end");
        }
    }

    public void excute(ISXSSFWorkBook sxsWorkBook, List list) throws IOException, RuntimeException {
        excute(PageSizeUtils.pageSize, sxsWorkBook, list);
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getSheet_num() {
        return sheet_num;
    }

    public ISXSSFWorkBook getSxIsxssfWorkBook() {
        return sxIsxssfWorkBook;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public SXSSFWorkbook getWb() {
        return wb;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setSheet_num(int sheetNum) {
        sheet_num = sheetNum;
    }

    public void setSxIsxssfWorkBook(ISXSSFWorkBook sxIsxssfWorkBook) {
        this.sxIsxssfWorkBook = sxIsxssfWorkBook;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    /**
     * ?
     * 
     * @param ByteArrayOutputStream
     *            outputStream
     * @return byte[]
     */
    public byte[] write(ByteArrayOutputStream outputStream) throws IOException {
        byte[] b = null;

        wb.write(outputStream);
        outputStream.flush();

        b = outputStream.toByteArray();
        return b;

    }

    public org.apache.commons.io.output.ByteArrayOutputStream write(
            org.apache.commons.io.output.ByteArrayOutputStream outputStream) throws IOException {
        byte[] b = null;

        wb.write(outputStream);

        outputStream.flush();

        return outputStream;

    }

}