Skip to content

Export "Expense Reimbursement Detail" to Excel with Java (Merge & Grouped Reports)

JQuick-ExcelMerge & Grouped Reports

Merge & Grouped ReportsScene 0022

Groups employee expense reimbursement detail by department, with an amount subtotal row after each department and a grand total row at the end; the department-name column is vertically merged into one block. 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

Expense Reimbursement Detail — Merge & Grouped Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Expense Reimbursement Detail" workbook: 1 data sheet(s); MAPPING over 5 columns (部门, 报销人, 费用类型, 申请日期, 金额), 7 formula(s), 7 style rule(s). On the Java side it only builds the data and calls exportExpenseReimbursementDetail() 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 (104 lines, runnable as-is)
  • The complete XML config (41 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 Expense Reimbursement Detail to Excel with Java

Output specs

Scene no.
0022
Group
Merge & Grouped Reports
Config lines
41
Source lines
104

Java source · Merge0022ExpenseReimbursementDetailDemo.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.Calendar;
import java.util.Date;
import java.util.List;

public class Merge0022ExpenseReimbursementDetailDemo {

    private static final String XML = "jquick/biz/merge/0022_merge_expense-reimbursement-detail.xml";

    /** 导出:Java 分组 + 插小计行,部门列纵向合并,金额统计由 XML FORMULAS 完成。 */
    @Test
    public void exportExpenseReimbursementDetail() throws Exception {
        // 1. 构造扁平报销明细(Java 不做任何金额汇总)
        List<JQuickRow> flat = new ArrayList<>();
        flat.add(detail("研发部", "张伟", "差旅费", date(2026, 1, 8), 3200));
        flat.add(detail("研发部", "李娜", "办公用品", date(2026, 1, 12), 850));
        flat.add(detail("研发部", "王强", "培训费", date(2026, 1, 20), 5000));
        flat.add(detail("市场部", "赵敏", "招待费", date(2026, 1, 15), 2800));
        flat.add(detail("市场部", "陈刚", "差旅费", date(2026, 1, 22), 4500));
        flat.add(detail("财务部", "孙丽", "办公用品", date(2026, 1, 9), 600));
        flat.add(detail("财务部", "周杰", "会议费", date(2026, 1, 18), 1500));
        flat.add(detail("财务部", "吴敏", "差旅费", date(2026, 1, 26), 2200));

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

        File out = new File("D:/test/excel/biz/merge/0022_merge_expense-reimbursement-detail.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("sunsetOrange", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Merge0022ExpenseReimbursementDetailService service = factory.createApi(Merge0022ExpenseReimbursementDetailService.class);
            service.exportExpenseReimbursementDetail("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);
        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);
        return row;
    }

    /** 构造一条报销明细(a=部门,b=报销人,c=费用类型,d=申请日期,e=金额)。 */
    private static JQuickRow detail(String dept, String applicant, String type, Date applyDate, double amount) {
        JQuickRow row = new JQuickRow();
        row.put("a", dept);
        row.put("b", applicant);
        row.put("c", type);
        row.put("d", applyDate);
        row.put("e", amount);
        return row;
    }

    /** 构造一个「年-月-日」日期(月份从 1 开始)。 */
    private static Date date(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(year, month - 1, day);
        return c.getTime();
    }
}

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("sunsetOrange", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0022ExpenseReimbursementDetailService service = factory.createApi(Merge0022ExpenseReimbursementDetailService.class);

XML config · 0022_merge_expense-reimbursement-detail.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.Merge0022ExpenseReimbursementDetailService">

    <!-- 导出:费用报销明细表 -->
    <excel name="exportExpenseReimbursementDetail" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="费用报销明细表", HEADER=true,
                MAPPING={"a":"部门","b":"报销人","c":"费用类型","d":"申请日期","e":"金额"},
                FORMAT={"d":"yyyy-MM-dd","e":"#,##0.00"},
                FORMULAS={
                    E5:'SUM(E2:E4)',
                    E8:'SUM(E6:E7)',
                    E12:'SUM(E9:E11)',
                    E13:'E5+E8+E12'
                },
                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 12:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    ROW 13:{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,
                    RANGE A9:A11 WITH MERGE_WITH_FIRST
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的费用报销明细表 -->
    <excel name="importExpenseReimbursementDetail" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="费用报销明细表", HEADER=true,
                MAPPING={"部门":"dept","报销人":"applicant","费用类型":"expenseType","申请日期":"applyDate","金额":"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.