POI解析Word2007(.docx)跨行跨列并生成HTML内容标签

本文介绍如何使用Apache POI库解析Word2007(.docx)文档,提取表格数据,并转换成HTML表格格式。重点在于处理表格的跨行和跨列特性,以保持原始布局。

POI解析Word2007(.docx)并生成HTML内容标签

前言

查阅一些资料后,经过不断地尝试和终于弄出想要的结果了。一些内容好多帖子都一样,实在看不出谁是原创,对于内容也给了一些灵感吧,那些都是实现读取之后又在转入另一个Word中,或者直接就把字符串内容拿出来。要么没有直接看到内容,要么与原Word中表格差异太大。
简单的读取就不阐述了,下面说一下我的吧,主要是读出来生成相应的<table></table>标签,便于使用和展示。但是并没有读取表格的详细样式配置。实际只需要读取内容以及单元格的跨行跨列即可。

一、原理

首先docdocx是现实不一样的,因此在读取的时候需要分开写。在**Word2007(.docx)**文档中的解析中,先是将文档当做.zip的压缩文件解压处理,其中包含一些.xml文件,其中就包含了文档中的内容以及样式参数。跨行跨列的xml信息获取方式不同。

1.1 跨行getVMerge

以下是我举得一个例子,是一个单元格的信息,其中w:val="restart" 表示此单元格为跨行信息。一般单元格不存在跨行时无此xml信息,对象为null。存在此xml信息并且存在w:val="restart"信息时为跨行首行,若存在此xml信息但并没有w:val="restart"信息时为跨行非首行。根据此信息加上自己的处理即可

<xml-fragment w:val="restart" 
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" 
xmlns:v="urn:schemas-microsoft-com:vml" 
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" 
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" 
xmlns:w10="urn:schemas-microsoft-com:office:word" 
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" 
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" 
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" 
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" 
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" 
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" 
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
/>

1.2 跨列getGridSpan

对于跨列相对容易,w:val="2"这个值就说跨两列。不存在垮列时则无此xml信息。

<xml-fragment w:val="2" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"/>

二、实现

以下代码只是实例,实际不可能只有docx还会涉及doc、PDF、WPS等多种文档。写好接口做好不同实现就好。我的方法并不是最好的,只是根据想法先实现了出来,测试了不同复杂表格均可以表现出来。逻辑也都注释了,一目了然,代码直接用就可以。POI的jar就不多说了。

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

import java.io.FileInputStream;
import java.util.List;

/*
 * 使用POI库对word进行处理的工具类
 */
public class DocxPOIUtils {
   
   
	public static void main(String[] args) throws Exception {
   
   
		String sourceFile = "D:\\JetBrains\\Cloud\\测试表格.docx";
		getAllTable2(sourceFile);
	}
	public 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值