Export "Grouped Sales Aggregation" to Excel with Java (Merge & Grouped Reports)
Merge & Grouped ReportsScene 0104
Lists each store's Q1-Q4 sales grouped by region, with the region-name column vertically merged into one block to visually group the stores of the same region. Java only groups and pre-processes the rows; all statistics and layout are declared in XML.

About this demo
This demo exports the "Grouped Sales Aggregation" workbook: 1 data sheet(s); MAPPING over 6 columns (大区, 门店, 一季度, 二季度, 三季度, 四季度), 4 style rule(s). On the Java side it builds 6 data rows and calls exportGroupSalesAggregate() 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 (53 lines, runnable as-is)
- The complete XML config (32 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 Grouped Sales Aggregation to Excel with Java
Output specs
- Scene no.
- 0104
- Group
- Merge & Grouped Reports
- Config lines
- 32
- Source lines
- 53
Java source · Merge0104GroupSalesAggregateDemo.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 Merge0104GroupSalesAggregateDemo {
private static final String XML = "jquick/biz/merge/0104_merge_group-sales-aggregate.xml";
/** 导出:按大区分组建表,大区列纵向合并,回读校验合并区块与表头样式。 */
@Test
public void exportGroupSalesAggregate() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
rows.add(line("华东", "上海店", 400, 205, 210, 190));
rows.add(line("华东", "杭州店", 250, 230, 200, 260));
rows.add(line("华南", "广州店", 300, 260, 290, 320));
rows.add(line("华南", "深圳店", 280, 260, 270, 250));
rows.add(line("华北", "北京店", 400, 360, 380, 410));
rows.add(line("华北", "天津店", 360, 340, 350, 370));
File out = new File("D:/test/excel/biz/merge/0104_merge_group-sales-aggregate.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("terracotta", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0104GroupSalesAggregateService service = factory.createApi(Merge0104GroupSalesAggregateService.class);
service.exportGroupSalesAggregate("field", "value");
}
}
/** 构造一条门店季度明细(a=大区,b=门店,c..f=一至四季度)。 */
private static JQuickRow line(String region, String store, int q1, int q2, int q3, int q4) {
JQuickRow row = new JQuickRow();
row.put("a", region);
row.put("b", store);
row.put("c", q1);
row.put("d", q2);
row.put("e", q3);
row.put("f", q4);
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("terracotta", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0104GroupSalesAggregateService service = factory.createApi(Merge0104GroupSalesAggregateService.class);XML config · 0104_merge_group-sales-aggregate.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.Merge0104GroupSalesAggregateService">
<!-- 导出:分组销售聚合(大区 / 门店 / 四个季度) -->
<excel name="exportGroupSalesAggregate" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="分组销售聚合", HEADER=true,
MAPPING={"a":"大区","b":"门店","c":"一季度","d":"二季度","e":"三季度","f":"四季度"},
FORMAT={"c":"#,##0","d":"#,##0","e":"#,##0","f":"#,##0"},
STYLE={
ROW 1:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:center,verticalAlignment:center}
},
MERGE:{
RANGE A2:A3 WITH MERGE_WITH_FIRST,
RANGE A4:A5 WITH MERGE_WITH_FIRST,
RANGE A6:A7 WITH MERGE_WITH_FIRST
}
]]>
</excel>
<!-- 导入解析:读取导出的分组销售聚合 -->
<excel name="importGroupSalesAggregate" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="分组销售聚合", HEADER=true,
MAPPING={"大区":"region","门店":"store","一季度":"q1","二季度":"q2","三季度":"q3","四季度":"q4"}
]]>
</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.