Skip to content

Export "Material Return Overlay Print" to Excel with Java (Formula & Calculated Reports)

JQuick-ExcelFormula & Calculated Reports

Formula & Calculated ReportsScene 0066

Registers materials by return detail, computing Return Amount = Return Quantity × Unit Price per row, with the last row summarizing return quantity and return amount. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Material Return Overlay Print — Formula & Calculated Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Material Return Overlay Print" workbook: 1 data sheet(s); MAPPING over 7 columns (物料编码, 物料名称, 规格型号, 退货数量, 单价, 退货金额, 退货原因), 8 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportMaterialReturn() once — everything else is driven by the config.

It belongs to the "Formula & Calculated Reports" group: FORMULAS — inline expressions and aggregate functions such as Quantity × Unit Price or Balance = Receivable − Received, with SUM totals on the last row.

What you can copy from this page

  • The complete Java test class (65 lines, runnable as-is)
  • The complete XML config (31 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 formulas, Java Excel SUM formula export, Apache POI write formula example, Java computed column export, export Material Return Overlay Print to Excel with Java

Output specs

Scene no.
0066
Group
Formula & Calculated Reports
Config lines
31
Source lines
65

Java source · Formulas0066MaterialReturnDemo.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.formulas;

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 Formulas0066MaterialReturnDemo {

    private static final String XML = "jquick/biz/formulas/0066_formulas_material-return.xml";

    /** 导出:纯 XML 完成退货金额计算与合计。 */
    @Test
    public void exportMaterialReturn() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        // f=退货金额 留空,由 FORMULAS 计算
        rows.add(line("M2601", "冷轧钢板", "1.2mm", 200, 4.50, "尺寸偏差"));
        rows.add(line("M2602", "铜芯电缆", "2.5mm²", 150, 12.80, "绝缘不合格"));
        rows.add(line("M2603", "铝合金型材", "6063", 300, 8.20, "表面划伤"));
        rows.add(line("M2604", "不锈钢管", "304", 120, 25.00, "壁厚不足"));
        rows.add(line("M2605", "橡胶密封圈", "DN50", 500, 2.30, "硬度不达标"));
        // 合计占位行:退货数量与退货金额合计留给 FORMULAS
        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);
        rows.add(total);

        File out = new File("D:/test/excel/biz/formulas/0066_formulas_material-return.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("navyBlue", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Formulas0066MaterialReturnService service = factory.createApi(Formulas0066MaterialReturnService.class);
            service.exportMaterialReturn("field", "value");
        }

    }

    /** 构造一条退货明细(a=物料编码,b=物料名称,c=规格,d=退货数量,e=单价,f=退货金额,g=退货原因)。 */
    private static JQuickRow line(String materialCode, String materialName, String spec,
                                  int returnQty, double unitPrice, String returnReason) {
        JQuickRow row = new JQuickRow();
        row.put("a", materialCode);
        row.put("b", materialName);
        row.put("c", spec);
        row.put("d", returnQty);
        row.put("e", unitPrice);
        row.put("f", null);
        row.put("g", returnReason);
        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("navyBlue", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0066MaterialReturnService service = factory.createApi(Formulas0066MaterialReturnService.class);

XML config · 0066_formulas_material-return.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.formulas.Formulas0066MaterialReturnService">

    <!-- 导出:材料退货单 -->
    <excel name="exportMaterialReturn" 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:'D4*E4',F5:'D5*E5',F6:'D6*E6',
                    D7:'SUM(D2:D6)',F7:'SUM(F2:F6)'
                },
                STYLE={
                    ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的材料退货单 -->
    <excel name="importMaterialReturn" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="材料退货单", HEADER=true,
                MAPPING={"物料编码":"materialCode","物料名称":"materialName","规格型号":"spec","退货数量":"returnQty","单价":"unitPrice","退货金额":"returnAmount","退货原因":"returnReason"}
        ]]>
    </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.