Export "Budget vs. Actual Variance Analysis" to Excel with Java (Styled & Formatted Reports)
Styled & Formatted ReportsScene 0019
Compares budget and actual by expense account, computing Variance = Actual - Budget and Variance Rate = Variance / Budget per row, with a total row at the end; the conclusion cell of accounts concluded as "Over Budget" is marked in red font. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Budget vs. Actual Variance Analysis" workbook: 1 data sheet(s); MAPPING over 6 columns (费用科目, 预算金额, 实际金额, 差异, 差异率, 结论), 18 formula(s), 1 style rule(s). On the Java side it builds 7 data rows and calls exportBudgetVariance() 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 (62 lines, runnable as-is)
- The complete XML config (35 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 Budget vs. Actual Variance Analysis to Excel with Java
Output specs
- Scene no.
- 0019
- Group
- Styled & Formatted Reports
- Config lines
- 35
- Source lines
- 62
Java source · Style0019BudgetVarianceDemo.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 Style0019BudgetVarianceDemo {
private static final String XML = "jquick/biz/style/0019_style_budget-variance.xml";
/** 导出:纯 XML 完成差异、差异率、合计与超支标红。 */
@Test
public void exportBudgetVariance() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
rows.add(subject("人力成本", 800000, 780000, "结余"));
rows.add(subject("市场推广", 300000, 350000, "超支"));
rows.add(subject("研发投入", 500000, 480000, "结余"));
rows.add(subject("差旅费用", 80000, 95000, "超支"));
rows.add(subject("办公费用", 60000, 55000, "结余"));
rows.add(subject("招待费用", 40000, 52000, "超支"));
// 合计占位行:金额、差异、差异率留给 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);
rows.add(total);
File out = new File("D:/test/excel/biz/style/0019_style_budget-variance.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("slate", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0019BudgetVarianceService service = factory.createApi(Style0019BudgetVarianceService.class);
service.exportBudgetVariance("field", "value");
}
}
/** 构造一条科目差异明细(a=科目,b=预算,c=实际,f=结论)。 */
private static JQuickRow subject(String name, double budget, double actual, String conclusion) {
JQuickRow row = new JQuickRow();
row.put("a", name);
row.put("b", budget);
row.put("c", actual);
row.put("d", null);
row.put("e", null);
row.put("f", conclusion);
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("slate", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0019BudgetVarianceService service = factory.createApi(Style0019BudgetVarianceService.class);XML config · 0019_style_budget-variance.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.Style0019BudgetVarianceService">
<!-- 导出:预算与实际差异分析 -->
<excel name="exportBudgetVariance" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="预算实际差异分析", HEADER=true,
MAPPING={"a":"费用科目","b":"预算金额","c":"实际金额","d":"差异","e":"差异率","f":"结论"},
FORMAT={"b":"#,##0.00","c":"#,##0.00","d":"#,##0.00","e":"0.00%"},
FORMULAS={
D2:'C2-B2',D3:'C3-B3',D4:'C4-B4',D5:'C5-B5',D6:'C6-B6',D7:'C7-B7',
E2:'D2/B2',E3:'D3/B3',E4:'D4/B4',E5:'D5/B5',E6:'D6/B6',E7:'D7/B7',
B8:'SUM(B2:B7)',C8:'SUM(C2:C7)',D8:'C8-B8',E8:'D8/B8'
},
STYLE={
ROW 8:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
F3:{color:red,bold:true},
F5:{color:red,bold:true},
F7:{color:red,bold:true}
}
]]>
</excel>
<!-- 导入解析:读取导出的预算与实际差异分析 -->
<excel name="importBudgetVariance" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="预算实际差异分析", HEADER=true,
MAPPING={"费用科目":"subject","预算金额":"budget","实际金额":"actual","差异":"variance","差异率":"varianceRate","结论":"conclusion"}
]]>
</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.