Export "Project Cost Accumulation Ledger" to Excel with Java (Merge & Grouped Reports)
Merge & Grouped ReportsScene 0051
Groups the incurred amounts of each cost account by project, with a cost subtotal row after each project and a grand total for all projects in the last row; the project-name column is vertically merged into one block. Java only groups and pre-processes the rows; all statistics and layout are declared in XML.

About this demo
This demo exports the "Project Cost Accumulation Ledger" workbook: 1 data sheet(s); MAPPING over 4 columns (项目, 成本科目, 发生月份, 成本金额), 5 formula(s), 5 style rule(s). On the Java side it only builds the data and calls exportProjectCostLedger() 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 (88 lines, runnable as-is)
- The complete XML config (38 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 Project Cost Accumulation Ledger to Excel with Java
Output specs
- Scene no.
- 0051
- Group
- Merge & Grouped Reports
- Config lines
- 38
- Source lines
- 88
Java source · Merge0051ProjectCostLedgerDemo.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 Merge0051ProjectCostLedgerDemo {
private static final String XML = "jquick/biz/merge/0051_merge_project-cost-ledger.xml";
/** 导出:Java 分组 + 插小计行,项目列纵向合并,小计与合计由 XML FORMULAS 完成。 */
@Test
public void exportProjectCostLedger() throws Exception {
// 1. 构造扁平成本明细(Java 不做任何金额计算)
List<JQuickRow> flat = new ArrayList<>();
flat.add(line("智能仓储系统", "设备采购", "2026-01", 300000.00));
flat.add(line("智能仓储系统", "施工安装", "2026-02", 120000.00));
flat.add(line("智能仓储系统", "软件授权", "2026-03", 80000.00));
flat.add(line("产线改造", "设备采购", "2026-02", 260000.00));
flat.add(line("产线改造", "技术服务", "2026-03", 90000.00));
// 2. Java 分组并插入小计 / 合计占位行(成本金额留给 FORMULAS)
List<JQuickRow> rows = groupWithSubtotal(flat);
File out = new File("D:/test/excel/biz/merge/0051_merge_project-cost-ledger.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("bronze", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0051ProjectCostLedgerService service = factory.createApi(Merge0051ProjectCostLedgerService.class);
service.exportProjectCostLedger("field", "value");
}
}
/** 按项目分组,项目切换处插入小计行,末尾追加合计行(不做金额计算)。 */
private static List<JQuickRow> groupWithSubtotal(List<JQuickRow> flat) {
List<JQuickRow> out = new ArrayList<>();
String current = null;
for (JQuickRow r : flat) {
String project = String.valueOf(r.get("a"));
if (current != null && !current.equals(project)) {
out.add(subtotalRow(current));
}
out.add(r);
current = project;
}
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);
out.add(total);
return out;
}
/** 小计占位行:只写项目与小计文字,成本金额留空由 FORMULAS 计算。 */
private static JQuickRow subtotalRow(String project) {
JQuickRow row = new JQuickRow();
row.put("a", project);
row.put("b", "小计");
row.put("c", null);
row.put("d", null);
return row;
}
/** 构造一条成本明细(a=项目,b=科目,c=月份,d=成本金额,作为分段 SUM 的数据源)。 */
private static JQuickRow line(String project, String subject, String month, double cost) {
JQuickRow row = new JQuickRow();
row.put("a", project);
row.put("b", subject);
row.put("c", month);
row.put("d", cost);
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("bronze", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0051ProjectCostLedgerService service = factory.createApi(Merge0051ProjectCostLedgerService.class);XML config · 0051_merge_project-cost-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.Merge0051ProjectCostLedgerService">
<!-- 导出:项目成本归集台账 -->
<excel name="exportProjectCostLedger" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="项目成本归集台账", HEADER=true,
MAPPING={"a":"项目","b":"成本科目","c":"发生月份","d":"成本金额"},
FORMAT={"d":"#,##0.00"},
FORMULAS={
D5:'SUM(D2:D4)',
D8:'SUM(D6:D7)',
D9:'D5+D8'
},
STYLE={
ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
ROW 8:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
ROW 9:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}
},
MERGE:{
RANGE A2:A4 WITH MERGE_WITH_FIRST,
RANGE A6:A7 WITH MERGE_WITH_FIRST
}
]]>
</excel>
<!-- 导入解析:读取导出的项目成本归集台账 -->
<excel name="importProjectCostLedger" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="项目成本归集台账", HEADER=true,
MAPPING={"项目":"project","成本科目":"subject","发生月份":"month","成本金额":"cost"}
]]>
</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.