Skip to content

Export "Inventory Count Variance" to Excel with Java (Merge & Grouped Reports)

JQuick-ExcelMerge & Grouped Reports

Merge & Grouped ReportsScene 0027

Groups count results by warehouse, computing Variance Quantity = Physical Count - Book Quantity and Variance Amount = Variance Quantity × Unit Price per row, with a variance subtotal row after each warehouse and a grand total row at the end; the warehouse-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

Inventory Count Variance — Merge & Grouped Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Inventory Count Variance" workbook: 1 data sheet(s); MAPPING over 8 columns (仓库, 物料编码, 物料名称, 账面数量, 实盘数量, 单价, 差异数量, 差异金额), 16 formula(s), 5 style rule(s). On the Java side it only builds the data and calls exportInventoryCountVariance() 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 (100 lines, runnable as-is)
  • The complete XML config (43 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 Inventory Count Variance to Excel with Java

Output specs

Scene no.
0027
Group
Merge & Grouped Reports
Config lines
43
Source lines
100

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

    private static final String XML = "jquick/biz/merge/0027_merge_inventory-count-variance.xml";

    /** 导出:Java 分组 + 插小计行,仓库列纵向合并,差异数量 / 金额由 XML FORMULAS 完成。 */
    @Test
    public void exportInventoryCountVariance() throws Exception {
        // 1. 构造扁平盘点明细(Java 不做任何差异计算)
        List<JQuickRow> flat = new ArrayList<>();
        flat.add(item("一号仓", "WL001", "螺丝 M6", 5000, 4950, 0.5));
        flat.add(item("一号仓", "WL002", "垫片", 3000, 3000, 0.2));
        flat.add(item("一号仓", "WL003", "轴承", 200, 185, 80));
        flat.add(item("二号仓", "WL004", "电机", 50, 52, 500));
        flat.add(item("二号仓", "WL005", "皮带", 300, 288, 25));

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

        File out = new File("D:/test/excel/biz/merge/0027_merge_inventory-count-variance.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("turquoise", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Merge0027InventoryCountVarianceService service = factory.createApi(Merge0027InventoryCountVarianceService.class);
            service.exportInventoryCountVariance("field", "value");
        }

    }

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

    /** 小计占位行:只写仓库与小计文字,差异数量 / 金额留空由 FORMULAS 计算。 */
    private static JQuickRow subtotalRow(String warehouse) {
        JQuickRow row = new JQuickRow();
        row.put("a", warehouse);
        row.put("b", "小计");
        row.put("c", null);
        row.put("d", null);
        row.put("e", null);
        row.put("f", null);
        row.put("g", null);
        row.put("h", null);
        return row;
    }

    /** 构造一条盘点明细(a=仓库,b=物料编码,c=物料名称,d=账面,e=实盘,f=单价)。 */
    private static JQuickRow item(String warehouse, String code, String name, int bookQty, int actualQty, double price) {
        JQuickRow row = new JQuickRow();
        row.put("a", warehouse);
        row.put("b", code);
        row.put("c", name);
        row.put("d", bookQty);
        row.put("e", actualQty);
        row.put("f", price);
        row.put("g", null);
        row.put("h", 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("turquoise", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Merge0027InventoryCountVarianceService service = factory.createApi(Merge0027InventoryCountVarianceService.class);

XML config · 0027_merge_inventory-count-variance.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.Merge0027InventoryCountVarianceService">

    <!-- 导出:库存盘点差异表 -->
    <excel name="exportInventoryCountVariance" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="库存盘点差异表", HEADER=true,
                MAPPING={"a":"仓库","b":"物料编码","c":"物料名称","d":"账面数量","e":"实盘数量","f":"单价","g":"差异数量","h":"差异金额"},
                FORMAT={"d":"#,##0","e":"#,##0","f":"#,##0.00","g":"#,##0","h":"#,##0.00"},
                FORMULAS={
                    G2:'E2-D2',H2:'G2*F2',
                    G3:'E3-D3',H3:'G3*F3',
                    G4:'E4-D4',H4:'G4*F4',
                    G5:'SUM(G2:G4)',H5:'SUM(H2:H4)',
                    G6:'E6-D6',H6:'G6*F6',
                    G7:'E7-D7',H7:'G7*F7',
                    G8:'SUM(G6:G7)',H8:'SUM(H6:H7)',
                    G9:'G5+G8',H9:'H5+H8'
                },
                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="importInventoryCountVariance" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="库存盘点差异表", HEADER=true,
                MAPPING={"仓库":"warehouse","物料编码":"materialCode","物料名称":"materialName","账面数量":"bookQty","实盘数量":"actualQty","单价":"unitPrice","差异数量":"diffQty","差异金额":"diffAmount"}
        ]]>
    </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.