项目需求:需要将数据导出成word文档,但是必须是按照一定的格式。这里使用了一个小插件,是基于poi的再次封装:https://github.com/Sayi/poi-tl/

1.maven引入包

<!-- poi  Excel、Word操作-->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.4.2</version>
        </dependency>

2.编写测试来使用poi-tl

首先看一下模板和对应的效果:

主要的java代码如下:

package com.justcs.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.junit.Before;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordDemoTest {

    // 数据准备
    Map<String, Object> data = new HashMap<String, Object>();

    @Before
    public void init() {
        data.put("confname", "测试会议");
        data.put("years", "2019");
        data.put("semester", "2018-2019下半学期");
        data.put("weeksno", "13");
        data.put("leadername", "陈鹏");
        data.put("cfdatetime", "2019年5月21日");
        // 上次会议
        List<RowRenderData> issueLastmeetList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            RowRenderData rowdata = RowRenderData.build(String.valueOf(1 + i), "上次会议" + i);
            issueLastmeetList.add(rowdata);
        }
        // 本次会议
        List<RowRenderData> issueCurmeetList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            RowRenderData rowdata = RowRenderData.build(String.valueOf(1 + i), "本次会议" + i);
            issueCurmeetList.add(rowdata);
        }
        // 建议解决方案
        List<RowRenderData> suggestionList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            RowRenderData rowdata = RowRenderData.build(String.valueOf(1 + i), "建议解决方案" + i);
            suggestionList.add(rowdata);
        }
        DetailTable detailTable = new DetailTable();
        detailTable.setIssueLast(issueLastmeetList);
        detailTable.setIssuecur(issueCurmeetList);
        detailTable.setSuggestion(suggestionList);

        data.put("detail_table", detailTable);
    }

    @Test
    public void testWord() throws IOException {

        Configure config = Configure.newBuilder().customPolicy("detail_table", new DetailTablePolicy()).build();
        XWPFTemplate template = XWPFTemplate.compile("D:\\java_project\\conferencem\\src\\main\\resources\\template\\template.docx", config).render(data);

        FileOutputStream out = new FileOutputStream("C:\\Users\\chenpeng\\Desktop\\out_template.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }
}

class DetailTablePolicy extends DynamicTableRenderPolicy {

    private static final int lastissue = 2;

    @Override
    public void render(XWPFTable xwpfTable, Object data) {
        if (null == data) return;
        DetailTable detailData = (DetailTable) data;

        List<RowRenderData> issueLastmeetList = (List<RowRenderData>) detailData.getIssueLast();
        List<RowRenderData> issueCurmeets = (List<RowRenderData>) detailData.getIssuecur();
        List<RowRenderData> suggestionList = (List<RowRenderData>) detailData.getSuggestion();
        // 上次
        if (issueLastmeetList != null && issueLastmeetList.size() > 0) {
            int lastrowindex = lastissue;
            xwpfTable.removeRow(lastissue);

            for (int i = 0; i < issueLastmeetList.size(); i++) {
                XWPFTableRow insertNewTableRow = xwpfTable.insertNewTableRow(lastissue);
                for (int j = 0; j < 2; j++) {
                    insertNewTableRow.createCell();
                }
                // 渲染单行获取明细数据
                MiniTableRenderPolicy.renderRow(xwpfTable, lastissue, issueLastmeetList.get(i));
            }
        }

        // 本次
        if (issueCurmeets != null && issueCurmeets.size() > 0) {
            int currowindex = issueLastmeetList.size() + 3;
            xwpfTable.removeRow(currowindex);
            for (int i = 0; i < issueCurmeets.size(); i++) {
                XWPFTableRow insertNewTableRow = xwpfTable.insertNewTableRow( currowindex);
                for (int j = 0; j < 2; j++) {
                    insertNewTableRow.createCell();
                }
                // 渲染单行获取明细数据
                MiniTableRenderPolicy.renderRow(xwpfTable, currowindex, issueCurmeets.get(i));
            }
        }

        // 建议
        if (suggestionList != null && suggestionList.size() > 0) {
            int sugindex = issueLastmeetList.size()+issueCurmeets.size()+4;
            xwpfTable.removeRow(sugindex);
            for (int i = 0; i < suggestionList.size(); i++) {
                XWPFTableRow insertNewTableRow = xwpfTable.insertNewTableRow(sugindex);
                for (int j = 0; j < 2; j++) {
                    insertNewTableRow.createCell();
                }
                // 渲染单行获取明细数据
                MiniTableRenderPolicy.renderRow(xwpfTable, sugindex, suggestionList.get(i));
            }
        }
    }
}

 

Logo

前往低代码交流专区

更多推荐