java利用poi解析docx生成html

公司业务需要把world文档中编辑好的新闻(文字+图片)录入到CMS管理后台,生成一篇新闻发布。因为不能把图片直接复制粘贴到UEditor编辑器上,还要一个一个上传太麻烦。所以这里做了一个上传docx文件解析后,直接返回html正文放到前端编辑器继续编辑。
功能要求:
1.图片要下载到服务器指定位置,并把前端请求图片地址拼接到img标签的src上。
2.图片文字要按照顺序排列。
3.过滤掉超链接、其他图形等一般新闻不用的元素。
实现:

  1. maven最小依赖,3.17版本支持jdk1.6及以上。4版本需要jdk1.8及以上支持了
    	<groupId>org.apache.poi</groupId>
    		<artifactId>poi</artifactId>
    		<version>3.17</version>
		</dependency>
		<dependency>
    	<groupId>org.apache.poi</groupId>
    		<artifactId>poi-ooxml</artifactId>
   		 	<version>3.17</version>
		</dependency>
		<dependency>
    	<groupId>org.apache.poi</groupId>
    		<artifactId>poi-ooxml-schemas</artifactId>
    		<version>3.17</version>
		</dependency>

2.代码实现

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

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

public class AnalyzeDocx {

	public static void main(String[] args) throws Exception {
		String content = analyzeDocx("e://abc.docx");
		System.out.println(content);
	}

	public static String analyzeDocx(String path) throws Exception {

		StringBuilder sb = new StringBuilder();
		try (InputStream in = new FileInputStream(path); XWPFDocument xwpfDocument = new XWPFDocument(in);) {
			List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
			for (XWPFParagraph xwpfParagraph : paragraphs) {
				List<XWPFRun> runs = xwpfParagraph.getRuns();
				for (XWPFRun xwpfRun : runs) {
					CTR ctr = xwpfRun.getCTR();
					if(ctr.xmlText().contains("w:type=\"textWrapping\"")){
						sb.append("<br>");//段内换行
						continue;
					}
					XmlCursor newCursor = ctr.newCursor();
					newCursor.selectPath("./*");
					while (newCursor.toNextSelection()) {
						XmlObject object = newCursor.getObject();
						if (object instanceof CTText) {// 文字
							CTText ctText = (CTText) object;
							if (ctText.isSetSpace()) {
								continue;// 先不支持超链接
							}
							String text = ctText.getStringValue();
							if (text != null && text.length() > 0) {
								sb.append(text);
							}
						} else if (object instanceof CTDrawing) {// 图片1
							CTDrawing drawing = (CTDrawing) object;
							CTInline[] inlineArray = drawing.getInlineArray();
							for (CTInline ctInline : inlineArray) {
								CTGraphicalObject graphic = ctInline.getGraphic();
								XmlCursor newCursor2 = graphic.getGraphicData().newCursor();
								newCursor2.selectPath("./*");
								while (newCursor2.toNextSelection()) {
									XmlObject object2 = newCursor2.getObject();
									if (object2 instanceof CTPicture) {
										CTPicture picture = (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) object2;
										sb.append("<br>").append(
												imgHtml(xwpfDocument, picture.getBlipFill().getBlip().getEmbed()))
												.append("<br>");
									}
								}
							}
						}
					}
				}
				sb.append("<br>");// 分段
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	private static String imgHtml(XWPFDocument xwpfDocument, String blipID) {
		XWPFPictureData pictureData = xwpfDocument.getPictureDataByID(blipID);
		String imageName = pictureData.getFileName();
		String newfilename = System.currentTimeMillis() + imageName;
		byte[] bytev = pictureData.getData();
		try (FileOutputStream fos = new FileOutputStream("E:/" + newfilename);) {
			fos.write(bytev);// 此处保存图片后,变成可访问的http然后用<img>标签包裹
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "<img src='/rongmeitiapi/api/picture/find/image/20181107/d66ce5ffc18365a3dab1e46c484dfabb.jpeg'>";
	}

}

imgHtml方法需要把图片重命名后,变成前端可访问的连接,再去拼接img标签。我这边因为是测试,所以写死了img标签。
注意:这个只是处理正常的可视图片,对于emf类型的图片,不处理因为新闻也用不到。
如果需要捕获所有的,请参考https://www.jb51.net/article/132091.htm

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中,我们可以使用XSLT(eXtensible Stylesheet Language Transformations)来解析.docx文件。.docx是Microsoft Word中使用的一种文件格式,其中包含文本、图像、表格和其他内容。以下是解析.docx文件的基本步骤: 1. 导入必要的Java类库和工具包:首先,我们需要导入一些Java类库和工具包,以便能够读取和解析.docx文件。这些类库和工具包包括Apache POI、Apache POI OOXML、Apache POI XMLBeans等。 2. 创建一个XSLT模板:XSLT模板是用于解析和转换XML文档的样式表。我们需要创建一个XSLT模板来解析.docx文件,并从中提取所需的数据。 3. 加载.docx文件:使用Apache POI库的XWPFDocument类,我们可以加载.docx文件。XWPFDocument类提供了许多用于读取和操作.docx文件的方法。 4. 将.docx文件内容转换为XML:使用XWPFWordExtractor类,我们可以将.docx文件内容提取为纯文本。然后,我们可以将提取的文本转换为XML格式,以便更容易地进行解析和处理。 5. 应用XSLT模板:使用Javajavax.xml.transform包中的类,我们可以将XSLT模板应用于XML文档。这将触发相应的转换和解析逻辑,从而解析和提取所需的数据。 6. 处理提取的数据:一旦数据被解析和提取,我们可以使用Java代码对其进行处理。可以将数据保存到数据库中、生成报告或进行其他操作,具体取决于需求。 综上所述,使用Java的XSLT功能,我们可以轻松解析和提取.docx文件中的数据。这种方法可以帮助我们自动化处理文档并快速获取所需的信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值