Export "Annual Inventory Count Variance" to Excel with Java (Styled & Formatted Reports)
Styled & Formatted ReportsScene 0080
Compares book quantity and physical count per material, computing Variance Quantity = Physical Count - Book Quantity and Variance Rate = Variance / Book Quantity, with the last row summarizing total book quantity, total physical count, total variance and the overall variance rate; the material with the most severe shortage is marked in red. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Annual Inventory Count Variance" workbook: 1 data sheet(s); MAPPING over 7 columns (物料编码, 物料名称, 账面数量, 实盘数量, 差异数量, 差异率, 盘点结果), 18 formula(s), 1 style rule(s). On the Java side it builds 7 data rows and calls exportAnnualInventoryCount() 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 (66 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 Annual Inventory Count Variance to Excel with Java
Output specs
- Scene no.
- 0080
- Group
- Styled & Formatted Reports
- Config lines
- 33
- Source lines
- 66
Java source · Style0080AnnualInventoryCountDemo.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 Style0080AnnualInventoryCountDemo {
private static final String XML = "jquick/biz/style/0080_style_annual-inventory-count.xml";
/** 导出:纯 XML 完成逐行差异、差异率、合计与盘亏标红。 */
@Test
public void exportAnnualInventoryCount() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
// e/f 留空,由 FORMULAS 计算
rows.add(material("M001", "螺丝 M4", 10000, 9980, "盘亏"));
rows.add(material("M002", "轴承 6204", 5000, 5000, "相符"));
rows.add(material("M003", "电机 Y2", 200, 176, "盘亏"));
rows.add(material("M004", "齿轮 Z30", 800, 820, "盘盈"));
rows.add(material("M005", "密封圈", 3000, 2950, "盘亏"));
rows.add(material("M006", "皮带 A型", 1000, 1005, "盘盈"));
// 合计占位行:各列合计与整体差异率留给 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/0080_style_annual-inventory-count.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("terracotta", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0080AnnualInventoryCountService service = factory.createApi(Style0080AnnualInventoryCountService.class);
service.exportAnnualInventoryCount("field", "value");
}
}
/** 构造一种物料(a=编码,b=名称,c=账面数量,d=实盘数量,e/f 留空,g=盘点结果)。 */
private static JQuickRow material(String code, String name, double bookQuantity,
double actualQuantity, String countResult) {
JQuickRow row = new JQuickRow();
row.put("a", code);
row.put("b", name);
row.put("c", bookQuantity);
row.put("d", actualQuantity);
row.put("e", null);
row.put("f", null);
row.put("g", countResult);
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("terracotta", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0080AnnualInventoryCountService service = factory.createApi(Style0080AnnualInventoryCountService.class);XML config · 0080_style_annual-inventory-count.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.Style0080AnnualInventoryCountService">
<!-- 导出:年度库存盘点差异表 -->
<excel name="exportAnnualInventoryCount" 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.00%"},
FORMULAS={
E2:'D2-C2',F2:'E2/C2',E3:'D3-C3',F3:'E3/C3',E4:'D4-C4',F4:'E4/C4',
E5:'D5-C5',F5:'E5/C5',E6:'D6-C6',F6:'E6/C6',E7:'D7-C7',F7:'E7/C7',
C8:'SUM(C2:C7)',D8:'SUM(D2:D7)',E8:'D8-C8',F8:'E8/C8'
},
STYLE={
ROW 8:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
E4:{color:red,bold:true}
}
]]>
</excel>
<!-- 导入解析:读取导出的年度库存盘点差异表 -->
<excel name="importAnnualInventoryCount" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="年度库存盘点差异表", HEADER=true,
MAPPING={"物料编码":"materialCode","物料名称":"materialName","账面数量":"bookQuantity","实盘数量":"actualQuantity","差异数量":"differenceQuantity","差异率":"differenceRate","盘点结果":"countResult"}
]]>
</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.