Skip to content

Export "Department Expense Comparison" to Excel with Java (Chart & Visualization)

JQuick-ExcelChart & Visualization

Chart & VisualizationScene 0106

Budget and actual expenses compared per department, computing the variance (Actual - Budget) at each row's end and summarizing in the last row; includes a 3-D bar chart comparing budget vs. actual. Exported as a multi-sheet workbook (Department Expense Comparison / Department Expense Comparison Chart). Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Department Expense Comparison — Chart & Visualization
Full screenshot of the exported xlsx

About this demo

This demo exports the "Department Expense Comparison" workbook: 1 data sheet(s); MAPPING over 4 columns (部门, 预算, 实际, 差异), 9 formula(s), 1 style rule(s), BAR3D chart on its own sheet. On the Java side it builds 5 data rows and calls exportDepartmentCostCompare() once — everything else is driven by the config.

It belongs to the "Chart & Visualization" group: GRAPH charts — column, line, scatter and radar charts written into a separate sheet, making the workbook multi-sheet.

What you can copy from this page

  • The complete Java test class (60 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 chart, Java generate Excel line chart, Apache POI chart export example, Java multi-sheet workbook export, export Department Expense Comparison to Excel with Java

Output specs

Scene no.
0106
Group
Chart & Visualization
Config lines
35
Source lines
60

Java source · Graph0106DepartmentCostCompareDemo.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.graph;

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

    private static final String XML = "jquick/biz/graph/0106_graph_department-cost-compare.xml";

    /** 导出:4 个部门 + 1 行合计 + 三维条形图,回读校验差异、汇总与图表 sheet。 */
    @Test
    public void exportDepartmentCostCompare() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(detail("销售部", 120000, 135000));
        rows.add(detail("研发部", 180000, 165000));
        rows.add(detail("客服部", 90000, 105000));
        rows.add(detail("运营部", 150000, 142000));
        rows.add(totalRow());

        File out = new File("D:/test/excel/biz/graph/0106_graph_department-cost-compare.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("sunsetOrange", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Graph0106DepartmentCostCompareService service = factory.createApi(Graph0106DepartmentCostCompareService.class);
            service.exportDepartmentCostCompare("field", "value");
        }

    }

    /** 构造一条部门明细(d=差异由 FORMULAS 计算,占位保列)。 */
    private static JQuickRow detail(String department, double budget, double actual) {
        JQuickRow row = new JQuickRow();
        row.put("a", department);
        row.put("b", budget);
        row.put("c", actual);
        row.put("d", null);
        return row;
    }

    /** 合计行:只写「合计」文字,其余列以空值占位(必须补齐全部映射列)。 */
    private static JQuickRow totalRow() {
        JQuickRow row = new JQuickRow();
        row.put("a", "合计");
        row.put("b", null);
        row.put("c", null);
        row.put("d", null);
        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("sunsetOrange", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Graph0106DepartmentCostCompareService service = factory.createApi(Graph0106DepartmentCostCompareService.class);

XML config · 0106_graph_department-cost-compare.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.graph.Graph0106DepartmentCostCompareService">

    <!-- 导出:部门费用对比 -->
    <excel name="exportDepartmentCostCompare" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="部门费用对比", HEADER=true,
                MAPPING={"a":"部门","b":"预算","c":"实际","d":"差异"},
                FORMAT={"b":"#,##0.00","c":"#,##0.00","d":"#,##0.00"},
                FORMULAS={
                    D2:'C2-B2',D3:'C3-B3',D4:'C4-B4',D5:'C5-B5',
                    B6:'SUM(B2:B5)',C6:'SUM(C2:C5)',D6:'C6-B6'
                },
                STYLE={ROW 6:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}},
                GRAPH={TYPE=BAR3D, TITLE="部门费用对比图", CATEGORY_AXIS=部门, VALUE_AXIS=金额,
                       CATEGORIES=["销售部","研发部","客服部","运营部"],
                       SERIES=[
                           {NAME=预算, DATA=[120000,180000,90000,150000]},
                           {NAME=实际, DATA=[135000,165000,105000,142000]}
                       ]}
        ]]>
    </excel>

    <!-- 导入解析:读取导出的部门费用对比 -->
    <excel name="importDepartmentCostCompare" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="部门费用对比", HEADER=true,
                MAPPING={"部门":"department","预算":"budget","实际":"actual","差异":"variance"}
        ]]>
    </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.