POI操作Word

前提工作

<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);
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值