java读取word文件转html

一.pom引入依赖

java 复制代码
<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words</artifactId>
	<version>15.12.0</version>
	<classifier>jdk16</classifier>
</dependency>

二.代码实现

java 复制代码
package com.example.demo.handler;

import com.alibaba.fastjson.JSONObject;
import com.aspose.words.HtmlSaveOptions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;

@Component
public class WordAnalysis {
    /**
     * 解析word
     * @param multipartFile 前端接收的文件,根据自己的需求也可以将MultipartFile转换为File
     * @return TitleTreeVO 存放标题的实体
     */
    public List wordAnalysis(MultipartFile multipartFile) throws IOException {
        byte[] byteArr = multipartFile.getBytes();
        InputStream inputStream = new ByteArrayInputStream(byteArr);
        List tableList = new ArrayList();
        try {
            // 设置转化的格式
            HtmlSaveOptions saveOptions = new HtmlSaveOptions();
            saveOptions.setExportImagesAsBase64(false);
            // 将所有word中的图片放在临时文件夹中,并将html中的链接替换为临时文件夹中绝对路径
            String property = System.getProperty("java.io.tmpdir");
            saveOptions.setImagesFolder(property);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // 把流转化为Document
            com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
            doc.save(baos, saveOptions);
            // 将html文件转化为Document,方便后续使用jsoup的操作
            Document htmlDoc = Jsoup.parse(baos.toString());
            // 解析Document
            tableList = analysisDoc(htmlDoc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
        }
        return tableList;
    }

    /**
     * 解析Document,按需写,样例只写了表格处理
     * @param htmlDoc
     * @return
     */
    public List analysisDoc(Document htmlDoc) {
        Elements tables = htmlDoc.getElementsByTag("table");
        List tableList = new ArrayList();
        for (int i = 0; i < tables.size(); i++) {
            Map<String, Object> tableInfo = new HashMap<>();
            UUID uuid = UUID.randomUUID();
            Element table = tables.get(i);
//            String tableName = table.previousElementSibling().text();
//            if ("".equals(tableName)) {
//                tableName = table.nextElementSibling().text();
//            }
            tableInfo.put("tableId", uuid);
            tableInfo.put("tableName", "表"+(i+1));
            tableInfo.put("tableHtml", tables.get(i).toString());
            Elements rows = table.select("tr");
            List rowList = new ArrayList();
            for (Element row: rows) {
                if (!row.attributes().get("style").contains("height:0pt")) {
                    List rowInfo = new ArrayList();
                    Elements cells = row.select("td");
                    for (Element cell: cells) {
                        JSONObject cellInfo = new JSONObject();
                        String data = cell.text();
                        int rowspan = new Integer(cell.attributes().get("rowspan")=="" ? "1" : cell.attributes().get("rowspan"));
                        int colspan = new Integer(cell.attributes().get("colspan")=="" ? "1" : cell.attributes().get("colspan"));
                        System.out.print(data + "\t");
                        cellInfo.put("content", data);
                        cellInfo.put("rowspan", rowspan);
                        cellInfo.put("colspan", colspan);
                        rowInfo.add(cellInfo);
                    }
                    System.out.println();
                    rowList.add(rowInfo);
                }
            }
            tableInfo.put("tableContent", rowList);
            tableList.add(tableInfo);
        }
        return tableList;
    }
}
相关推荐
敲代码的小王!2 小时前
MD5加密算法和BCrypt密码加密算法
java·算法·安全
罗政7 小时前
冒险岛079 V8 整合版源码搭建教程+IDEA启动
java·ide·intellij-idea
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
不只会拍照的程序猿8 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法
我荔枝呢!9 小时前
Java中的hashCode和equals方法之间有什么联系
java·开发语言·equals·hashcode
望未来无悔9 小时前
系统学习算法:专题十一 floodfill算法
java·算法
黑客老李9 小时前
新手小白如何挖掘cnvd通用漏洞之存储xss漏洞(利用xss钓鱼)
java·运维·服务器·前端·xss
不良人天码星9 小时前
Redis的简单使用
java·spring boot·redis·mybatis
面向未来_10 小时前
JAVA Kotlin Androd 使用String.format()格式化日期
java·开发语言·kotlin
qq_124987075310 小时前
Java+SpringBoot+数据可视化的家庭记账小程序(程序+论文+安装+调试+售后等)
java·spring boot·小程序·毕业设计