为防止内存开销过大,通过Apache POI技术实现报表的分页导

2019-08-03  本文已影响0人  iMUST_Clown

本文内容

前置知识

代码如下:

工具类

可以创建工作薄的sheet页信息和表格信息。 目前暂未很好实现自动列宽功能,暂时外部传值进来。
package workhard.poiUtil;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

public class POIUtil {
    /**
     * 根据调节创建sheet页和sheet页名称
     * @param workbook
     * @param sheetName
     * @param sheetCount
     * @return
     */
    public static List<HSSFSheet> createExcelSheet(HSSFWorkbook workbook,String sheetName,int sheetCount){
        List<HSSFSheet> sheetList=new ArrayList<HSSFSheet>();
        for (int i = 0; i < sheetCount; i++) {
            sheetList.add(i,workbook.createSheet(sheetName+"-"+(i+1)));
        }
        return sheetList;
        
    }
    /**
     * 创建sheet内容
     * @param sheet
     * @param cellWidth
     * @param columnCount
     * @param rowCount
     * @param titleStyle
     * @param titleNames
     * @param contentStyle
     * @param contentStrs
     * @return
     */
    public static HSSFSheet createHSSFSheetContent(HSSFSheet sheet,int[] cellWidth,int columnCount,int rowCount,HSSFCellStyle titleStyle,String[] titleNames,HSSFCellStyle contentStyle,String contentStrs[][]){
//      列宽
        for (int i = 0; i < columnCount; i++) {
            sheet.setColumnWidth(i, 256*cellWidth[i]);
        }
//      编写表头区域
        HSSFRow titleRow=sheet.createRow(0);
        List<HSSFCell> titleRowCells =new ArrayList<HSSFCell>();
        for (int tcol = 0; tcol < columnCount ; tcol++) {
            titleRowCells.add(titleRow.createCell(tcol));
            titleRowCells.get(tcol).setCellStyle(titleStyle);
            titleRowCells.get(tcol).setCellValue(titleNames[tcol]);
        }
//      编写内容区域
        for (int row = 0; row < rowCount; row++) {
            HSSFRow contentRow=sheet.createRow(row+1);
            List<HSSFCell> contentCells=new ArrayList<HSSFCell>();
            for (int col = 0; col < columnCount; col++) {
                contentCells.add(contentRow.createCell(col));
                contentCells.get(col).setCellStyle(contentStyle);
                contentCells.get(col).setCellValue(contentStrs[row][col]);
            }
        }
        return sheet;
    }
    /**
     * 假定通用标题样式
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createTitleStyle(HSSFWorkbook workbook) {
        HSSFCellStyle titleStyle=workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        return titleStyle;
    }
    /**
     * 假定内容样式
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createContenrStyle(HSSFWorkbook workbook) {
        HSSFCellStyle contentStyle=workbook.createCellStyle();
        contentStyle.setAlignment(HorizontalAlignment.CENTER);
        return contentStyle;
    }
}

测试类

和业务进行关联,返回一个HSSFWorkbook实例,写入到流中,返回到页面,即可实现通过浏览器导出Excel报表的操作。
package workhard.poiUtil;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class POIUtilTest{
    private static int DEFAULT_SELECT_COUNT=15000;
    
    public HSSFWorkbook exportReportForm(String selectCriteria) {
//      根据条件查询数据库得到的条数
        int count=0;
        if (count == 0) {
            return null;
        }
//      分页数
        int page=count/DEFAULT_SELECT_COUNT+1;
        int sheetCount=page+1;
        HSSFWorkbook workbook=new HSSFWorkbook();
        List<HSSFSheet> sheetList = POIUtil.createExcelSheet(workbook, "这里是sheet页名", sheetCount);
        String titleNames[]= {"列0","列1","列2","列3","列4","列5"};
        int titleCount=titleNames.length;
//      中文记得乘2
        int cellWidth[]= {10,16,20,14,18};
        for (int i = 0; i < page; i++) {
            //后面根据分页查询出来的
            List<Object> objectList=getObjectsPaging(selectCriteria, DEFAULT_SELECT_COUNT*page, DEFAULT_SELECT_COUNT);
            int rowCount=objectList.size();
            String contentStrs[][]=new String[titleCount][DEFAULT_SELECT_COUNT];
            for (int row = 0; row < objectList.size() ; row++) {
                contentStrs[row][0]=objectList.get(row).toString();//这里面用String类型的元素最好,如果不是,建议新建get方法转换。
                contentStrs[row][1]=objectList.get(row).toString();
                contentStrs[row][2]=objectList.get(row).toString();
                contentStrs[row][3]=objectList.get(row).toString();
            }
            POIUtil.createHSSFSheetContent(sheetList.get(page), cellWidth, titleCount, rowCount, POIUtil.createTitleStyle(workbook), titleNames, POIUtil.createContenrStyle(workbook), contentStrs);
        }
        return null;
        
    }
    
    public List<Object>  getObjectsPaging(String selectCriteria,int resultCount,int pageSize){
//      根据条件查询数据库得到的条数
        int count=0;
        if (count == 0) {
            return null;
        }
//      根据条件分页查询,得到对象集合
        return new ArrayList<Object>();
    }
}
 

上一篇下一篇

猜你喜欢

热点阅读