Export "Regional Sales Comparison" to Excel with Java (Merge & Grouped Reports)
Merge & Grouped ReportsScene 0009
Sales by city listed under each region, with a subtotal row after every region and a grand total row at the end; the region-name column is vertically merged to visually group the cities of the same region 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 "Regional Sales Comparison" workbook: 1 data sheet(s); MAPPING over 3 columns (大区, 城市, 销售额), 7 formula(s), 7 style rule(s). On the Java side it only builds the data and calls exportRegionSales() 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 (87 lines, runnable as-is)
- The complete XML config (36 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 Regional Sales Comparison to Excel with Java
Output specs
- Scene no.
- 0009
- Group
- Merge & Grouped Reports
- Config lines
- 36
- Source lines
- 87
Java source · Merge0009RegionSalesCompareDemo.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 Merge0009RegionSalesCompareDemo {
private static final String XML = "jquick/biz/merge/0009_merge_region-sales-compare.xml";
/** 导出:Java 分组 + 插小计行,大区列纵向合并,求和由 XML FORMULAS 完成。 */
@Test
public void exportRegionSales() throws Exception {
// 1. 构造扁平明细(Java 不做任何金额聚合)
List<JQuickRow> flat = new ArrayList<>();
flat.add(detail("华东", "上海", 3200000));
flat.add(detail("华东", "杭州", 2100000));
flat.add(detail("华东", "南京", 1800000));
flat.add(detail("华南", "广州", 2600000));
flat.add(detail("华南", "深圳", 3400000));
flat.add(detail("华北", "北京", 4200000));
flat.add(detail("华北", "天津", 1500000));
// 2. Java 分组并插入小计 / 合计占位行(金额列留给 FORMULAS)
List<JQuickRow> rows = groupWithSubtotal(flat);
File out = new File("D:/test/excel/biz/merge/0009_merge_region-sales-compare.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("peach", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0009RegionSalesCompareService service = factory.createApi(Merge0009RegionSalesCompareService.class);
service.exportRegionSales("field", "value");
}
}
/** 按大区分组,大区切换处插入小计行,末尾追加合计行(不做金额求和)。 */
private static List<JQuickRow> groupWithSubtotal(List<JQuickRow> flat) {
List<JQuickRow> out = new ArrayList<>();
String current = null;
for (JQuickRow r : flat) {
String region = String.valueOf(r.get("a"));
if (current != null && !current.equals(region)) {
out.add(subtotalRow(current));
}
out.add(r);
current = region;
}
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 region) {
JQuickRow row = new JQuickRow();
row.put("a", region);
row.put("b", "小计");
row.put("c", null);
return row;
}
/** 构造一条城市销售明细(a=大区,b=城市,c=销售额)。 */
private static JQuickRow detail(String region, String city, double amount) {
JQuickRow row = new JQuickRow();
row.put("a", region);
row.put("b", city);
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("peach", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0009RegionSalesCompareService service = factory.createApi(Merge0009RegionSalesCompareService.class);XML config · 0009_merge_region-sales-compare.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.Merge0009RegionSalesCompareService">
<!-- 导出:区域销售对比(省 / 市维度) -->
<excel name="exportRegionSales" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="区域销售对比", HEADER=true,
MAPPING={"a":"大区","b":"城市","c":"销售额"},
FORMAT={"c":"#,##0.00"},
FORMULAS={C5:'SUM(C2:C4)',C8:'SUM(C6:C7)',C11:'SUM(C9:C10)',C12:'C5+C8+C11'},
STYLE={
ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right},
ROW 8:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right},
ROW 11:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right},
ROW 12:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right}
},
MERGE:{
RANGE A2:A4 WITH MERGE_WITH_FIRST,
RANGE A6:A7 WITH MERGE_WITH_FIRST,
RANGE A9:A10 WITH MERGE_WITH_FIRST
}
]]>
</excel>
<!-- 导入解析:读取导出的区域销售对比 -->
<excel name="importRegionSales" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="区域销售对比", HEADER=true,
MAPPING={"大区":"region","城市":"city","销售额":"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.