Skip to content

Export "Department Budget Execution Ledger" to Excel with Java (Merge & Grouped Reports)

JQuick-ExcelMerge & Grouped Reports

Merge & Grouped ReportsScene 0039

Groups the annual budget and actual spending of each expense account by department, computing Budget Balance = Budget - Actual and Execution Rate = Actual / Budget per row, with a subtotal row after each department and a company-wide total in the last row; the department-name column is vertically merged into one block, and the execution rate of over-budget accounts is marked in red. 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 Budget Execution Ledger — Merge & Grouped Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Department Budget Execution Ledger" workbook: 1 data sheet(s); MAPPING over 6 columns (部门, 费用科目, 年度预算, 实际支出, 预算余额, 执行率), 28 formula(s), 5 style rule(s). On the Java side it only builds the data and calls exportDepartmentBudget() 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 (95 lines, runnable as-is)
  • The complete XML config (46 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 Budget Execution Ledger to Excel with Java

Output specs

Scene no.
0039
Group
Merge & Grouped Reports
Config lines
46
Source lines
95

Java source · Merge0039DepartmentBudgetLedgerDemo.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 Merge0039DepartmentBudgetLedgerDemo {

    private static final String XML = "jquick/biz/merge/0039_merge_department-budget-ledger.xml";

    /** 导出:Java 分组 + 插小计行,部门列纵向合并,余额 / 执行率由 XML FORMULAS 完成。 */
    @Test
    public void exportDepartmentBudget() throws Exception {
        // 1. 构造扁平预算明细(Java 不做任何金额计算)
        List<JQuickRow> flat = new ArrayList<>();
        flat.add(line("研发部", "人力成本", 1200000, 1150000));
        flat.add(line("研发部", "设备采购", 500000, 520000));
        flat.add(line("研发部", "云服务", 200000, 150000));
        flat.add(line("市场部", "广告投放", 800000, 720000));
        flat.add(line("市场部", "展会费用", 300000, 340000));
        flat.add(line("市场部", "差旅费", 150000, 120000));

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

        File out = new File("D:/test/excel/biz/merge/0039_merge_department-budget-ledger.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("amethyst", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Merge0039DepartmentBudgetLedgerService service = factory.createApi(Merge0039DepartmentBudgetLedgerService.class);
            service.exportDepartmentBudget("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);
        total.put("d", null);
        total.put("e", null);
        total.put("f", null);
        out.add(total);
        return out;
    }

    /** 小计占位行:只写部门与小计文字,余额、执行率留空由 FORMULAS 计算。 */
    private static JQuickRow subtotalRow(String dept) {
        JQuickRow row = new JQuickRow();
        row.put("a", dept);
        row.put("b", "小计");
        row.put("c", null);
        row.put("d", null);
        row.put("e", null);
        row.put("f", null);
        return row;
    }

    /** 构造一条预算明细(a=部门,b=费用科目,c=年度预算,d=实际支出,e/f 留空)。 */
    private static JQuickRow line(String dept, String subject, double budget, double actual) {
        JQuickRow row = new JQuickRow();
        row.put("a", dept);
        row.put("b", subject);
        row.put("c", budget);
        row.put("d", actual);
        row.put("e", null);
        row.put("f", null);
        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("amethyst", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0039DepartmentBudgetLedgerService service = factory.createApi(Merge0039DepartmentBudgetLedgerService.class);

XML config · 0039_merge_department-budget-ledger.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.Merge0039DepartmentBudgetLedgerService">

    <!-- 导出:部门预算执行台账 -->
    <excel name="exportDepartmentBudget" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="部门预算执行台账", HEADER=true,
                MAPPING={"a":"部门","b":"费用科目","c":"年度预算","d":"实际支出","e":"预算余额","f":"执行率"},
                FORMAT={"c":"#,##0.00","d":"#,##0.00","e":"#,##0.00","f":"0.00%"},
                FORMULAS={
                    E2:'C2-D2',F2:'D2/C2',
                    E3:'C3-D3',F3:'D3/C3',
                    E4:'C4-D4',F4:'D4/C4',
                    C5:'SUM(C2:C4)',D5:'SUM(D2:D4)',E5:'C5-D5',F5:'D5/C5',
                    E6:'C6-D6',F6:'D6/C6',
                    E7:'C7-D7',F7:'D7/C7',
                    E8:'C8-D8',F8:'D8/C8',
                    C9:'SUM(C6:C8)',D9:'SUM(D6:D8)',E9:'C9-D9',F9:'D9/C9',
                    C10:'C5+C9',D10:'D5+D9',E10:'E5+E9',F10:'D10/C10'
                },
                STYLE={
                    ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    ROW 9:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    ROW 10:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    F3:{color:red,bold:true},
                    F7:{color:red,bold:true}
                },
                MERGE:{
                    RANGE A2:A4 WITH MERGE_WITH_FIRST,
                    RANGE A6:A8 WITH MERGE_WITH_FIRST
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的部门预算执行台账 -->
    <excel name="importDepartmentBudget" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="部门预算执行台账", HEADER=true,
                MAPPING={"部门":"dept","费用科目":"subject","年度预算":"budget","实际支出":"actual","预算余额":"balance","执行率":"executionRate"}
        ]]>
    </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.