Skip to content

Export "Product Quarterly Sales Comparison" to Excel with Java (Chart & Visualization)

JQuick-ExcelChart & Visualization

Chart & VisualizationScene 0105

Q1-Q4 sales for four products, computing the full-year total at each row's end and summarizing by quarter in the last row; includes a column chart comparing each product's quarterly trend. Exported as a multi-sheet workbook (Product Quarterly Sales / Product Quarterly Sales Chart). Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Product Quarterly Sales Comparison — Chart & Visualization
Full screenshot of the exported xlsx

About this demo

This demo exports the "Product Quarterly Sales Comparison" workbook: 1 data sheet(s); MAPPING over 6 columns (产品, 一季度, 二季度, 三季度, 四季度, 全年), 16 formula(s), 1 style rule(s), COLUMN chart on its own sheet. On the Java side it builds 5 data rows and calls exportProductQuarterlySales() 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 (64 lines, runnable as-is)
  • The complete XML config (37 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 Product Quarterly Sales Comparison to Excel with Java

Output specs

Scene no.
0105
Group
Chart & Visualization
Config lines
37
Source lines
64

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

    private static final String XML = "jquick/biz/graph/0105_graph_product-quarterly-sales.xml";

    /** 导出:4 个产品 + 1 行合计 + 柱形图,回读校验全年合计、末行汇总与图表 sheet。 */
    @Test
    public void exportProductQuarterlySales() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(detail("产品A", 120, 180, 150, 200));
        rows.add(detail("产品B", 200, 210, 220, 240));
        rows.add(detail("产品C", 150, 190, 170, 210));
        rows.add(detail("产品D", 180, 160, 200, 190));
        rows.add(totalRow());

        File out = new File("D:/test/excel/biz/graph/0105_graph_product-quarterly-sales.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("oceanBlue", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Graph0105ProductQuarterlySalesService service = factory.createApi(Graph0105ProductQuarterlySalesService.class);
            service.exportProductQuarterlySales("field", "value");
        }

    }

    /** 构造一条产品明细(f=全年由 FORMULAS 计算,占位保列)。 */
    private static JQuickRow detail(String product, double q1, double q2, double q3, double q4) {
        JQuickRow row = new JQuickRow();
        row.put("a", product);
        row.put("b", q1);
        row.put("c", q2);
        row.put("d", q3);
        row.put("e", q4);
        row.put("f", 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);
        row.put("e", null);
        row.put("f", 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("oceanBlue", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Graph0105ProductQuarterlySalesService service = factory.createApi(Graph0105ProductQuarterlySalesService.class);

XML config · 0105_graph_product-quarterly-sales.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.Graph0105ProductQuarterlySalesService">

    <!-- 导出:产品季度销量对比 -->
    <excel name="exportProductQuarterlySales" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="产品季度销量", HEADER=true,
                MAPPING={"a":"产品","b":"一季度","c":"二季度","d":"三季度","e":"四季度","f":"全年"},
                FORMAT={"b":"#,##0","c":"#,##0","d":"#,##0","e":"#,##0","f":"#,##0"},
                FORMULAS={
                    F2:'SUM(B2:E2)',F3:'SUM(B3:E3)',F4:'SUM(B4:E4)',F5:'SUM(B5:E5)',
                    B6:'SUM(B2:B5)',C6:'SUM(C2:C5)',D6:'SUM(D2:D5)',E6:'SUM(E2:E5)',F6:'SUM(F2:F5)'
                },
                STYLE={ROW 6:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}},
                GRAPH={TYPE=COLUMN, TITLE="产品季度销量图", CATEGORY_AXIS=产品, VALUE_AXIS=销量,
                       CATEGORIES=["产品A","产品B","产品C","产品D"],
                       SERIES=[
                           {NAME=一季度, DATA=[120,200,150,180]},
                           {NAME=二季度, DATA=[180,210,190,160]},
                           {NAME=三季度, DATA=[150,220,170,200]},
                           {NAME=四季度, DATA=[200,240,210,190]}
                       ]}
        ]]>
    </excel>

    <!-- 导入解析:读取导出的产品季度销量 -->
    <excel name="importProductQuarterlySales" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="产品季度销量", HEADER=true,
                MAPPING={"产品":"product","一季度":"q1","二季度":"q2","三季度":"q3","四季度":"q4","全年":"total"}
        ]]>
    </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.