Skip to content

Export "Department Expense Summary" to Excel with Java (Merge & Grouped Reports)

JQuick-ExcelMerge & Grouped Reports

Merge & Grouped ReportsScene 0003

Expense detail for each department, with a subtotal row after every department and a grand total row at the end. Java only groups and pre-processes the rows; all statistics and layout are declared in XML.

Download the Excel fileView raw XML configBack to all demos

Department Expense Summary — Merge & Grouped Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Department Expense Summary" workbook: 1 data sheet(s); MAPPING over 3 columns (部门, 费用类型, 金额), 7 formula(s), 4 style rule(s). On the Java side it only builds the data and calls exportDepartmentExpense() once — everything else is driven by the config.

It belongs to the "Merge & Grouped Reports" group: MERGE group blocks with SUBTOTAL/TOTAL rows: each group column merges into one vertical block, every group closes with a subtotal and the report ends with a grand total.

What you can copy from this page

  • The complete Java test class (89 lines, runnable as-is)
  • The complete XML config (31 lines: mapping, formulas, styles, charts and every other rule)
  • The exported xlsx file (real output, comparable with the preview above)

Related searches: Java export Excel merged cells, Java Excel group subtotal and total rows, Apache POI merged cells export, Java grouped report export, export Department Expense Summary to Excel with Java

Output specs

Scene no.
0003
Group
Merge & Grouped Reports
Config lines
31
Source lines
89

Java source · Merge0003DepartmentExpenseStatDemo.java

This is the complete test class that produced the Excel file above — copy it and run as-is. Java only builds the data rows; all statistics and styling live in the XML config.

package com.github.paohaijiao.biz.merge;

import com.github.paohaijiao.statement.JQuickRow;
import com.github.paohaijiao.xml.ex.JQuickExcelExportXmlParseFactory;
import com.github.paohaijiao.xml.factory.JQuickFactory;
import com.github.paohaijiao.xml.factory.JQuickXmlFactory;
import com.github.paohaijiao.xml.handler.JQuickParseHandler;
import org.junit.Test;

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

public class Merge0003DepartmentExpenseStatDemo {

    private static final String XML = "jquick/biz/merge/0003_merge_department-expense-stat.xml";

    /** 导出:Java 分组 + 插小计行,金额求和由 XML FORMULAS 完成。 */
    @Test
    public void exportDepartmentExpense() throws Exception {
        // 1. 构造扁平明细(Java 不做任何金额聚合)
        List<JQuickRow> flat = new ArrayList<>();
        flat.add(detail("研发部", "差旅费", 12000));
        flat.add(detail("研发部", "办公费", 8000));
        flat.add(detail("研发部", "招待费", 5000));
        flat.add(detail("市场部", "广告费", 30000));
        flat.add(detail("市场部", "差旅费", 15000));
        flat.add(detail("市场部", "招待费", 10000));
        flat.add(detail("财务部", "办公费", 6000));
        flat.add(detail("财务部", "审计费", 20000));
        flat.add(detail("财务部", "培训费", 4000));

        // 2. Java 分组并插入小计 / 合计占位行(金额列留给 FORMULAS)
        List<JQuickRow> rows = groupWithSubtotal(flat);

        File out = new File("D:/test/excel/biz/merge/0003_merge_department-expense-stat.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("mustard", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Merge0003DepartmentExpenseStatService service = factory.createApi(Merge0003DepartmentExpenseStatService.class);
            service.exportDepartmentExpense("field", "value");
        }

    }

    /** 按部门分组,部门切换处插入小计行,末尾追加合计行(不做金额求和)。 */
    private static List<JQuickRow> groupWithSubtotal(List<JQuickRow> flat) {
        List<JQuickRow> out = new ArrayList<>();
        String current = null;
        for (JQuickRow r : flat) {
            String dept = String.valueOf(r.get("a"));
            if (current != null && !current.equals(dept)) {
                out.add(subtotalRow(current));
            }
            out.add(r);
            current = dept;
        }
        if (current != null) {
            out.add(subtotalRow(current));
        }
        JQuickRow total = new JQuickRow();
        total.put("a", "合计");
        total.put("b", null);
        total.put("c", null);
        out.add(total);
        return out;
    }

    /** 小计占位行:只写部门与小计文字,金额列以空值占位(值由 XML FORMULAS 计算)。 */
    private static JQuickRow subtotalRow(String dept) {
        JQuickRow row = new JQuickRow();
        row.put("a", dept);
        row.put("b", "小计");
        row.put("c", null);
        return row;
    }

    /** 构造一条费用明细(a=部门,b=费用类型,c=金额)。 */
    private static JQuickRow detail(String dept, String item, double amount) {
        JQuickRow row = new JQuickRow();
        row.put("a", dept);
        row.put("b", item);
        row.put("c", amount);
        return row;
    }
}

Minimal invocation (4 lines)

The core is these four lines: hand the rows to the parser, the parser to the factory, get a typed Service and call it. Copy them straight into your project.

// rows 由业务代码构造(List<JQuickRow>),os 为输出流,见下方完整源码
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("mustard", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0003DepartmentExpenseStatService service = factory.createApi(Merge0003DepartmentExpenseStatService.class);

XML config · 0003_merge_department-expense-stat.xml

Formats (FORMAT), formulas (FORMULAS) and styles (STYLE) are all declared in this XML, fully decoupled from the Java code. The full config is below — copy it as-is.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE excels PUBLIC "-//PAOHAIJIAO//DTD API EXCEL 1.0//EN"
        "classpath:paohaijiao/dtd/Jquick-excel.dtd">
<excels namespace="com.github.paohaijiao.biz.merge.Merge0003DepartmentExpenseStatService">

    <!-- 导出:部门费用统计表 -->
    <excel name="exportDepartmentExpense" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="部门费用统计", HEADER=true,
                MAPPING={"a":"部门","b":"费用类型","c":"金额"},
                FORMAT={"c":"#,##0.00"},
                FORMULAS={C5:'SUM(C2:C4)',C9:'SUM(C6:C8)',C13:'SUM(C10:C12)',C14:'C5+C9+C13'},
                STYLE={
                    ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right},
                    ROW 9:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right},
                    ROW 13:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right},
                    ROW 14:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的 Excel -->
    <excel name="importDepartmentExpense" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="部门费用统计", HEADER=true,
                MAPPING={"部门":"dept","费用类型":"item","金额":"amount"}
        ]]>
    </excel>

</excels>

More demos in this group

The Excel file, preview image, XML config and source code on this page were all produced by a real JQuick-Excel run and are free to copy into your own project.