Skip to content

Export "Monthly Expense Budget Execution Variance Comparison" to Excel with Java (Styled & Formatted Reports)

JQuick-ExcelStyled & Formatted Reports

Styled & Formatted ReportsScene 0085

Compares budget and actual spending per expense item, computing Variance Amount = Budget - Actual and Variance Rate = Variance / Budget, with the last row summarizing budget, actual and total variance; the most over-budget item is marked in red. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Monthly Expense Budget Execution Variance Comparison — Styled & Formatted Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Monthly Expense Budget Execution Variance Comparison" workbook: 1 data sheet(s); MAPPING over 6 columns (费用项目, 预算金额, 实际支出, 偏差金额, 偏差率, 控制结论), 16 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportExpenseBudgetVariance() 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 (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 Monthly Expense Budget Execution Variance Comparison to Excel with Java

Output specs

Scene no.
0085
Group
Styled & Formatted Reports
Config lines
33
Source lines
62

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

    private static final String XML = "jquick/biz/style/0085_style_expense-budget-variance.xml";

    /** 导出:纯 XML 完成逐行偏差、偏差率、合计与超支标红。 */
    @Test
    public void exportExpenseBudgetVariance() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        // d/e 留空,由 FORMULAS 计算
        rows.add(item("办公费", 50000.00, 46000.00, "节约"));
        rows.add(item("差旅费", 120000.00, 138000.00, "超支"));
        rows.add(item("招待费", 80000.00, 92000.00, "超支"));
        rows.add(item("培训费", 60000.00, 55000.00, "节约"));
        rows.add(item("水电费", 40000.00, 38000.00, "节约"));
        // 合计占位行:预算 / 实际 / 总偏差留给 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/0085_style_expense-budget-variance.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("sage", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Style0085ExpenseBudgetVarianceService service = factory.createApi(Style0085ExpenseBudgetVarianceService.class);
            service.exportExpenseBudgetVariance("field", "value");
        }

    }

    /** 构造一条费用记录(a=项目,b=预算,c=实际,d/e 留空,f=控制结论)。 */
    private static JQuickRow item(String expenseItem, double budgetAmount, double actualAmount, String conclusion) {
        JQuickRow row = new JQuickRow();
        row.put("a", expenseItem);
        row.put("b", budgetAmount);
        row.put("c", actualAmount);
        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("sage", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0085ExpenseBudgetVarianceService service = factory.createApi(Style0085ExpenseBudgetVarianceService.class);

XML config · 0085_style_expense-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.Style0085ExpenseBudgetVarianceService">

    <!-- 导出:月度费用预算执行偏差对比表 -->
    <excel name="exportExpenseBudgetVariance" 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:'B2-C2',E2:'D2/B2',D3:'B3-C3',E3:'D3/B3',D4:'B4-C4',E4:'D4/B4',
                    D5:'B5-C5',E5:'D5/B5',D6:'B6-C6',E6:'D6/B6',
                    B7:'SUM(B2:B6)',C7:'SUM(C2:C6)',D7:'B7-C7',E7:'D7/B7'
                },
                STYLE={
                    ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    D3:{color:red,bold:true}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的月度费用预算执行偏差对比表 -->
    <excel name="importExpenseBudgetVariance" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="月度费用预算执行偏差对比表", HEADER=true,
                MAPPING={"费用项目":"expenseItem","预算金额":"budgetAmount","实际支出":"actualAmount","偏差金额":"varianceAmount","偏差率":"varianceRate","控制结论":"controlConclusion"}
        ]]>
    </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.