Skip to content

Export "Monthly Sales Matrix" to Excel with Java (Chart & Visualization)

JQuick-ExcelChart & Visualization

Chart & VisualizationScene 0110

A sales matrix for three products across January-March, summarizing by month in the last row; includes a surface chart showing the two-dimensional (Product × Month) distribution. Exported as a multi-sheet workbook (Monthly Sales Matrix / Monthly Sales Surface Chart). Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Monthly Sales Matrix — Chart & Visualization
Full screenshot of the exported xlsx

About this demo

This demo exports the "Monthly Sales Matrix" workbook: 1 data sheet(s); MAPPING over 4 columns (产品, 1月, 2月, 3月), 6 formula(s), 1 style rule(s), SURFACE chart on its own sheet. On the Java side it builds 4 data rows and calls exportMonthlyProductMatrix() 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 (59 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 Monthly Sales Matrix to Excel with Java

Output specs

Scene no.
0110
Group
Chart & Visualization
Config lines
35
Source lines
59

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

    private static final String XML = "jquick/biz/graph/0110_graph_monthly-product-matrix.xml";

    /** 导出:3 个产品 + 1 行合计 + 曲面图,回读校验末行汇总与图表 sheet。 */
    @Test
    public void exportMonthlyProductMatrix() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(detail("产品A", 120, 140, 160));
        rows.add(detail("产品B", 150, 160, 180));
        rows.add(detail("产品C", 180, 200, 220));
        rows.add(totalRow());

        File out = new File("D:/test/excel/biz/graph/0110_graph_monthly-product-matrix.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("turquoise", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Graph0110MonthlyProductMatrixService service = factory.createApi(Graph0110MonthlyProductMatrixService.class);
            service.exportMonthlyProductMatrix("field", "value");
        }

    }

    /** 构造一条产品明细(三列为直接值,末行汇总由 FORMULAS 计算)。 */
    private static JQuickRow detail(String product, double jan, double feb, double mar) {
        JQuickRow row = new JQuickRow();
        row.put("a", product);
        row.put("b", jan);
        row.put("c", feb);
        row.put("d", mar);
        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("turquoise", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Graph0110MonthlyProductMatrixService service = factory.createApi(Graph0110MonthlyProductMatrixService.class);

XML config · 0110_graph_monthly-product-matrix.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.Graph0110MonthlyProductMatrixService">

    <!-- 导出:月度销量矩阵 -->
    <excel name="exportMonthlyProductMatrix" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="月度销量矩阵", HEADER=true,
                MAPPING={"a":"产品","b":"1月","c":"2月","d":"3月"},
                FORMAT={"b":"#,##0","c":"#,##0","d":"#,##0"},
                FORMULAS={
                    B5:'SUM(B2:B4)',C5:'SUM(C2:C4)',D5:'SUM(D2:D4)'
                },
                STYLE={ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}},
                GRAPH={TYPE=SURFACE, TITLE="月度销量曲面图", CATEGORY_AXIS=产品, VALUE_AXIS=销量,
                       CATEGORIES=["产品A","产品B","产品C"],
                       SERIES=[
                           {NAME=一月, DATA=[120,150,180]},
                           {NAME=二月, DATA=[140,160,200]},
                           {NAME=三月, DATA=[160,180,220]}
                       ]}
        ]]>
    </excel>

    <!-- 导入解析:读取导出的月度销量矩阵 -->
    <excel name="importMonthlyProductMatrix" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="月度销量矩阵", HEADER=true,
                MAPPING={"产品":"product","1月":"jan","2月":"feb","3月":"mar"}
        ]]>
    </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.