Skip to content

Export "Supplier Supply Ledger" to Excel with Java (Merge & Grouped Reports)

JQuick-ExcelMerge & Grouped Reports

Merge & Grouped ReportsScene 0029

Groups purchase-supply detail by supplier, computing Supply Amount = Supply Quantity × Unit Price per row, with an amount subtotal row after each supplier and a grand total row at the end; the supplier-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

Supplier Supply Ledger — Merge & Grouped Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Supplier Supply Ledger" workbook: 1 data sheet(s); MAPPING over 7 columns (供应商, 采购单号, 物料名称, 供货数量, 单价, 供货金额, 到货状态), 11 formula(s), 7 style rule(s). On the Java side it only builds the data and calls exportSupplierDelivery() 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 (99 lines, runnable as-is)
  • The complete XML config (44 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 Supplier Supply Ledger to Excel with Java

Output specs

Scene no.
0029
Group
Merge & Grouped Reports
Config lines
44
Source lines
99

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

    private static final String XML = "jquick/biz/merge/0029_merge_supplier-delivery-ledger.xml";

    /** 导出:Java 分组 + 插小计行,供应商列纵向合并,供货金额由 XML FORMULAS 完成。 */
    @Test
    public void exportSupplierDelivery() throws Exception {
        // 1. 构造扁平供货明细(Java 不做任何金额计算)
        List<JQuickRow> flat = new ArrayList<>();
        flat.add(delivery("华新电子", "PO001", "电容", 2000, 1.5, "已到货"));
        flat.add(delivery("华新电子", "PO002", "电阻", 5000, 0.2, "已到货"));
        flat.add(delivery("恒通材料", "PO003", "钢板", 100, 320, "已到货"));
        flat.add(delivery("恒通材料", "PO004", "铝材", 200, 85, "部分到货"));
        flat.add(delivery("恒通材料", "PO005", "铜管", 50, 210, "未到货"));
        flat.add(delivery("佳美包装", "PO006", "纸箱", 3000, 2.8, "已到货"));
        flat.add(delivery("佳美包装", "PO007", "泡沫", 1500, 1.2, "已到货"));

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

        File out = new File("D:/test/excel/biz/merge/0029_merge_supplier-delivery-ledger.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("sage", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Merge0029SupplierDeliveryLedgerService service = factory.createApi(Merge0029SupplierDeliveryLedgerService.class);
            service.exportSupplierDelivery("field", "value");
        }

    }

    /** 按供应商分组,供应商切换处插入小计行,末尾追加合计行(不做金额计算)。 */
    private static List<JQuickRow> groupWithSubtotal(List<JQuickRow> flat) {
        List<JQuickRow> out = new ArrayList<>();
        String current = null;
        for (JQuickRow r : flat) {
            String supplier = String.valueOf(r.get("a"));
            if (current != null && !current.equals(supplier)) {
                out.add(subtotalRow(current));
            }
            out.add(r);
            current = supplier;
        }
        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);
        total.put("g", null);
        out.add(total);
        return out;
    }

    /** 小计占位行:只写供应商与小计文字,供货金额留空由 FORMULAS 计算。 */
    private static JQuickRow subtotalRow(String supplier) {
        JQuickRow row = new JQuickRow();
        row.put("a", supplier);
        row.put("b", "小计");
        row.put("c", null);
        row.put("d", null);
        row.put("e", null);
        row.put("f", null);
        row.put("g", null);
        return row;
    }

    /** 构造一条供货明细(a=供应商,b=采购单号,c=物料名称,d=供货数量,e=单价,g=到货状态)。 */
    private static JQuickRow delivery(String supplier, String poNo, String material, int qty, double price, String status) {
        JQuickRow row = new JQuickRow();
        row.put("a", supplier);
        row.put("b", poNo);
        row.put("c", material);
        row.put("d", qty);
        row.put("e", price);
        row.put("f", null);
        row.put("g", status);
        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("sage", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0029SupplierDeliveryLedgerService service = factory.createApi(Merge0029SupplierDeliveryLedgerService.class);

XML config · 0029_merge_supplier-delivery-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.Merge0029SupplierDeliveryLedgerService">

    <!-- 导出:供应商供货台账 -->
    <excel name="exportSupplierDelivery" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="供应商供货台账", HEADER=true,
                MAPPING={"a":"供应商","b":"采购单号","c":"物料名称","d":"供货数量","e":"单价","f":"供货金额","g":"到货状态"},
                FORMAT={"d":"#,##0","e":"#,##0.00","f":"#,##0.00"},
                FORMULAS={
                    F2:'D2*E2',F3:'D3*E3',
                    F4:'SUM(F2:F3)',
                    F5:'D5*E5',F6:'D6*E6',F7:'D7*E7',
                    F8:'SUM(F5:F7)',
                    F9:'D9*E9',F10:'D10*E10',
                    F11:'SUM(F9:F10)',
                    F12:'F4+F8+F11'
                },
                STYLE={
                    ROW 4:{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 11:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    ROW 12:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}
                },
                MERGE:{
                    RANGE A2:A3 WITH MERGE_WITH_FIRST,
                    RANGE A5:A7 WITH MERGE_WITH_FIRST,
                    RANGE A9:A10 WITH MERGE_WITH_FIRST
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的供应商供货台账 -->
    <excel name="importSupplierDelivery" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="供应商供货台账", HEADER=true,
                MAPPING={"供应商":"supplier","采购单号":"poNo","物料名称":"materialName","供货数量":"supplyQty","单价":"unitPrice","供货金额":"supplyAmount","到货状态":"arrivalStatus"}
        ]]>
    </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.