Skip to content

Export "Employee Master Data Code Value Transformation" to Excel with Java (Data Transformation)

JQuick-ExcelData Transformation

Data TransformationScene 0101

In the employee master data, gender / department / status are stored as internal code values and are converted to Chinese via a dictionary on export. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Employee Master Data Code Value Transformation — Data Transformation
Full screenshot of the exported xlsx

About this demo

This demo exports the "Employee Master Data Code Value Transformation" workbook: 1 data sheet(s); MAPPING over 7 columns (工号, 姓名, 性别, 部门, 入职日期, 状态, 年假). On the Java side it builds 6 data rows and calls exportEmployeeMasterData() once — everything else is driven by the config.

It belongs to the "Data Transformation" group: TRANSFORM field conversion: code-value mapping, date formatting and masking applied during export.

What you can copy from this page

  • The complete Java test class (73 lines, runnable as-is)
  • The complete XML config (26 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 data transform, Java Excel code value mapping, Java Excel date formatting export, export Employee Master Data Code Value Transformation to Excel with Java

Output specs

Scene no.
0101
Group
Data Transformation
Config lines
26
Source lines
73

Java source · Transform0101EmployeeMasterDataDemo.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.transform;

import com.github.paohaijiao.param.JContext;
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.HashMap;
import java.util.List;
import java.util.Map;

public class Transform0101EmployeeMasterDataDemo {

    private static final String XML = "jquick/biz/transform/0101_transform_employee-master-data.xml";

    /** 导出:内部码值经 TRANSFORM 字典转为中文,回读校验性别 / 部门 / 状态三列。 */
    @Test
    public void exportEmployeeMasterData() throws Exception {
        JContext ctx = new JContext();
        Map<String, Object> dict = new HashMap<>();
        // 性别码值
        dict.put("1", "男");
        dict.put("2", "女");
        // 部门码值
        dict.put("RD", "研发部");
        dict.put("MK", "市场部");
        dict.put("FN", "财务部");
        // 状态码值
        dict.put("A", "在职");
        dict.put("B", "离职");
        ctx.put("dict", dict);

        List<JQuickRow> rows = new ArrayList<>();
        rows.add(employee("E2601", "张伟", "1", "RD", "2019-03-01", "A", 6));
        rows.add(employee("E2602", "李娜", "2", "MK", "2020-06-15", "A", 8));
        rows.add(employee("E2603", "王强", "1", "FN", "2018-09-20", "B", 0));
        rows.add(employee("E2604", "刘洋", "2", "RD", "2021-01-10", "A", 5));
        rows.add(employee("E2605", "陈静", "1", "MK", "2017-11-05", "A", 12));
        rows.add(employee("E2606", "赵磊", "2", "FN", "2022-03-18", "A", 3));

        File out = new File("D:/test/excel/biz/transform/0101_transform_employee-master-data.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("navyBlue", ctx, rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Transform0101EmployeeMasterDataService service = factory.createApi(Transform0101EmployeeMasterDataService.class);
            service.exportEmployeeMasterData("field", "value");
        }

    }

    /** 构造一条员工主数据(c/d/f 为内部码值,导出时经字典转中文;e 为日期对象走 FORMAT)。 */
    private static JQuickRow employee(String no, String name, String sex, String dept,
                                      String hireDate, String status, int leaveDays) throws Exception {
        JQuickRow row = new JQuickRow();
        row.put("a", no);
        row.put("b", name);
        row.put("c", sex);
        row.put("d", dept);
        row.put("e", new SimpleDateFormat("yyyy-MM-dd").parse(hireDate));
        row.put("f", status);
        row.put("g", leaveDays);
        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("navyBlue", ctx, rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Transform0101EmployeeMasterDataService service = factory.createApi(Transform0101EmployeeMasterDataService.class);

XML config · 0101_transform_employee-master-data.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.transform.Transform0101EmployeeMasterDataService">

    <!-- 导出:内部码值 -> 中文 -->
    <excel name="exportEmployeeMasterData" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="员工主数据", HEADER=true,
                MAPPING={"a":"工号","b":"姓名","c":"性别","d":"部门","e":"入职日期","f":"状态","g":"年假"},
                FORMAT={"e":"yyyy-MM-dd","g":"0"},
                TRANSFORM={"c":trans(${dict},${c}),"d":trans(${dict},${d}),"f":trans(${dict},${f})}
        ]]>
    </excel>

    <!-- 导入解析:日期归一,其余字段按表头映射 -->
    <excel name="importEmployeeMasterData" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="员工主数据", HEADER=true,
                MAPPING={"工号":"employeeNo","姓名":"name","性别":"sex","部门":"dept","入职日期":"hireDate","状态":"status","年假":"leaveDays"},
                TRANSFORM={"hireDate":dateFormat(${hireDate},'yyyy-MM-dd')}
        ]]>
    </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.