Export "Customer Return Reason Analysis" to Excel with Java (Formula & Calculated Reports)
Formula & Calculated ReportsScene 0097
Registers return quantity and amount per return reason, computing Quantity Share = This Reason's Return Quantity / Total Return Quantity, with the last row summarizing return quantity, amount and share; the reason with the highest share is marked in red. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Customer Return Reason Analysis" workbook: 1 data sheet(s); MAPPING over 5 columns (退货原因, 退货数量, 退货金额, 数量占比, 责任部门), 10 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportReturnReasonAnalysis() once — everything else is driven by the config.
It belongs to the "Formula & Calculated Reports" group: FORMULAS — inline expressions and aggregate functions such as Quantity × Unit Price or Balance = Receivable − Received, with SUM totals on the last row.
What you can copy from this page
- The complete Java test class (61 lines, runnable as-is)
- The complete XML config (32 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 formulas, Java Excel SUM formula export, Apache POI write formula example, Java computed column export, export Customer Return Reason Analysis to Excel with Java
Output specs
- Scene no.
- 0097
- Group
- Formula & Calculated Reports
- Config lines
- 32
- Source lines
- 61
Java source · Formulas0097ReturnReasonAnalysisDemo.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.formulas;
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 Formulas0097ReturnReasonAnalysisDemo {
private static final String XML = "jquick/biz/formulas/0097_formulas_return-reason-analysis.xml";
/** 导出:纯 XML 完成逐行数量占比、各列汇总与占比最高原因标红。 */
@Test
public void exportReturnReasonAnalysis() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
// d 留空,由 FORMULAS 计算
rows.add(reason("质量问题", 80, 24000.00, "生产部"));
rows.add(reason("包装破损", 45, 6750.00, "物流部"));
rows.add(reason("尺寸不符", 120, 36000.00, "研发部"));
rows.add(reason("交期延误", 30, 4500.00, "计划部"));
rows.add(reason("客户误购", 25, 2500.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);
rows.add(total);
File out = new File("D:/test/excel/biz/formulas/0097_formulas_return-reason-analysis.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("emerald", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0097ReturnReasonAnalysisService service = factory.createApi(Formulas0097ReturnReasonAnalysisService.class);
service.exportReturnReasonAnalysis("field", "value");
}
}
/** 构造一类退货原因(a=原因,b=数量,c=金额,d 留空,e=责任部门)。 */
private static JQuickRow reason(String returnReason, double returnQuantity, double returnAmount,
String responsibleDepartment) {
JQuickRow row = new JQuickRow();
row.put("a", returnReason);
row.put("b", returnQuantity);
row.put("c", returnAmount);
row.put("d", null);
row.put("e", responsibleDepartment);
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("emerald", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0097ReturnReasonAnalysisService service = factory.createApi(Formulas0097ReturnReasonAnalysisService.class);XML config · 0097_formulas_return-reason-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.formulas.Formulas0097ReturnReasonAnalysisService">
<!-- 导出:客户退货原因分析表 -->
<excel name="exportReturnReasonAnalysis" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="客户退货原因分析表", HEADER=true,
MAPPING={"a":"退货原因","b":"退货数量","c":"退货金额","d":"数量占比","e":"责任部门"},
FORMAT={"b":"#,##0","c":"#,##0.00","d":"0.00%"},
FORMULAS={
D2:'B2/B7',D3:'B3/B7',D4:'B4/B7',D5:'B5/B7',D6:'B6/B7',
B7:'SUM(B2:B6)',C7:'SUM(C2:C6)',D7:'SUM(D2:D6)'
},
STYLE={
ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
D4:{color:red,bold:true}
}
]]>
</excel>
<!-- 导入解析:读取导出的客户退货原因分析表 -->
<excel name="importReturnReasonAnalysis" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="客户退货原因分析表", HEADER=true,
MAPPING={"退货原因":"returnReason","退货数量":"returnQuantity","退货金额":"returnAmount","数量占比":"quantityRatio","责任部门":"responsibleDepartment"}
]]>
</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.