添加依赖
<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);
}
}