Skip to content

Export "Self-Service Store Sales Analysis Report" to Excel with Java (Styled & Formatted Reports)

JQuick-ExcelStyled & Formatted Reports

Styled & Formatted ReportsScene 0076

Registers sales and cost per store, computing Gross Profit = Sales - Cost, Gross Margin = Gross Profit / Sales and Sales Share = Store Sales / Total Sales, with the last row summarizing sales, cost, gross profit and the overall gross margin; the store with the lowest gross margin is marked in red. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Self-Service Store Sales Analysis Report — Styled & Formatted Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Self-Service Store Sales Analysis Report" workbook: 1 data sheet(s); MAPPING over 7 columns (门店, 主营品类, 销售额, 成本, 毛利, 毛利率, 销售占比), 21 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportStoreSalesAnalysis() 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 (64 lines, runnable as-is)
  • The complete XML config (36 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 Self-Service Store Sales Analysis Report to Excel with Java

Output specs

Scene no.
0076
Group
Styled & Formatted Reports
Config lines
36
Source lines
64

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

    private static final String XML = "jquick/biz/style/0076_style_store-sales-analysis.xml";

    /** 导出:纯 XML 完成毛利、毛利率、销售占比、合计与最低毛利率标红。 */
    @Test
    public void exportStoreSalesAnalysis() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        // e/f/g 留空,由 FORMULAS 计算
        rows.add(store("城东店", "家电", 520000.00, 380000.00));
        rows.add(store("城西店", "服饰", 360000.00, 240000.00));
        rows.add(store("城南店", "生鲜", 280000.00, 230000.00));
        rows.add(store("城北店", "数码", 640000.00, 520000.00));
        rows.add(store("中心店", "家居", 410000.00, 300000.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);
        total.put("g", null);
        rows.add(total);

        File out = new File("D:/test/excel/biz/style/0076_style_store-sales-analysis.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("plum", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Style0076StoreSalesAnalysisService service = factory.createApi(Style0076StoreSalesAnalysisService.class);
            service.exportStoreSalesAnalysis("field", "value");
        }

    }

    /** 构造一家门店(a=门店,b=品类,c=销售额,d=成本,e/f/g 留空)。 */
    private static JQuickRow store(String storeName, String category, double salesAmount, double costAmount) {
        JQuickRow row = new JQuickRow();
        row.put("a", storeName);
        row.put("b", category);
        row.put("c", salesAmount);
        row.put("d", costAmount);
        row.put("e", null);
        row.put("f", null);
        row.put("g", 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("plum", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0076StoreSalesAnalysisService service = factory.createApi(Style0076StoreSalesAnalysisService.class);

XML config · 0076_style_store-sales-analysis.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.Style0076StoreSalesAnalysisService">

    <!-- 导出:门店销售自助分析报表 -->
    <excel name="exportStoreSalesAnalysis" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="门店销售自助分析报表", HEADER=true,
                MAPPING={"a":"门店","b":"主营品类","c":"销售额","d":"成本","e":"毛利","f":"毛利率","g":"销售占比"},
                FORMAT={"c":"#,##0","d":"#,##0","e":"#,##0","f":"0.00%","g":"0.00%"},
                FORMULAS={
                    E2:'C2-D2',F2:'E2/C2',G2:'C2/C7',
                    E3:'C3-D3',F3:'E3/C3',G3:'C3/C7',
                    E4:'C4-D4',F4:'E4/C4',G4:'C4/C7',
                    E5:'C5-D5',F5:'E5/C5',G5:'C5/C7',
                    E6:'C6-D6',F6:'E6/C6',G6:'C6/C7',
                    C7:'SUM(C2:C6)',D7:'SUM(D2:D6)',E7:'C7-D7',F7:'E7/C7'
                },
                STYLE={
                    ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    F4:{color:red,bold:true}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的门店销售自助分析报表 -->
    <excel name="importStoreSalesAnalysis" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="门店销售自助分析报表", HEADER=true,
                MAPPING={"门店":"store","主营品类":"category","销售额":"salesAmount","成本":"costAmount","毛利":"grossProfit","毛利率":"grossMargin","销售占比":"salesShare"}
        ]]>
    </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.