Skip to content

Export "Inventory Age Analysis" to Excel with Java (Styled & Formatted Reports)

JQuick-ExcelStyled & Formatted Reports

Styled & Formatted ReportsScene 0028

Layers inventory by age range, tallying the number of material types, inventory amount and amount share for each range, with a summary in the last row; the cell of the range with the longest age is marked in red to flag slow-moving inventory risk. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Inventory Age Analysis — Styled & Formatted Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Inventory Age Analysis" workbook: 1 data sheet(s); MAPPING over 5 columns (库龄区间, 物料种类数, 库存金额, 金额占比, 风险等级), 9 formula(s), 1 style rule(s). On the Java side it builds 5 data rows and calls exportInventoryAging() once — everything else is driven by the config.

It belongs to the "Styled & Formatted Reports" group: STYLE and conditional formatting: themed headers, zebra striping, highlighted total rows and threshold-based colouring of abnormal cells.

What you can copy from this page

  • The complete Java test class (59 lines, runnable as-is)
  • The complete XML config (33 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 styles, Java Excel header style zebra rows, Apache POI CellStyle export, Java Excel conditional formatting, export Inventory Age Analysis to Excel with Java

Output specs

Scene no.
0028
Group
Styled & Formatted Reports
Config lines
33
Source lines
59

Java source · Style0028InventoryAgingAnalysisDemo.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.style;

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

    private static final String XML = "jquick/biz/style/0028_style_inventory-aging-analysis.xml";

    /** 导出:纯 XML 完成金额占比、合计行汇总与呆滞区间标红。 */
    @Test
    public void exportInventoryAging() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(bucket("0-30 天", 45, 850000, "低"));
        rows.add(bucket("31-90 天", 28, 420000, "低"));
        rows.add(bucket("91-180 天", 12, 180000, "中"));
        // 第 5 行:180 天以上为呆滞库存,模板中 C5 / D5 标红
        rows.add(bucket("180 天以上", 5, 95000, "高"));
        // 合计占位行:占比留给 FORMULAS
        JQuickRow total = new JQuickRow();
        total.put("a", "合计");
        total.put("b", null);
        total.put("c", null);
        total.put("d", null);
        total.put("e", null);
        rows.add(total);

        File out = new File("D:/test/excel/biz/style/0028_style_inventory-aging-analysis.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("pearl", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Style0028InventoryAgingAnalysisService service = factory.createApi(Style0028InventoryAgingAnalysisService.class);
            service.exportInventoryAging("field", "value");
        }

    }

    /** 构造一条库龄区间(a=区间,b=种类数,c=库存金额,e=风险等级)。 */
    private static JQuickRow bucket(String agingBucket, int skuCount, double amount, String riskLevel) {
        JQuickRow row = new JQuickRow();
        row.put("a", agingBucket);
        row.put("b", skuCount);
        row.put("c", amount);
        row.put("d", null);
        row.put("e", riskLevel);
        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("pearl", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0028InventoryAgingAnalysisService service = factory.createApi(Style0028InventoryAgingAnalysisService.class);

XML config · 0028_style_inventory-aging-analysis.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.style.Style0028InventoryAgingAnalysisService">

    <!-- 导出:库存库龄分析表 -->
    <excel name="exportInventoryAging" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="库存库龄分析表", HEADER=true,
                MAPPING={"a":"库龄区间","b":"物料种类数","c":"库存金额","d":"金额占比","e":"风险等级"},
                FORMAT={"b":"0","c":"#,##0.00","d":"0.00%"},
                FORMULAS={
                    D2:'C2/C6',D3:'C3/C6',D4:'C4/C6',D5:'C5/C6',
                    B6:'SUM(B2:B5)',C6:'SUM(C2:C5)',D6:'SUM(D2:D5)'
                },
                STYLE={
                    ROW 6:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    C5:{color:red,bold:true},
                    D5:{color:red,bold:true}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的库存库龄分析表 -->
    <excel name="importInventoryAging" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="库存库龄分析表", HEADER=true,
                MAPPING={"库龄区间":"agingBucket","物料种类数":"skuCount","库存金额":"stockAmount","金额占比":"amountRatio","风险等级":"riskLevel"}
        ]]>
    </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.