Export "Top N Product Sales Ranking" to Excel with Java (Formula & Calculated Reports)
Formula & Calculated ReportsScene 0008
Ranking of the top N products by sales volume, showing the sales share per row with a total row at the end. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Top N Product Sales Ranking" 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 exportProductSalesTopN() 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 (58 lines, runnable as-is)
- The complete XML config (30 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 Top N Product Sales Ranking to Excel with Java
Output specs
- Scene no.
- 0008
- Group
- Formula & Calculated Reports
- Config lines
- 30
- Source lines
- 58
Java source · Formulas0008ProductSalesTopNDemo.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 Formulas0008ProductSalesTopNDemo {
private static final String XML = "jquick/biz/formulas/0008_formulas_product-sales-topn.xml";
/** 导出:Top 5 产品 + 1 行合计,回读校验占比公式与合计行样式。 */
@Test
public void exportProductSalesTopN() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
rows.add(detail(1, "智能手表", 1200, 360000));
rows.add(detail(2, "无线耳机", 980, 294000));
rows.add(detail(3, "蓝牙音箱", 760, 228000));
rows.add(detail(4, "平板电脑", 520, 208000));
rows.add(detail(5, "智能手环", 410, 123000));
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/0008_formulas_product-sales-topn.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("charcoal", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0008ProductSalesTopNService service = factory.createApi(Formulas0008ProductSalesTopNService.class);
service.exportProductSalesTopN("field", "value");
}
}
/** 构造一条产品销量明细(a=排名,b=产品名称,c=销量,d=销售额;e=销售占比由 FORMULAS 计算,占位保列)。 */
private static JQuickRow detail(int rank, String name, int qty, double amount) {
JQuickRow row = new JQuickRow();
row.put("a", rank);
row.put("b", name);
row.put("c", qty);
row.put("d", amount);
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("charcoal", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0008ProductSalesTopNService service = factory.createApi(Formulas0008ProductSalesTopNService.class);XML config · 0008_formulas_product-sales-topn.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.Formulas0008ProductSalesTopNService">
<!-- 导出:产品销量 Top N 排行 -->
<excel name="exportProductSalesTopN" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="产品销量TopN", HEADER=true,
MAPPING={"a":"排名","b":"产品名称","c":"销量(件)","d":"销售额(元)","e":"销售占比"},
FORMAT={"c":"#,##0","d":"#,##0.00","e":"0.00%"},
FORMULAS={
E2:'D2/SUM(D2:D6)',E3:'D3/SUM(D2:D6)',E4:'D4/SUM(D2:D6)',
E5:'D5/SUM(D2:D6)',E6:'D6/SUM(D2:D6)',
C7:'SUM(C2:C6)',D7:'SUM(D2:D6)',E7:'SUM(E2:E6)'
},
STYLE={ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}}
]]>
</excel>
<!-- 导入解析:读取导出的产品销量排行 -->
<excel name="importProductSalesTopN" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="产品销量TopN", HEADER=true,
MAPPING={"排名":"rank","产品名称":"name","销量(件)":"qty","销售额(元)":"amount","销售占比":"ratio"}
]]>
</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.