润乾报表V4大数据api分批导出excel

报表在api导出excel的时候,如果遇到数据量较大时,一次性导出可能会导致内存溢出或程序卡死的情况。

那么,针对这种情况,润乾报表可以借鉴大报表的导出机制,即分批导出。 Api内可以算出总共分多少页(根据总条数和每页的条数),然后遍历页数按页导出到excel文件,且导出过程中每导出一页及时把当前页的缓存清掉,避免出现同样溢出的情况。

下面的内容为参考示例,报表总共800多条(测试用),代码中有详细的注释,当遇到类似情况时可直接参考使用:

importjava.sql.Connection;

import java.sql.Driver;

importjava.sql.DriverManager;

 

importcom.runqian.report4.cache.CacheManager;

importcom.runqian.report4.model.ReportDefine;

importcom.runqian.report4.model.engine.ExtCellSet;

importcom.runqian.report4.usermodel.Context;

importcom.runqian.report4.usermodel.Engine;

importcom.runqian.report4.usermodel.IReport;

importcom.runqian.report4.usermodel.PageBuilder;

importcom.runqian.report4.util.ReportUtils;

importcom.runqian.report4.view.excel.ExcelReport;

public class RunsingleRaq {

    public static void main(String[] args) {

        Stringlicense = "D:/server_windows.lic";

        ExtCellSet.setLicenseFileName(license);

        String raq = "D:/bbb.raq";

        // 上下文

        //Context cxt = new Context();

        Connection con = null;

        try {

            Driver driver = (Driver) Class.forName("org.hsqldb.jdbcDriver")

                    .newInstance();

            DriverManager.registerDriver(driver);

            con = (Connection) DriverManager.getConnection(

                    "jdbc:hsqldb:hsql://localhost:9001/runqianDB", "sa", "");

        } catch (Exception e) {

            e.printStackTrace();

        }

        // 设置默认数据源连接

        //cxt.setConnection("demo", con);

        ReportDefine rd = null;       

        PageBuilder pb = null;

        try {

            rd = (ReportDefine) ReportUtils.read(raq);

            // 读入报表,已知(不知道共多少条就先算  然后看共几页再循环遍历)总共8543 for3次即可,这里就写死了

            for (int i = 0; i < 3; i++) {

                Context cxt = new Context();

                cxt.setConnection("demo", con);

                System.out.println("---- "+(i+1)+"次算 ----");

                cxt.setParamValue("startRow", 300 * i);

                int endRow = 0;

                if(i==2){

                    endRow=853;

                }else{

                    endRow = 300 * (i + 1)-1;

                }

                cxt.setParamValue("endRow", endRow);

                System.out.println("---- 起始行:"+cxt.getParamValue("startRow"));

               

                Engine engine = new Engine(rd, cxt); // 构造报表引擎

                IReport ir = engine.calc(); // 运算报表

                System.out.println("---- ir数据行数:"+ir.getRowCount());

                ExcelReport excel = new ExcelReport();

                excel.export(ir);

                excel.saveTo("D:/test"+i+".xls");

                CacheManager.getInstance().deleteReportEntry(raq);//清除单个报表缓存

            }

        } catch (Exception e) {

            e.printStackTrace();

        } catch (Throwable e) {

            e.printStackTrace();

        }

    }

 

}

从上面代码可以看出,通过动态传入startRow和endRow即可做到分批导出大数据报表到excel文件。

猜你喜欢

转载自blog.csdn.net/cainiao_M/article/details/78770945