【问题标题】:How to use DynamicJasper to create an excel spreadsheet with formulas for subtotals of columns?如何使用 DynamicJasper 创建包含列小计公式的 Excel 电子表格?
【发布时间】:2010-09-16 18:24:35
【问题描述】:

我想使用 DynamicJasper 生成一个 Excel 电子表格,该电子表格使用公式计算列的小计。例如:

Employee   Department        Expenses
----------------------------------------
Alice      Sales             $600.00
Bob        IT                $400.00
Charlie    IT                $450.00

           Sales subtotal    $600.00 
           IT subtotal       $850.00

我希望以这样的方式生成底部两行,使数字是 Excel 公式的结果,这样如果编辑小时数,小计就会改变。

我目前遇到了各种困难。如果我将Expenses 列设置为double 类型,那么DynamicJasper 将插入$ 并使其成为字符串。 (因此添加变得不可能。)但是一个更基本的问题是如何将一个单元格定义为它上面的单元格的小计。

如果有人能指出一个简单汇总一列的示例,我会很高兴,只要它使用 excel 公式来完成它。

【问题讨论】:

    标签: java excel jasper-reports dynamic-jasper


    【解决方案1】:

    它会做这样的事情,这没有经过测试或任何事情:

    GroupBuilder gb1 = new GroupBuilder("column_name");
    
    gb1.addFooterVariable(frb.getColumn("column_name_with_amounts"),CALCULATION.EMPTY,style_lala,getBigDecimalFormatter());
    

    getBigDecimalFormatter 是:

    private DJValueFormatter getBigDecimalFormatter(f) {
        return new DJValueFormatter(){
    
            public Object evaluate(Object value, Map fields, Map variables, Map parameters) {
    
                return "here is excel code for calculating like sum=SUM(A1:A15)";
            }
    
            public String getClassName() {
                return String.class.getName();
            }};
    }
    

    仅此而已,但请注意:您必须完全控制要从中创建报告的数据。例如,在getBigDecimalFormatter,您必须知道您正在计算列中的哪些行。通常我有 2d Object 数组来存储数据,我可以从 Map 字段中确定我需要的一切。

    在我看来是相当多的工作,但我对 dynamicJasper 不是很熟悉,现在使用了一段时间......也许对于 xls 格式你甚至不需要getBigDecimalFormatter,只需b1.addFooterVariable() .

    但另一方面,如果您只是创建简单的数据表,然后用户在 Excel 中使用一个按钮创建总和,这无论如何都不是那么工作:)

    保重,NB

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多