JAVA POI 导出Excel 多个Sheet分页

package com.mms.controller;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.mms.utils.ExportExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Test {
    public static void main(String[] args) {
a();
    }

    public static void a(){
        ExportExcelUtil gwu = null;
        String path = null;
        try{
            gwu = new ExportExcelUtil();
            path = "E:\\test2.xls";
//1、输出的文件地址及名称
            OutputStream out = new FileOutputStream(path);
//2、sheet表中的标题行内容,需要输入excel的汇总数据
            String[] headers = {"姓名", "年级"};
            List list = new ArrayList();
            Student student1 = new Student("小明", "二年级");
            Student student2 = new Student("小光", "三年级");
            Student student3 = new Student("小花", "四年级");
            list.add(student1);
            list.add(student2);
            list.add(student3);
            List> summaryData = new ArrayList>();
            for (Student s :list){
                List rowData = new ArrayList();
                rowData.add(s.getGrade());
                rowData.add(s.getName());
                summaryData.add(rowData);
            }
/*
String[] summary = {"系统名称", "活动名称", "门店号", "日报时间", "发券数量", "使用数量"};
这些是对象封装数据
List> summaryData = new ArrayList>();
            List _listSummary = new ArrayList();
            for (SummaryInfo sum : _listSummary) {
                List rowData = new ArrayList();
                rowData.add(sum.getXtmc());
                rowData.add(sum.getHdmc());
                rowData.add(sum.getMdh());
                rowData.add(sum.getCreatTime());
                rowData.add(String.valueOf(sum.getHandoutTotal()));
                rowData.add(String.valueOf(sum.getUseTotal()));
                summaryData.add(rowData);
            }

String[] water = {"系统名称", "门店号", "门店名称", "小票号", "活动编号"
                    , "活动名称", "发券数量", "商品条码", "商品名称", "购买数量"
                    , "发券时间", "分类代码", "是否领赠", "数据是否为真"};
            List> waterData = new ArrayList>();
            List _listWater = new ArrayList();
            for (GenerWater wat : _listWater) {
                List rowData = new ArrayList();
                rowData.add(wat.getXtmc());
                rowData.add(wat.getMdh());
                rowData.add(wat.getMdmc());
                rowData.add(wat.getXph());
                rowData.add(wat.getHdbh());
                rowData.add(wat.getHdmc());
                rowData.add(wat.getFqsl());
                rowData.add(wat.getSptm());
                rowData.add(wat.getSpmc());
                rowData.add(wat.getSl());
                rowData.add(wat.getFqsj());
                rowData.add(wat.getFldm());
                rowData.add(wat.getSflq());
                rowData.add(wat.getReal());
                waterData.add(rowData);
            }
*/

//3、生成格式是xlsx可存储103万行数据,如果是xls则只能存不到6万行数据
            HSSFWorkbook wb = new HSSFWorkbook();
//第一个表格内容
            gwu.exportExcel(wb, 0, "ces", headers, summaryData, out);
//第二个表格内容
            gwu.exportExcel(wb, 1, "ccccc", headers, summaryData, out);
//将所有的数据一起写入,然后再关闭输入流。
            wb.write(out);
            out.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }


}

 
 

你可能感兴趣的:(java)