java生成word动态表格-使用itext

添加依赖

<dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext</artifactId>
                <version>2.1.7</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-asian</artifactId>
                <version>5.2.0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.lowagie/itext-rtf -->
            <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext-rtf</artifactId>
                <version>2.1.7</version>
</dependency>

编写工具类

package com.xk.dw.modelling.reverseProject.utils;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.rtf.RtfWriter2;
import com.xk.dw.modelling.reverseProject.model.dto.Column;
import com.xk.dw.modelling.reverseProject.model.dto.TableDetailDto;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.io.*;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

public class WordUtils {

    /**
     * @author: 执着(zlm)
     * @Date: 2020/11/12 10:16
     * @Description: 将表信息变成word字节流的形式
     */
    public static byte[] processWord(List<TableDetailDto> tableDetailDtoList, String title) throws DocumentException {
        Document document = new Document(PageSize.A4);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            // 写入文件信息
            RtfWriter2.getInstance(document, outputStream);
            document.open();
            Paragraph ph = new Paragraph();
            Font f = new Font();
            Paragraph p = new Paragraph(title, new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
            p.setAlignment(1);
            document.add(p);
            ph.setFont(f);
            for (int i = 0; i < tableDetailDtoList.size(); i++) {
                TableDetailDto tableDetailDto = tableDetailDtoList.get(i);
                String tableName = tableDetailDto.getName();
                String remarks = tableDetailDto.getRemarks();
                String tableTitle = String.format("%s. 表名称:%s%s", (i + 1), tableName, StringUtils.isEmpty(remarks) ? "" : String.format("(%s)", remarks));
                Table table = new Table(8);
                document.add(new Paragraph(""));
                table.setBorderWidth(1);
                table.setPadding(0);
                table.setSpacing(0);
                //设置表格格子的宽度
                table.setWidths(new int[]{50, 120, 100, 50, 50, 80, 80, 150});
                //添加表头的元素,并设置表头背景的颜色
                Color color = new Color(176, 196, 222);
                //表头
                String[] headersStr = {"编号", "字段名", "类型", "长度", "精度", "是否非空", "是否主键", "注释"};
                //添加表头到表格中
                Stream.of(headersStr).forEach(header->addCell(table,new Cell(header), color));
                table.endHeaders();
                List<Column> fileds = tableDetailDto.getColumns();
                // 表格的主体
                for (int k = 0; k < fileds.size(); k++) {
                    Column column = fileds.get(k);
                    //精度
                    addContent(table, (k + 1) + "");
                    addContent(table, column.getName());
                    addContent(table, column.getColumnDataType().getName().toLowerCase());
                    addContent(table, ParseTableUtils.getTrueLength(column.getSize()));
                    addContent(table, ParseTableUtils.getTruePrecision(column.getColumnDataType().getPrecision()));
                    addContent(table, column.isNullable() ? "否" : "是");
                    addContent(table, ParseTableUtils.isPk(tableDetailDto.getPrimaryKey(), column) ? "是" : "否");
                    addContent(table, fileds.get(k).getRemarks());
                }
                Paragraph paragraph = new Paragraph(tableTitle);
                //写入表说明
                document.add(paragraph);
                //生成表格
                document.add(table);
            }
            document.close();
            return outputStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 添加表头到表格
     *
     * @param table
     * @param cell
     * @param color
     */
    private static void addCell(Table table, Cell cell, Color color) {
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(color);
        table.addCell(cell);
    }

    /**
     * 添加内容到表格
     *
     * @param table
     * @param content
     */
    private static void addContent(Table table, String content) {
        Cell cell = new Cell(content);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值