Skip to content

Export "Employee Information Submission Form" to Excel with Java (Data Validation)

JQuick-ExcelData Validation

Data ValidationScene 0012

Employee-submitted information (employee ID / name / department / age / hire date / monthly salary / email / mobile), with average age and total monthly salary in the last row; each submitted field is validated on import. Java only groups and pre-processes the rows; all statistics and layout are declared in XML.

Download the Excel fileView raw XML configBack to all demos

Employee Information Submission Form — Data Validation
Full screenshot of the exported xlsx

About this demo

This demo exports the "Employee Information Submission Form" workbook: 1 data sheet(s); MAPPING over 8 columns (工号, 姓名, 部门, 年龄, 入职日期, 月薪, 邮箱, 手机号), 4 formula(s), 2 style rule(s). On the Java side it builds 4 data rows and calls exportEmployeeInfo() once — everything else is driven by the config.

It belongs to the "Data Validation" group: VALIDATION form validation: an input template is exported and every field is validated when it is imported back.

What you can copy from this page

  • The complete Java test class (69 lines, runnable as-is)
  • The complete XML config (38 lines: mapping, formulas, styles, charts and every other rule)
  • The exported xlsx file (real output, comparable with the preview above)

Related searches: Java Excel export validation, Java validate imported Excel, Java Excel form template validation, export Employee Information Submission Form to Excel with Java

Output specs

Scene no.
0012
Group
Data Validation
Config lines
38
Source lines
69

Java source · Validation0012EmployeeInfoReportDemo.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.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Validation0012EmployeeInfoReportDemo {

    private static final String XML = "jquick/biz/validation/0012_validation_employee-info-report.xml";

    /** 导出:3 名员工 + 1 行统计,回读校验统计公式与统计行样式。 */
    @Test
    public void exportEmployeeInfo() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(employee("EMP001", "张三", "研发部", 26, "2023-03-15", 12000.00, "[email protected]", "13800000001"));
        rows.add(employee("EMP002", "李四", "市场部", 31, "2021-07-01", 15000.00, "[email protected]", "13900000002"));
        rows.add(employee("EMP003", "王五", "财务部", 29, "2022-11-20", 13000.00, "[email protected]", "13700000003"));
        rows.add(statRow());

        File out = new File("D:/test/excel/biz/validation/0012_validation_employee-info-report.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("bronze", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Validation0012EmployeeInfoReportService service = factory.createApi(Validation0012EmployeeInfoReportService.class);
            service.exportEmployeeInfo("field", "value");
        }

    }

    /** 构造一名员工填报行(含全部映射列)。 */
    private static JQuickRow employee(String code, String name, String dept, int age,
                                      String hireDate, double salary, String email, String phone) throws Exception {
        JQuickRow row = new JQuickRow();
        row.put("a", code);
        row.put("b", name);
        row.put("c", dept);
        row.put("d", age);
        row.put("e", new SimpleDateFormat("yyyy-MM-dd").parse(hireDate));
        row.put("f", salary);
        row.put("g", email);
        row.put("h", phone);
        return row;
    }

    /** 统计占位行:只写「统计」文字,人数 / 平均年龄 / 薪资合计三列留给 FORMULAS。 */
    private static JQuickRow statRow() {
        JQuickRow row = new JQuickRow();
        row.put("a", "统计");
        row.put("b", null);
        row.put("c", null);
        row.put("d", null);
        row.put("e", null);
        row.put("f", null);
        row.put("g", null);
        row.put("h", 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("bronze", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Validation0012EmployeeInfoReportService service = factory.createApi(Validation0012EmployeeInfoReportService.class);

XML config · 0012_validation_employee-info-report.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.validation.Validation0012EmployeeInfoReportService">

    <!-- 导出:员工信息填报表 -->
    <excel name="exportEmployeeInfo" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="员工信息填报表", HEADER=true,
                MAPPING={"a":"工号","b":"姓名","c":"部门","d":"年龄","e":"入职日期","f":"月薪","g":"邮箱","h":"手机号"},
                FORMAT={"d":"0","e":"yyyy-MM-dd","f":"#,##0.00"},
                FORMULAS={D5:'AVERAGE(D2:D4)',F5:'SUM(F2:F4)'},
                STYLE={ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}}
        ]]>
    </excel>

    <!-- 导入解析:读取填报结果并逐项校验(校验不通过时抛出异常,异常信息即规则 msg) -->
    <excel name="importEmployeeInfo" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="员工信息填报表", HEADER=true,
                MAPPING={"工号":"code","姓名":"name","部门":"dept","年龄":"age","入职日期":"hireDate","月薪":"salary","邮箱":"email","手机号":"phone"},
                VALIDATION={
                    COL A:{max_length{required:true,msg:'工号最长10位',map:{maxLength:10}}},
                    A2:A4:{min_length{required:true,msg:'工号至少4位',map:{minLength:4}}},
                    B2:B4:{min_length{required:true,msg:'姓名至少2个字',map:{minLength:2}}},
                    C2:C4:{dict{required:true,msg:'部门必须在字典内(研发部/市场部/财务部)',map:{'研发部':'研发部','市场部':'市场部','财务部':'财务部'}}},
                    D2:D4:{integer{required:true,msg:'年龄必须是整数'}},
                    D2:D4:{min_value{required:true,msg:'年龄不能小于18',map:{minValue:18}}},
                    D2:D4:{max_value{required:true,msg:'年龄不能超过60',map:{maxValue:60}}},
                    G2:G4:{email{required:true,msg:'邮箱格式无效'}},
                    H2:H4:{mobile{required:true,msg:'手机号格式无效'}},
                    H2:H4:{regex{required:true,msg:'手机号必须是11位且以1[3-9]开头',map:{pattern:'^1[3-9][0-9]{9}$'}}}
                }
        ]]>
    </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.