Export "Purchase Price Comparison Analysis" to Excel with Java (Formula & Calculated Reports)
Formula & Calculated ReportsScene 0045
Compares supplier quotes with the internal benchmark price per material, computing Price Difference = Quote - Benchmark and Price Difference Rate = Price Difference / Benchmark per row, with quotes above the benchmark marked in red and the last row giving the average price and average price difference rate. Implemented declaratively in pure XML — no Java business code required.

About this demo
This demo exports the "Purchase Price Comparison Analysis" workbook: 1 data sheet(s); MAPPING over 7 columns (物料名称, 供应商, 基准价, 报价, 价差, 价差率, 价格评定), 16 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportPurchasePriceCompare() 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 (63 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 Purchase Price Comparison Analysis to Excel with Java
Output specs
- Scene no.
- 0045
- Group
- Formula & Calculated Reports
- Config lines
- 36
- Source lines
- 63
Java source · Formulas0045PurchasePriceCompareDemo.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 Formulas0045PurchasePriceCompareDemo {
private static final String XML = "jquick/biz/formulas/0045_formulas_purchase-price-compare.xml";
/** 导出:纯 XML 完成价差、价差率、均价汇总与高于基准价标红。 */
@Test
public void exportPurchasePriceCompare() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
rows.add(quote("304 不锈钢板", "甲供", 120, 132, "偏高"));
rows.add(quote("铝型材", "乙供", 85, 80, "偏低"));
rows.add(quote("轴承钢", "丙供", 60, 66, "偏高"));
rows.add(quote("铜排", "甲供", 200, 195, "偏低"));
rows.add(quote("工程塑料", "乙供", 45, 47, "持平"));
// 均价占位行:两列均价、平均价差与价差率留给 FORMULAS
JQuickRow avg = new JQuickRow();
avg.put("a", "均价");
avg.put("b", null);
avg.put("c", null);
avg.put("d", null);
avg.put("e", null);
avg.put("f", null);
avg.put("g", null);
rows.add(avg);
File out = new File("D:/test/excel/biz/formulas/0045_formulas_purchase-price-compare.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("royalGold", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0045PurchasePriceCompareService service = factory.createApi(Formulas0045PurchasePriceCompareService.class);
service.exportPurchasePriceCompare("field", "value");
}
}
/** 构造一条报价行(a=物料,b=供应商,c=基准价,d=报价,g=价格评定)。 */
private static JQuickRow quote(String materialName, String supplier, double basePrice, double quotedPrice, String rating) {
JQuickRow row = new JQuickRow();
row.put("a", materialName);
row.put("b", supplier);
row.put("c", basePrice);
row.put("d", quotedPrice);
row.put("e", null);
row.put("f", null);
row.put("g", rating);
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("royalGold", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0045PurchasePriceCompareService service = factory.createApi(Formulas0045PurchasePriceCompareService.class);XML config · 0045_formulas_purchase-price-compare.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.Formulas0045PurchasePriceCompareService">
<!-- 导出:采购价格对比分析表 -->
<excel name="exportPurchasePriceCompare" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="采购价格对比分析", HEADER=true,
MAPPING={"a":"物料名称","b":"供应商","c":"基准价","d":"报价","e":"价差","f":"价差率","g":"价格评定"},
FORMAT={"c":"#,##0.00","d":"#,##0.00","e":"#,##0.00","f":"0.00%"},
FORMULAS={
E2:'D2-C2',F2:'E2/C2',
E3:'D3-C3',F3:'E3/C3',
E4:'D4-C4',F4:'E4/C4',
E5:'D5-C5',F5:'E5/C5',
E6:'D6-C6',F6:'E6/C6',
C7:'AVERAGE(C2:C6)',D7:'AVERAGE(D2:D6)',E7:'D7-C7',F7:'E7/C7'
},
STYLE={
ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
F4:{color:red,bold:true}
}
]]>
</excel>
<!-- 导入解析:读取导出的采购价格对比分析表 -->
<excel name="importPurchasePriceCompare" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="采购价格对比分析", HEADER=true,
MAPPING={"物料名称":"materialName","供应商":"supplier","基准价":"basePrice","报价":"quotedPrice","价差":"priceDiff","价差率":"diffRate","价格评定":"priceRating"}
]]>
</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.