Export "Product Sales Monthly Trend" to Excel with Java (Formula & Calculated Reports)
Formula & Calculated ReportsScene 0065
Registers the sales quantity and unit price of a single product by month, computing Sales Amount = Quantity × Unit Price per row and the Month-over-Month Growth Rate = (Current Sales - Previous Sales) / Previous Sales month by month, with the last row summarizing quantity and sales amount. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Product Sales Monthly Trend" workbook: 1 data sheet(s); MAPPING over 5 columns (月份, 销售数量, 单价, 销售额, 环比增长率), 12 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportProductSalesTrend() 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 (60 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 Product Sales Monthly Trend to Excel with Java
Output specs
- Scene no.
- 0065
- Group
- Formula & Calculated Reports
- Config lines
- 32
- Source lines
- 60
Java source · Formulas0065ProductSalesTrendDemo.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 Formulas0065ProductSalesTrendDemo {
private static final String XML = "jquick/biz/formulas/0065_formulas_product-sales-trend.xml";
/** 导出:纯 XML 完成销售额、环比增长率与合计。 */
@Test
public void exportProductSalesTrend() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
// d=销售额、e=环比增长率 留空,由 FORMULAS 计算(首月无环比)
rows.add(month("2026-01", 1200, 199.00));
rows.add(month("2026-02", 1500, 199.00));
rows.add(month("2026-03", 1350, 205.00));
rows.add(month("2026-04", 1800, 205.00));
rows.add(month("2026-05", 2000, 210.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/0065_formulas_product-sales-trend.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("oceanBlue", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0065ProductSalesTrendService service = factory.createApi(Formulas0065ProductSalesTrendService.class);
service.exportProductSalesTrend("field", "value");
}
}
/** 构造一条月度明细(a=月份,b=数量,c=单价,d=销售额,e=环比增长率)。 */
private static JQuickRow month(String month, int quantity, double unitPrice) {
JQuickRow row = new JQuickRow();
row.put("a", month);
row.put("b", quantity);
row.put("c", unitPrice);
row.put("d", null);
row.put("e", 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);
Formulas0065ProductSalesTrendService service = factory.createApi(Formulas0065ProductSalesTrendService.class);XML config · 0065_formulas_product-sales-trend.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.Formulas0065ProductSalesTrendService">
<!-- 导出:产品销售月度趋势表 -->
<excel name="exportProductSalesTrend" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="产品销售月度趋势表", HEADER=true,
MAPPING={"a":"月份","b":"销售数量","c":"单价","d":"销售额","e":"环比增长率"},
FORMAT={"b":"#,##0","c":"#,##0.00","d":"#,##0.00","e":"0.00%"},
FORMULAS={
D2:'B2*C2',D3:'B3*C3',D4:'B4*C4',D5:'B5*C5',D6:'B6*C6',
E3:'(D3-D2)/D2',E4:'(D4-D3)/D3',E5:'(D5-D4)/D4',E6:'(D6-D5)/D5',
B7:'SUM(B2:B6)',D7:'SUM(D2:D6)'
},
STYLE={
ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}
}
]]>
</excel>
<!-- 导入解析:读取导出的月度趋势表 -->
<excel name="importProductSalesTrend" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="产品销售月度趋势表", HEADER=true,
MAPPING={"月份":"month","销售数量":"quantity","单价":"unitPrice","销售额":"salesAmount","环比增长率":"growthRate"}
]]>
</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.