前提工作
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<-- 下面这两个不太清楚有什么用 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>1.4</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>
Word
表格
XWPFDocument document = new XWPFDocument(new FileInputStream(filePath));
// 获取表格集合
List<XWPFTable> tableList = document.getTables();
// 获取第一个表格
XWPFTable table = tableList.get(0);
// 文件写入
ByteArrayOutputStream bos = new ByteArrayOutputStream();
document.write(bos);
byte[] bytes = bos.toByteArray();
// 转为二进制字符
String bytesRes = StringUtils.bytesToHexString2(bytes);
// 关闭流...
修改表格样式
// 这一段还没明白什么意思
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString tblStyle = tblPr.addNewTblStyle();
tblStyle.setVal("StyleTable");
// 设置表格左边距,不是表格内容
CTTblWidth tblInd = table.getCTTbl().getTblPr().getTblInd();
if (Objects.isNull(tblInd)) {
tblInd = table.getCTTbl().getTblPr().addNewTblInd();
}
tblInd.setW(BigInteger.valueOf(200));
table.getCTTbl().getTblPr().setTblInd(tblInd);
// 创建一行
XWPFTableRow row = table.createRow();
XWPFTableCell cell = row.getCell(0);
// 设置表格的字体大小和内容
XWPFParagraph xwpfParagraph3 = cell.getParagraphs().get(0);
// 获取单元格的对齐方式
// ParagraphAlignment alignment = xwpfParagraph3.getAlignment();
// 设置单元格的对齐方式
// xwpfParagraph3.setAlignment(ParagraphAlignment.LEFT);
XWPFRun run1 = xwpfParagraph3.createRun();
run1.setFontSize(OfficeGetReturnController.FONT_SIZE);
run1.setText(detail.getSupplies().getName());
// 设置竖直居中
CTTcPr ctTcPr = row.getCell(0).getCTTc().addNewTcPr();
CTVerticalJc ctVerticalJc = ctTcPr.addNewVAlign();
ctVerticalJc.setVal(STVerticalJc.CENTER);
// 设置行高
CTTrPr ctTrPr = option.getCtRow().addNewTrPr();
CTHeight ctHeight = ctTrPr.addNewTrHeight();
ctHeight.setVal(BigInteger.valueOf(600));
// 合并单元格
POIUtils.mergeCellsHorizontal(表格名称, 行号, 开始的单元格, 结束的单元格);
工具类
package com.gangwantech.web.utils;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
public class POIUtils {
/**
* Word跨列合并单元格
*
* @param table table对象
* @param row 要合并的行
* @param fromCell 开始单元格的位置(从0开始)
* @param toCell 结束位置
*/
public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if (cellIndex == fromCell) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* Word跨列合并单元格
*
* @param table table对象
* @param col 要合并的列
* @param fromRow 开始行的位置(从0开始)
* @param toRow 结束位置
*/
public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if (rowIndex == fromRow) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
}