Skip to content

Export "Inventory Turnover Report" to Excel with Java (Formula & Calculated Reports)

JQuick-ExcelFormula & Calculated Reports

Formula & Calculated ReportsScene 0005

Material inbound count, outbound count, ending inventory and inventory age, computing Turnover Rate = Outbound Count / Ending Inventory per row. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Inventory Turnover Report — Formula & Calculated Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Inventory Turnover Report" workbook: 1 data sheet(s); MAPPING over 7 columns (物料编码, 物料名称, 入库次数, 出库次数, 期末库存, 库龄(天), 周转率(次)), 12 formula(s), 1 style rule(s). On the Java side it builds 4 data rows and calls exportInventoryTurnover() 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 (60 lines, runnable as-is)
  • The complete XML config (26 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 Inventory Turnover Report to Excel with Java

Output specs

Scene no.
0005
Group
Formula & Calculated Reports
Config lines
26
Source lines
60

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

    private static final String XML = "jquick/biz/formulas/0005_formulas_inventory-turnover.xml";

    /** 导出:3 条物料周转明细 + 1 行合计,回读校验周转率与汇总公式。 */
    @Test
    public void exportInventoryTurnover() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(detail("M001", "螺丝", 20, 50, 100, 45));
        rows.add(detail("M002", "扳手", 10, 30, 60, 30));
        rows.add(detail("M003", "电缆", 6, 24, 80, 60));
        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/0005_formulas_inventory-turnover.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("amethyst", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Formulas0005InventoryTurnoverService service = factory.createApi(Formulas0005InventoryTurnoverService.class);
            service.exportInventoryTurnover("field", "value");
        }

    }

    /** 构造一条物料周转明细(a=编码,b=名称,c=入库次数,d=出库次数,e=期末库存,f=库龄;g=周转率由 FORMULAS 计算,占位保列)。 */
    private static JQuickRow detail(String code, String name, int inCount, int outCount, int stock, int ageDays) {
        JQuickRow row = new JQuickRow();
        row.put("a", code);
        row.put("b", name);
        row.put("c", inCount);
        row.put("d", outCount);
        row.put("e", stock);
        row.put("f", ageDays);
        row.put("g", 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("amethyst", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0005InventoryTurnoverService service = factory.createApi(Formulas0005InventoryTurnoverService.class);

XML config · 0005_formulas_inventory-turnover.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.Formulas0005InventoryTurnoverService">

    <!-- 导出:库存周转报表 -->
    <excel name="exportInventoryTurnover" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="库存周转报表", HEADER=true,
                MAPPING={"a":"物料编码","b":"物料名称","c":"入库次数","d":"出库次数","e":"期末库存","f":"库龄(天)","g":"周转率(次)"},
                FORMAT={"c":"0","d":"0","e":"0","f":"0","g":"0.00"},
                FORMULAS={G2:'D2/E2',G3:'D3/E3',G4:'D4/E4',C5:'SUM(C2:C4)',D5:'SUM(D2:D4)',E5:'SUM(E2:E4)',F5:'AVERAGE(F2:F4)',G5:'D5/E5'},
                STYLE={ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}}
        ]]>
    </excel>

    <!-- 导入解析:读取导出的 Excel -->
    <excel name="importInventoryTurnover" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="库存周转报表", HEADER=true,
                MAPPING={"物料编码":"code","物料名称":"name","入库次数":"inCount","出库次数":"outCount","期末库存":"stock","库龄(天)":"ageDays","周转率(次)":"turnover"}
        ]]>
    </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.