Skip to content

Export "Contract Fulfillment Ledger" to Excel with Java (Merge & Grouped Reports)

JQuick-ExcelMerge & Grouped Reports

Merge & Grouped ReportsScene 0068

Groups each contract's amount and fulfilled amount by contract type, computing Fulfillment Rate = Fulfilled / Contract Amount per row, with a subtotal row after each contract type and a grand total for all contracts in the last row; the contract-type 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

Contract Fulfillment Ledger — Merge & Grouped Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Contract Fulfillment Ledger" workbook: 1 data sheet(s); MAPPING over 6 columns (合同类型, 合同编号, 签约对方, 合同金额, 已履行金额, 履行率), 18 formula(s), 5 style rule(s). On the Java side it only builds the data and calls exportContractPerformanceLedger() 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 (40 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 Contract Fulfillment Ledger to Excel with Java

Output specs

Scene no.
0068
Group
Merge & Grouped Reports
Config lines
40
Source lines
95

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

    private static final String XML = "jquick/biz/merge/0068_merge_contract-performance-ledger.xml";

    /** 导出:Java 分组 + 插小计行,合同类型列纵向合并,履行率与小计由 XML FORMULAS 完成。 */
    @Test
    public void exportContractPerformanceLedger() throws Exception {
        // 1. 构造扁平合同明细(Java 不做任何履行率计算)
        List<JQuickRow> flat = new ArrayList<>();
        flat.add(line("采购合同", "HT2601", "华新电子", 500000.00, 500000.00));
        flat.add(line("采购合同", "HT2602", "恒通材料", 300000.00, 180000.00));
        flat.add(line("采购合同", "HT2603", "佳美包装", 200000.00, 200000.00));
        flat.add(line("销售合同", "HT2604", "中远集团", 800000.00, 640000.00));
        flat.add(line("销售合同", "HT2605", "城建集团", 600000.00, 600000.00));

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

        File out = new File("D:/test/excel/biz/merge/0068_merge_contract-performance-ledger.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("tropicalTeal", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Merge0068ContractPerformanceLedgerService service = factory.createApi(Merge0068ContractPerformanceLedgerService.class);
            service.exportContractPerformanceLedger("field", "value");
        }

    }

    /** 按合同类型分组,类型切换处插入小计行,末尾追加合计行(不做履行率计算)。 */
    private static List<JQuickRow> groupWithSubtotal(List<JQuickRow> flat) {
        List<JQuickRow> out = new ArrayList<>();
        String current = null;
        for (JQuickRow r : flat) {
            String type = String.valueOf(r.get("a"));
            if (current != null && !current.equals(type)) {
                out.add(subtotalRow(current));
            }
            out.add(r);
            current = type;
        }
        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 contractType) {
        JQuickRow row = new JQuickRow();
        row.put("a", contractType);
        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 留空由 FORMULAS 计算)。 */
    private static JQuickRow line(String contractType, String contractNo, String counterparty,
                                  double contractAmount, double performedAmount) {
        JQuickRow row = new JQuickRow();
        row.put("a", contractType);
        row.put("b", contractNo);
        row.put("c", counterparty);
        row.put("d", contractAmount);
        row.put("e", performedAmount);
        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("tropicalTeal", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0068ContractPerformanceLedgerService service = factory.createApi(Merge0068ContractPerformanceLedgerService.class);

XML config · 0068_merge_contract-performance-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.Merge0068ContractPerformanceLedgerService">

    <!-- 导出:合同履行台账 -->
    <excel name="exportContractPerformanceLedger" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="合同履行台账", HEADER=true,
                MAPPING={"a":"合同类型","b":"合同编号","c":"签约对方","d":"合同金额","e":"已履行金额","f":"履行率"},
                FORMAT={"d":"#,##0.00","e":"#,##0.00","f":"0.00%"},
                FORMULAS={
                    F2:'E2/D2',F3:'E3/D3',F4:'E4/D4',
                    D5:'SUM(D2:D4)',E5:'SUM(E2:E4)',F5:'E5/D5',
                    F6:'E6/D6',F7:'E7/D7',
                    D8:'SUM(D6:D7)',E8:'SUM(E6:E7)',F8:'E8/D8',
                    D9:'D5+D8',E9:'E5+E8',F9:'E9/D9'
                },
                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="importContractPerformanceLedger" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="合同履行台账", HEADER=true,
                MAPPING={"合同类型":"contractType","合同编号":"contractNo","签约对方":"counterparty","合同金额":"contractAmount","已履行金额":"performedAmount","履行率":"performanceRate"}
        ]]>
    </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.