POI实现Word动态行列数据的导出

1 篇文章 0 订阅
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;


/**
 * @program: dataview.api
 * @description:
 * @author: wangy
 * @create: 2021-07-20 13:40
 **/
public class ExportWordUtil {
    /**
     * 动态导出word数据方法
     * @param savePath    保存路径
     * @param tab    标签头
     * @param cname    中文字段名
     * @param infos    内容数据
     * @throws Exception
     */
    public static void createSimpleTable(String savePath,String tab,List<String> cname,List<List<String>> infos) throws Exception {
        XWPFDocument xdoc = new XWPFDocument();
        XWPFParagraph xp = xdoc.createParagraph();
        XWPFRun r1 = xp.createRun();
        r1.setText(tab);
        r1.addBreak(); // 换行
        r1.setFontFamily("宋体");
        r1.setFontSize(16);
        r1.setTextPosition(10);
        r1.setBold(true);
        //左对齐
        xp.setAlignment(ParagraphAlignment.LEFT);

        // 表格最多的列数	根据得到的中文字段数量得到最多列
        Integer col_total_count = cname.size();
        // 需要创建的总条数	根据得到的内容得到最多行
        Integer data_count = infos.size()+1;

        XWPFTable xTable = xdoc.createTable(1, col_total_count);

        CTTbl ttbl = xTable.getCTTbl();
        CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl
                .getTblPr();
        CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr
                .addNewTblW();
        tblWidth.setW(new BigInteger("8600"));
        tblWidth.setType(STTblWidth.DXA);

        // 创建表头数据
        int i = 0;
        xTable.getRow(i).setHeight(500);
        //循环表头信息
        for (int i1 = 0; i1 < cname.size(); i1++) {
            setCellText(xdoc, xTable.getRow(i).getCell(i1), cname.get(i1), "FFFFFF", getCellWidth(0));
        }
        // 创建表格内容
        i++;
        for (int i2 = i; i2 < data_count; i2++) {
            XWPFTableRow row = xTable.insertNewTableRow(i2);
            row.setHeight(450);
            for (int j = 0, j2 = 0; j < col_total_count; j++, j2++) {
                XWPFTableCell cell = row.createCell();
                CTTc cttc = cell.getCTTc();
                CTTcPr cellPr = cttc.addNewTcPr();
                cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                cellPr.addNewTcW().setW(BigInteger.valueOf(getCellWidth(j2)));
                cell.setText(infos.get(i2-1).get(j));
            }
        }
        FileOutputStream fos = new FileOutputStream(savePath);
        xdoc.write(fos);
        fos.close();
    }

    /**
     * 设置表头内容
     *
     * @param xDocument
     * @param cell
     * @param text
     * @param bgcolor
     * @param width
     */
    private static void setCellText(XWPFDocument xDocument, XWPFTableCell cell,
                                    String text, String bgcolor, int width) {
        CTTc cttc = cell.getCTTc();
        CTTcPr cellPr = cttc.addNewTcPr();
        cellPr.addNewTcW().setW(BigInteger.valueOf(width));
        cell.setColor(bgcolor);
        cell.setVerticalAlignment(XWPFVertAlign.CENTER);
        CTTcPr ctPr = cttc.addNewTcPr();
        ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
        cell.setText(text);
    }

    /**
     * 设置列宽
     *
     * @param index
     * @return
     */
    private static int getCellWidth(int index) {
        int cwidth = 1000;
        if (index == 0) {
            cwidth = 2100;
        } else if (index == 1) {
            cwidth = 2100;
        } else if (index == 2) {
            cwidth = 2100;
        } else if (index == 3) {
            cwidth = 2100;
        }
        return cwidth;
    }

    public static  void main(String[] args){
        String savePath = "d:/"+ System.currentTimeMillis() + ".docx";
        String tab = "第一个表";
        List<String> cname = new ArrayList<>();
        cname.add("第一列");
        cname.add("第二列");
        List<List<String>> infos = new ArrayList<>();
        List<String> info1 = new ArrayList<>();
        List<String> info2 = new ArrayList<>();

        info1.add("张三");
        info1.add("18");

        info2.add("李四");
        info2.add("28");

        infos.add(info1);
        infos.add(info2);
        try {
            ExportWordUtil.createSimpleTable(savePath,tab,cname,infos);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

pom.xml

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.1</version>
        </dependency>


 

导出效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值