Export "Product Gross Profit Contribution Analysis" to Excel with Java (Formula & Calculated Reports)
Formula & Calculated ReportsScene 0095
Registers sales and cost per product, computing Gross Profit = Sales - Cost, Gross Margin = Gross Profit / Sales and Contribution Share = Gross Profit / Total Gross Profit, with the last row summarizing sales, cost and gross profit; the product with the lowest gross margin is marked in red. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Product Gross Profit Contribution Analysis" workbook: 1 data sheet(s); MAPPING over 6 columns (产品名称, 销售额, 成本, 毛利, 毛利率, 贡献占比), 22 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportProductMarginAnalysis() 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 (62 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 formulas, Java Excel SUM formula export, Apache POI write formula example, Java computed column export, export Product Gross Profit Contribution Analysis to Excel with Java
Output specs
- Scene no.
- 0095
- Group
- Formula & Calculated Reports
- Config lines
- 36
- Source lines
- 62
Java source · Formulas0095ProductMarginAnalysisDemo.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 Formulas0095ProductMarginAnalysisDemo {
private static final String XML = "jquick/biz/formulas/0095_formulas_product-margin-analysis.xml";
/** 导出:纯 XML 完成逐行毛利、毛利率、贡献占比与合计。 */
@Test
public void exportProductMarginAnalysis() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
// d/e/f 留空,由 FORMULAS 计算
rows.add(product("A产品", 500000.00, 350000.00));
rows.add(product("B产品", 300000.00, 240000.00));
rows.add(product("C产品", 450000.00, 400000.00));
rows.add(product("D产品", 200000.00, 120000.00));
rows.add(product("E产品", 250000.00, 180000.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);
rows.add(total);
File out = new File("D:/test/excel/biz/formulas/0095_formulas_product-margin-analysis.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("sakuraPink", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0095ProductMarginAnalysisService service = factory.createApi(Formulas0095ProductMarginAnalysisService.class);
service.exportProductMarginAnalysis("field", "value");
}
}
/** 构造一款产品(a=产品,b=销售额,c=成本,d/e/f 留空)。 */
private static JQuickRow product(String productName, double salesAmount, double costAmount) {
JQuickRow row = new JQuickRow();
row.put("a", productName);
row.put("b", salesAmount);
row.put("c", costAmount);
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("sakuraPink", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0095ProductMarginAnalysisService service = factory.createApi(Formulas0095ProductMarginAnalysisService.class);XML config · 0095_formulas_product-margin-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.Formulas0095ProductMarginAnalysisService">
<!-- 导出:产品毛利贡献分析表 -->
<excel name="exportProductMarginAnalysis" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="产品毛利贡献分析表", HEADER=true,
MAPPING={"a":"产品名称","b":"销售额","c":"成本","d":"毛利","e":"毛利率","f":"贡献占比"},
FORMAT={"b":"#,##0.00","c":"#,##0.00","d":"#,##0.00","e":"0.00%","f":"0.00%"},
FORMULAS={
D2:'B2-C2',E2:'D2/B2',F2:'D2/D7',
D3:'B3-C3',E3:'D3/B3',F3:'D3/D7',
D4:'B4-C4',E4:'D4/B4',F4:'D4/D7',
D5:'B5-C5',E5:'D5/B5',F5:'D5/D7',
D6:'B6-C6',E6:'D6/B6',F6:'D6/D7',
B7:'SUM(B2:B6)',C7:'SUM(C2:C6)',D7:'B7-C7',E7:'D7/B7',F7:'SUM(F2:F6)'
},
STYLE={
ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
E4:{color:red,bold:true}
}
]]>
</excel>
<!-- 导入解析:读取导出的产品毛利贡献分析表 -->
<excel name="importProductMarginAnalysis" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="产品毛利贡献分析表", HEADER=true,
MAPPING={"产品名称":"productName","销售额":"salesAmount","成本":"costAmount","毛利":"grossProfit","毛利率":"grossMargin","贡献占比":"contributionRatio"}
]]>
</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.