Skip to content

Export "Sales Return Analysis" to Excel with Java (Styled & Formatted Reports)

JQuick-ExcelStyled & Formatted Reports

Styled & Formatted ReportsScene 0090

Registers sales quantity and return quantity per product, computing Return Amount = Return Quantity × Unit Price and Return Rate = Return Quantity / Sales Quantity, with the last row summarizing sales volume, return quantity and return amount; the product with the highest return rate is marked in red. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Sales Return Analysis — Styled & Formatted Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Sales Return Analysis" workbook: 1 data sheet(s); MAPPING over 7 columns (产品名称, 销售数量, 退货数量, 单价, 退货金额, 退货率, 处理方式), 18 formula(s), 1 style rule(s). On the Java side it builds 7 data rows and calls exportSalesReturnAnalysis() 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 (66 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 Sales Return Analysis to Excel with Java

Output specs

Scene no.
0090
Group
Styled & Formatted Reports
Config lines
33
Source lines
66

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

    private static final String XML = "jquick/biz/style/0090_style_sales-return-analysis.xml";

    /** 导出:纯 XML 完成逐行退货金额、退货率、合计与最高退货率标红。 */
    @Test
    public void exportSalesReturnAnalysis() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        // e/f 留空,由 FORMULAS 计算
        rows.add(product("A型电机", 2000, 40, 120.00, "返修"));
        rows.add(product("B型电机", 1500, 45, 95.00, "返修"));
        rows.add(product("C型水泵", 800, 64, 260.00, "报废"));
        rows.add(product("D型水泵", 1200, 18, 310.00, "返修"));
        rows.add(product("E型风机", 600, 30, 450.00, "折价处理"));
        rows.add(product("F型风机", 1000, 15, 280.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/0090_style_sales-return-analysis.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("amethyst", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Style0090SalesReturnAnalysisService service = factory.createApi(Style0090SalesReturnAnalysisService.class);
            service.exportSalesReturnAnalysis("field", "value");
        }

    }

    /** 构造一条退货明细(a=产品,b=销售数量,c=退货数量,d=单价,e/f 留空,g=处理方式)。 */
    private static JQuickRow product(String productName, double salesQuantity, double returnQuantity,
                                     double unitPrice, String handleType) {
        JQuickRow row = new JQuickRow();
        row.put("a", productName);
        row.put("b", salesQuantity);
        row.put("c", returnQuantity);
        row.put("d", unitPrice);
        row.put("e", null);
        row.put("f", null);
        row.put("g", handleType);
        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("amethyst", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0090SalesReturnAnalysisService service = factory.createApi(Style0090SalesReturnAnalysisService.class);

XML config · 0090_style_sales-return-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.Style0090SalesReturnAnalysisService">

    <!-- 导出:销售退货分析表 -->
    <excel name="exportSalesReturnAnalysis" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="销售退货分析表", HEADER=true,
                MAPPING={"a":"产品名称","b":"销售数量","c":"退货数量","d":"单价","e":"退货金额","f":"退货率","g":"处理方式"},
                FORMAT={"b":"#,##0","c":"#,##0","d":"#,##0.00","e":"#,##0.00","f":"0.00%"},
                FORMULAS={
                    E2:'C2*D2',F2:'C2/B2', E3:'C3*D3',F3:'C3/B3', E4:'C4*D4',F4:'C4/B4',
                    E5:'C5*D5',F5:'C5/B5', E6:'C6*D6',F6:'C6/B6', E7:'C7*D7',F7:'C7/B7',
                    B8:'SUM(B2:B7)',C8:'SUM(C2:C7)',E8:'SUM(E2:E7)',F8:'C8/B8'
                },
                STYLE={
                    ROW 8:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    F4:{color:red,bold:true}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的销售退货分析表 -->
    <excel name="importSalesReturnAnalysis" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="销售退货分析表", HEADER=true,
                MAPPING={"产品名称":"productName","销售数量":"salesQuantity","退货数量":"returnQuantity","单价":"unitPrice","退货金额":"returnAmount","退货率":"returnRate","处理方式":"handleType"}
        ]]>
    </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.