Java使用POI将doc文档转为Html

前提

关于依赖什么的请看上一篇文章:docx转Html

几个使用到的类

HWPFDocument :代表了一个doc文件对象
WordToHtmlConverter :看名字就知道了用于Word转Html的类
Document : 表示一个完整的Html或者XML文档对象
DOMSource : 源树
StreamResult : 转换结果的持有者
Transformer :转换器用于将源树转为结果树

吐槽一下POI类的注释,是真的少,还好命名都规范源码还是能看个大概 = =

几个方法的理解

converter.setPicturesManager(xxxx)

这个用的是匿名内部类

converter.processDocument(hwpfDocument);

就是一个简单的塞值,将hwpfDocument内容属性塞到转换器内部的HtmlDocumentFacade

外观模式

外观模式,这个一句话说不清楚,我就放在最下面了链接里了。上面的HtmlDocumentFacade就是使用了外观模式

具体实现

因为该方法作用和之前提到的类似,所以其中的工具类的方法大家去上一篇自取即可:docx转Html

    /*
     * @description 将doc文档转为html
     * @author 三文鱼
     * @date 9:16 2022/4/29
     * @param filePath
     * @param htmlPath
     * @return void
     **/
    public static void docToHtml(String filePath , String htmlPath) throws Exception {
        //获取文件名称
        String myFileName = getFileNameInfo(filePath , 0);

        //该doc文件转换后所有文件存放的目录
        String docRootPath = htmlPath + File.separator + myFileName + getDataTime() + File.separator;
        String imagePath = docRootPath  + "image" + File.separator;
        //转换的html文件路径 与图片在同目录中
        String fileOutName = docRootPath + myFileName + ".html";
        
        //创建图片文件的存储目录
        new File(imagePath).mkdirs();
        //poi中doc文档对应的实体类
        HWPFDocument hwpfDocument = new HWPFDocument(new FileInputStream(filePath));
        //使用空的文档对象构建一个转换对象
        WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory
                                                                    .newInstance()
                                                                    .newDocumentBuilder()
                                                                    .newDocument());

        //设置存储图片的管理者--使用匿名内部类实现 该类实现了PicturesManager接口,实现了其中的savePicture方法
        converter.setPicturesManager(new PicturesManager() {
            FileOutputStream out = null;
            //在下面的processDocument方法内部会调用该方法 用于存储word中的图片文件
            @Override
            public String savePicture(byte[] bytes, PictureType pictureType, String name, float width, float height) {
                try {
                    //单个照片的保存
                    out = new FileOutputStream(imagePath + name);
                    out.write(bytes);
                } catch (IOException exception) {
                    exception.printStackTrace();
                }finally {
                    if(out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //这里要返回给操作者(HtmlDocumentFacade)一个存储的路径 用于生成Html时定位到图片资源
                return imagePath + name;
            }
        });
        //使用外观模式,将hwpfDocument文档对象设置给HtmlDocumentFacade中的Document属性
        converter.processDocument(hwpfDocument);
        //获取转换器中的document文档
        Document htmlDocument = converter.getDocument();
        //充当文档对象模型 (DOM) 树形式的转换源树的持有者  -- 源树
        DOMSource domSource = new DOMSource(htmlDocument);

        //转换器 该对象用于将源树转换为结果树
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        //设置输出时的以什么方式输出,也可说是结果树的文件类型 可以是html/xml/text或者是一些扩展前三者的扩展类型
        transformer.setOutputProperty(OutputKeys.METHOD , "html");
        //设置一些必要的属性 设置输出时候的编码为utf-8
        transformer.setOutputProperty(OutputKeys.ENCODING , "utf-8");

        //转换 将输入的源树转换为结果树并且输出到streamResult中
        transformer.transform(domSource , new StreamResult(new File(fileOutName));
    }

结果

测试

测试的话就跟之前的代码一样了

public class DocTest {
    public static void main(String[] args) {
        String filePath = "F:\\学习记录\\测试数据\\word\\doc\\test.doc";
        String htmlPath = "F:\\学习记录\\测试数据\\word\\html";
        try {
            MyDocUtil.docToHtml(filePath, htmlPath);
        }catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

生成结果

Word内容

在这里插入图片描述

生成的目录以及结果

在这里插入图片描述
在这里插入图片描述

Html

在这里插入图片描述

参考资料

DOM树:DOM树的组成.
POI官方文档:POI官方文档.
JDK的API文档: Java的API文档.
外观模式:外观模式
word与html互转:word与Html互转.

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
JavaPOI库是一个强大的开源库,可以用于处理Microsoft Office文件,包括Word文档。如果想要将DOC文档转为DOCX格式,可以使用POI库中的功能。下面是一个简单的示例代码: ```java import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToConverter; import org.apache.poi.hwpf.usermodel.HWPFRange; import java.io.*; public class ConvertDocToDocx { public static void main(String[] args) throws IOException { String inputFile = "input.doc"; String outputFile = "output.docx"; InputStream input = new FileInputStream(inputFile); HWPFDocument doc = new HWPFDocument(input); WordToConverter converter = new WordToConverter(XWPFDocument.getDocument); converter.processDocument(doc); OutputStream output = new FileOutputStream(outputFile); converter.getDocument().write(output); output.close(); input.close(); System.out.println("DOC文件已成功转为DOCX文件!"); } } ``` 以上代码使用POI的HWPF库,其中包含了`HWPFDocument`类,可以处理DOC文档。首先,我们需要指定输入DOC文件和输出DOCX文件的路径。然后,通过`FileInputStream`读取输入文件并创建`HWPFDocument`对象。接下来,我们创建一个`WordToConverter`对象,将`HWPFDocument`对象传递给它,并调用`processDocument`方法将DOC文件转换为DOCX格式。最后,通过`FileOutputStream`将转换后的文档写入输出文件。 请注意,POI库的具体版本可能略有不同,上述代码适用于较新版本的POI库。在使用之前,请确保已正确配置POI库的依赖项。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值