Export "Inventory Alert Report" to Excel with Java (Styled & Formatted Reports)
Styled & Formatted ReportsScene 0035
Compares current inventory with safety stock per material, computing Inventory Variance = Current Inventory - Safety Stock per row, where a negative variance reaches the alert threshold, and the last row summarizes the two inventory quantity columns and gives the total variance. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Inventory Alert Report" workbook: 1 data sheet(s); MAPPING over 7 columns (物料编码, 物料名称, 所属仓库, 当前库存, 安全库存, 库存差额, 预警状态), 10 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportInventoryAlert() 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 (63 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 Alert Report to Excel with Java
Output specs
- Scene no.
- 0035
- Group
- Styled & Formatted Reports
- Config lines
- 33
- Source lines
- 63
Java source · Style0035InventoryAlertReportDemo.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 Style0035InventoryAlertReportDemo {
private static final String XML = "jquick/biz/style/0035_style_inventory-alert-report.xml";
/** 导出:纯 XML 完成逐行差额、合计汇总与预警标红。 */
@Test
public void exportInventoryAlert() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
rows.add(item("M-1001", "内六角螺丝 M6", "一号仓", 800, 500, "库存充足"));
rows.add(item("M-1002", "深沟球轴承 6204", "一号仓", 120, 300, "低于安全库存"));
rows.add(item("M-1003", "硅胶密封圈", "二号仓", 2000, 1000, "库存充足"));
rows.add(item("M-1004", "三相异步电机 1.5kW", "二号仓", 30, 50, "低于安全库存"));
rows.add(item("M-1005", "PLC 控制板", "三号仓", 450, 200, "库存充足"));
// 合计占位行:两列库存与差额留给 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/style/0035_style_inventory-alert-report.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("oceanBlue", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0035InventoryAlertReportService service = factory.createApi(Style0035InventoryAlertReportService.class);
service.exportInventoryAlert("field", "value");
}
}
/** 构造一条物料行(a=编码,b=名称,c=仓库,d=当前库存,e=安全库存,g=预警状态)。 */
private static JQuickRow item(String code, String name, String warehouse, double current, double safety, String status) {
JQuickRow row = new JQuickRow();
row.put("a", code);
row.put("b", name);
row.put("c", warehouse);
row.put("d", current);
row.put("e", safety);
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("oceanBlue", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0035InventoryAlertReportService service = factory.createApi(Style0035InventoryAlertReportService.class);XML config · 0035_style_inventory-alert-report.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.Style0035InventoryAlertReportService">
<!-- 导出:库存预警报表 -->
<excel name="exportInventoryAlert" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="库存预警报表", HEADER=true,
MAPPING={"a":"物料编码","b":"物料名称","c":"所属仓库","d":"当前库存","e":"安全库存","f":"库存差额","g":"预警状态"},
FORMAT={"d":"#,##0","e":"#,##0","f":"#,##0"},
FORMULAS={
F2:'D2-E2',F3:'D3-E3',F4:'D4-E4',F5:'D5-E5',F6:'D6-E6',
D7:'SUM(D2:D6)',E7:'SUM(E2:E6)',F7:'D7-E7'
},
STYLE={
ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
F3:{color:red,bold:true},
F5:{color:red,bold:true}
}
]]>
</excel>
<!-- 导入解析:读取导出的库存预警报表 -->
<excel name="importInventoryAlert" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="库存预警报表", HEADER=true,
MAPPING={"物料编码":"materialCode","物料名称":"materialName","所属仓库":"warehouse","当前库存":"currentStock","安全库存":"safetyStock","库存差额":"stockDiff","预警状态":"alertStatus"}
]]>
</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.