栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java使用POI将docx文件转为html

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java使用POI将docx文件转为html

Java使用POI将docx文件转为html
  • 使用到的依赖
  • 使用到的类
  • 具体代码
    • MyDocUtil.classsss
    • MyDocTest.class
    • 结果
      • word
      • Html
    • 文件结构
  • 扩展

使用到的依赖

这里 值得注意的是版本的问题,版本不一样的话会报错。

  		
            org.apache.poi
            poi
            3.15
        

        
        
            org.apache.poi
            poi-scratchpad
            3.15
        
                
        
            org.apache.poi
            poi-ooxml
            3.15
        

        
            org.apache.poi
            poi-ooxml-schemas
            3.15
        

        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.xhtml
            1.0.6
        
使用到的类

XWPFDocument :用于操作Word的对象
XHTMLOptions :转换成Html时的一些设置
XHTMLConverter :转换类
具体信息大家可以去POI的官网上看看,poi官网文档。

说出来你可能不信,后两个官网文档里没有。。。。唯一找到的有关WordToHtml的只有两个,一个WordToHtmlConverter,另一个是WordToHtmlUtils。但是前者是用来转换doc文件的(此文是转换docx),后者是用来操作doc文档的工具类,比如在文件里新增表格、图片啊什么的。。。。。

具体代码 MyDocUtil.classsss
package com;

import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;


public class MyDocUtil {

    
    public static void docxToHtml(String filePath , String htmlPath) throws IOException {
        //获取文件名称
        String myFileName = getFileNameInfo(filePath , 0);

        //图片存放路径
        String imagePath = htmlPath + File.separator + myFileName + getDataTime();
        //转换的html文件路径 与图片在同目录中
        String fileOutName = imagePath + File.separator + myFileName + ".html";

        //long startTime = System.currentTimeMillis();
        //获取一个用操作Word的对象
        XWPFDocument document = new XWPFDocument(new FileInputStream(filePath));
        //导出为html时的一些基本设置类
        XHTMLOptions options = null;
        //判断word文件中是否有图片
        if(document.getAllPictures().size() > 0) {
            //获取默认的对象,设置缩进indent
            options = XHTMLOptions.getDefault().indent(4);
            // 如果包含图片的话,要设置图片的导出位置
            File imageFolder = new File(imagePath);
            //设置图片抽取器的目的地文件夹 用于存放图片文件
            options.setExtractor(new FileImageExtractor(imageFolder));
            // URI resolver  word的html中图片的目录路径
            options.URIResolver(new BasicURIResolver(imagePath));
        }

        //获取输出的html文件对象
        File outFile = new File(fileOutName);
        //创建所有的父路径,如果不存在父目录的话
        outFile.getParentFile().mkdirs();
        //创建一个输出流
        OutputStream out = new FileOutputStream(outFile);

        //html转换器
        XHTMLConverter.getInstance().convert(document, out, options);
        //System.out.println("转换用时: " + fileOutName + " with " + (System.currentTimeMillis() - startTime) + " ms.");
    }


    
    public static String getDataTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        Date date =new Date();
        return simpleDateFormat.format(date);
    }

    
    public static String getFileNameAndExtension(String filePath) {
        String str[] = filePath.split("\\");
        return str[str.length - 1];
    }

    
    public static String getFileNameInfo(String str , int position) {
        String fileNameAndExtension = getFileNameAndExtension(str);
        String str1[] = fileNameAndExtension.split("\.");
        return str1[position];
    }
}
MyDocTest.class
import com.MyDocUtil;

import java.io.IOException;


public class MyDocTest {
    public static void main(String[] args) {
        //String filePath = "F:\学习记录\测试数据\word\docx\无图片文档.docx";
        String filePath = "F:\学习记录\测试数据\word\docx\测试文档.docx";
        //String filePath = "F:\学习记录\测试数据\word\doc\test.doc";

        String htmlPath = "F:\学习记录\测试数据\word\html\";

        try {
            MyDocUtil.docxToHtml(filePath , htmlPath);
        }catch (IOException exception) {
            exception.printStackTrace();
        }

    }
}

结果 word

Html

文件结构

扩展

java 使用 POI 操作 XWPFDocumen 创建和读取 Office Word 文档基础篇:https://www.cnblogs.com/mh-study/p/9747945.html
Poi之Word文档结构介绍:https://www.cnblogs.com/Springmoon-venn/p/5494602.html?utm_source=ld246.com
下面这个讲得更加简单明了
poi-tl: http://deepoove.com/poi-tl/apache-poi-guide.html#_%E7%94%9F%E6%88%90%E6%96%87%E6%A1%A3

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号