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;
    }
}
相关推荐
爱编程的鱼10 分钟前
C# 数据类型||C# 类型转换
java·算法·c#
一键三联啊10 分钟前
ArrayList的subList的数据仍是集合
java·开发语言
前鼻音太阳熊18 分钟前
【Spring Boot 应用开发】-06 自动配置-生成配置元数据
java·spring boot·后端
purrrew18 分钟前
【数据结构_8】栈和队列
java·开发语言·数据结构
汤永红20 分钟前
windows下git bash安装SDKMan报错Looking for unzip...Not found
java·git·sdkman
风中飘爻23 分钟前
MySQL入门:数据操作CURD
前端·bootstrap·html
云惠科技(SEO)40 分钟前
泛目录站群技术架构演进观察:2025年PHP+Java混合方案实战笔记
java·人工智能·搜索引擎
emoji1111111 小时前
vue3、原生html交互传值
vue.js·html·交互
nothingbutluck4641 小时前
2025.4.10 html有序、无序、定义列表、音视频标签
前端·html·音视频
牛马baby1 小时前
Springboot 自动装配原理是什么?SPI 原理又是什么?
java·spring boot·后端