2017年3月22日

(FineReport)用代码生成 cpt 模板

demo 功能说明

通过调用 FineReport 引擎 API,生成一个包含两个文件数据集(Excel),并实现过滤功能的 cpt 模板。重点演示如何绑定数据列,并添加过滤条件,生成 cpt。
原始数据集:
1、销量:
demo%e9%94%80%e9%87%8f
2、销售总额
demo%e9%94%80%e5%94%ae%e6%80%bb%e9%a2%9d
通过“销售员”字段关联两个数据集,最终得到每个地区的销售总额:
demo%e7%bb%93%e6%9e%9c

demo 运行说明

1、依赖的 jar 包都在 git 仓库 http://www.finedevelop.com:2015/projects/ST/repos/lib/browse 及 ftp 服务器 ftp://env.finedevelop.com/report/stable 上。具体配置可参考《从零搭建插件环境》http://www.finedevelop.com/pages/viewpage.action?pageId=1933726
2、代码中,envPath 变量的内容修改为自己电脑上的 WEB-INF 路径
3、把“销量.xlsx”和“销售总额.xlsx”放到“WEB-INF/reportlets”目录下

参考资料

实际开发时,需要大量参考设计器源码。

demo 源码如下:

package com.fr.demo;

/**
 * Created by plough on 2017/3/20.
 */
import java.io.File;
import java.io.FileOutputStream;
import com.fr.base.FRContext;
import com.fr.data.SimpleDSColumn;
import com.fr.data.condition.CommonCondition;
import com.fr.data.core.Compare;
import com.fr.data.impl.ExcelTableData;
import com.fr.data.util.function.SumFunction;
import com.fr.general.ModuleContext;
import com.fr.dav.LocalEnv;
import com.fr.general.data.TableDataColumn;
import com.fr.report.cell.TemplateCellElement;
import com.fr.report.cell.cellattr.CellExpandAttr;
import com.fr.report.cell.cellattr.core.group.DSColumn;
import com.fr.report.cell.cellattr.core.group.SummaryGrouper;
import com.fr.report.elementcase.TemplateElementCase;
import com.fr.report.module.EngineModule;
import com.fr.main.impl.WorkBook;
import com.fr.report.worksheet.WorkSheet;
import com.fr.stable.Constants;

public class WorkBookDemo {
    public static void main(String[] args) {
        // 定义报表运行环境,才能执行报表
//        String envPath = "D:\\FineReport\\develop\\code\\build\\package\\WebReport\\WEB-INF";
        String envPath = "/Applications/FineReport/WebReport/WEB-INF";
        FRContext.setCurrentEnv(new LocalEnv(envPath));
        ModuleContext.startModule(EngineModule.class.getName());
        try {
            WorkBook workbook = new WorkBook();
            // 设置第一个 Excel 文件数据集:销量.xlsx
            ExcelTableData td1 = new ExcelTableData();
            td1.setFilePath("reportlets/销量.xlsx");
            td1.setNeedColumnName(true);
            workbook.putTableData("销量", td1);
            // 设置第二个 Excel 文件数据集:销售总额.xlsx
            ExcelTableData td2 = new ExcelTableData();
            td2.setFilePath("reportlets/销售总额.xlsx");
            td2.setNeedColumnName(true);
            workbook.putTableData("销售总额", td2);

            // 得到 report
            workbook.addReport(0, new WorkSheet());
            TemplateElementCase report = (TemplateElementCase) workbook.getReport(0);

            // 创建三个数据列
            DSColumn dscol1 = new DSColumn();
            dscol1.setDSName("销量");
            dscol1.setColumn(TableDataColumn.createColumn("地区"));

            SimpleDSColumn dscol2 = new SimpleDSColumn();  // 用 SimpleDSColumn,不要用 DSColumn
            dscol2.setDsName("销量");
            dscol2.setColumn(TableDataColumn.createColumn("销售员"));

            DSColumn dscol3 = new DSColumn();
            dscol3.setDSName("销售总额");
            dscol3.setColumn(TableDataColumn.createColumn("销售总额"));
            // 添加过滤条件:"销售总额.销售员"等于"销量.销售员"
            CommonCondition cond = new CommonCondition("销售员", -1, new Compare(0, dscol2));
            dscol3.setCondition(cond);
            // 汇总求和
            SummaryGrouper grouper = new SummaryGrouper();
            grouper.setFunction(new SumFunction());
            dscol3.setGrouper(grouper);


            // 给单元格绑定数据列
            report.setCellValue(0, 0, dscol1);
            report.setCellValue(1, 0, dscol3);

            // 获取单元格 A1,设置纵向扩展
            TemplateCellElement cellA1 = report.getTemplateCellElement(0, 0);
            CellExpandAttr attrA1 = new CellExpandAttr();
            attrA1.setDirection(Constants.TOP_TO_BOTTOM);
            cellA1.setCellExpandAttr(attrA1);


            // 保存模板
            FileOutputStream outputStream = new FileOutputStream(new File(
                    envPath + "/reportlets/workbookDemo.cpt"));
            workbook.export(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

“以书为舟,遨游尘世”,
最好的免费 kindle 电子书分享站:

You may also like...

发表回复

您的电子邮箱地址不会被公开。


*