Java 导出「客户档案信息补录填报表」Excel 报表(数据校验)
数据校验场景 0067
汇总补录的客户档案(编号 / 名称 / 类型 / 信用等级 / 联系人 / 电话 / 年采购额 / 状态),末尾一行汇总年采购额;导入时对填报内容做规则校验。 纯 XML 声明式实现,无需编写 Java 业务代码。

关于这个示例
本示例导出「客户档案信息补录填报表」工作簿:1 个数据 sheet;以 客户编号、客户名称、客户类型、信用等级、联系人、联系电话、年采购额、建档状态 共 8 列字段建立 MAPPING 映射、2 条公式、1 处样式规则。Java 侧只构造 7 行数据并调用一次 exportCustomerProfileFiling(),其余全部由配置驱动。
这个示例属于「数据校验」分组:演示 VALIDATION 填报表校验:导出填报模板,导入时逐项校验内容是否合规。
可以直接抄走的内容
- 完整 Java 测试类(68 行,可直接运行)
- 完整 XML 配置(37 行,映射、公式、样式、图表等全部规则)
- 导出产物 xlsx(真实运行结果,可与上方预览图对照)
相关搜索:Java 导出 Excel 校验、Excel 导入数据校验 Java、Java Excel 填报模板 校验规则、Java 导出 客户档案信息补录填报表 Excel
输出规格
- 场景编号
- 0067
- 技术分组
- 数据校验
- 配置行数
- 37
- 源码行数
- 68
Java 源码 · Validation0067CustomerProfileFilingDemo.java
以下代码即生成上方 Excel 的完整测试类,可直接复制运行;Java 只负责构造数据,统计与样式逻辑全部在 XML 配置里。
package com.github.paohaijiao.biz.validation;
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 Validation0067CustomerProfileFilingDemo {
private static final String XML = "jquick/biz/validation/0067_validation_customer-profile-filing.xml";
/** 导出:纯 XML 完成年采购额汇总与合计行高亮。 */
@Test
public void exportCustomerProfileFiling() throws Exception {
List<JQuickRow> rows = new ArrayList<>();
rows.add(customer("C2601", "华新电子有限公司", "企业客户", "AAA", "张明", "13800138001", 1200000, "已建档"));
rows.add(customer("C2602", "恒通材料有限公司", "企业客户", "AA", "李强", "13900139002", 860000, "已建档"));
rows.add(customer("C2603", "佳美包装有限公司", "企业客户", "A", "王芳", "13700137003", 540000, "已建档"));
rows.add(customer("C2604", "个人客户赵敏", "个人客户", "A", "赵敏", "13600136004", 120000, "已建档"));
rows.add(customer("C2605", "个人客户陈杰", "个人客户", "B", "陈杰", "13500135005", 60000, "已建档"));
rows.add(customer("C2606", "天海物流有限公司", "企业客户", "AA", "周涛", "13400134006", 720000, "待审核"));
// 合计占位行:年采购额合计留给 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);
total.put("g", null);
total.put("h", null);
rows.add(total);
File out = new File("D:/test/excel/biz/validation/0067_validation_customer-profile-filing.xlsx");
try (OutputStream os = new FileOutputStream(out)) {
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("tropicalTeal", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Validation0067CustomerProfileFilingService service = factory.createApi(Validation0067CustomerProfileFilingService.class);
service.exportCustomerProfileFiling("field", "value");
}
}
/** 构造一条客户补录(a=客户编号,b=客户名称,c=客户类型,d=信用等级,e=联系人,f=电话,g=年采购额,h=建档状态)。 */
private static JQuickRow customer(String customerNo, String customerName, String customerType,
String creditLevel, String contact, String phone,
int annualAmount, String filingStatus) {
JQuickRow row = new JQuickRow();
row.put("a", customerNo);
row.put("b", customerName);
row.put("c", customerType);
row.put("d", creditLevel);
row.put("e", contact);
row.put("f", phone);
row.put("g", annualAmount);
row.put("h", filingStatus);
return row;
}
}最小调用(4 行)
核心就这四行:数据行交给解析器,解析器交给工厂,工厂生成 Service 接口,调用即产出 Excel。可直接复制到你的项目里。
// rows 由业务代码构造(List<JQuickRow>),os 为输出流,见下方完整源码
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("tropicalTeal", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Validation0067CustomerProfileFilingService service = factory.createApi(Validation0067CustomerProfileFilingService.class);XML 配置 · 0067_validation_customer-profile-filing.xml
格式(FORMAT)、公式(FORMULAS)、样式(STYLE)等统计逻辑全部声明在这份 XML 里,与 Java 代码解耦。下方为完整配置,可直接复制。
<?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.validation.Validation0067CustomerProfileFilingService">
<!-- 导出:客户档案信息补录填报表 -->
<excel name="exportCustomerProfileFiling" returnClass="void">
<![CDATA[
EXPORT WITH SHEET="客户档案信息补录填报表", HEADER=true,
MAPPING={"a":"客户编号","b":"客户名称","c":"客户类型","d":"信用等级","e":"联系人","f":"联系电话","g":"年采购额","h":"建档状态"},
FORMAT={"g":"0"},
FORMULAS={G8:'SUM(G2:G7)'},
STYLE={
ROW 8:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}
}
]]>
</excel>
<!-- 导入解析:读取补录结果并逐项校验(校验不通过时抛出异常,异常信息即规则 msg) -->
<excel name="importCustomerProfileFiling" returnClass="java.util.List">
<![CDATA[
IMPORT WITH SHEET="客户档案信息补录填报表", HEADER=true,
MAPPING={"客户编号":"customerNo","客户名称":"customerName","客户类型":"customerType","信用等级":"creditLevel","联系人":"contact","联系电话":"phone","年采购额":"annualAmount","建档状态":"filingStatus"},
VALIDATION={
A2:A7:{min_length{required:true,msg:'客户编号至少4位',map:{minLength:4}}},
B2:B7:{min_length{required:true,msg:'客户名称至少2个字',map:{minLength:2}}},
C2:C7:{dict{required:true,msg:'客户类型必须是(企业客户/个人客户)',map:{'企业客户':'企业客户','个人客户':'个人客户'}}},
D2:D7:{dict{required:true,msg:'信用等级必须在(AAA/AA/A/B)内',map:{'AAA':'AAA','AA':'AA','A':'A','B':'B'}}},
F2:F7:{mobile{required:true,msg:'联系电话格式无效'}},
G2:G7:{integer{required:true,msg:'年采购额必须是整数'}},
G2:G7:{min_value{required:true,msg:'年采购额不能为负数',map:{minValue:0}}}
}
]]>
</excel>
</excels>同分组示例
本页 Excel 产物、预览图、XML 配置与源码均由 JQuick-Excel 真实运行产出,可自由复制到你的项目中使用。