将Office(如:Word、Excel、PPT 等)文件转html(通过OpenOffice实现)

这里简单介绍下OpenOffice

OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows、linux、MacOS X (X11)
和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。
OpenOffice.org 是自由软件,任何人都可以免费下载、使用及推广它。


需要注意: Java环境下操作OpenOffice 要使用 JodConverter。
        需要在服务器中安装OpenOffice 

点击下载JodConverter

OpenOffice工具大家可以去官网下载,文件比较大我这里没办法分享

安装好OpenOffice跟JodConverter的jar包导入后,就开始编码吧。

我之前有写过一篇转换成pdf文件的案例,这里其实跟那个一样,
只不过是吧下面这个方法中的目标文件的后缀改成了html,
这样就直接能转成html文件了,有些人可能不信,
我把结果以图片方式在最下面贴出来吧

/**
 * 获取输出文件
 * @param inputFilePath
 * @return
 */
 public static String getOutputFilePath(String inputFilePath) {
    String outputFilePath=inputFilePath.replaceAll("."+getPostfix(inputFilePath),".html");
    return outputFilePath;
 }

代码如下:

package test;

import java.io.File;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

/**
 * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
 * 转化为pdf文件
 * Office2010的没测试
 * @author ZhouMengShun
 */
public class Demo  {

    /**
     * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为html文件
     * @param inputFilePath 源文件路径,如:"D:/论坛.docx"
     * @return
     */
    public static File openOfficeToPDF(String inputFilePath) {
        return office2pdf(inputFilePath);
    }

    /**
     * 根据操作系统的名称,获取OpenOffice.org 4的安装目录
* 如我的OpenOffice.org 4安装在:C:/Program Files (x86)/OpenOffice 4 * @return OpenOffice.org 4的安装目录 */
public static String getOfficeHome() { //这是返回的是OpenOffice的安装目录,建议将这个路径加入到配置文件中,然后直接通过配置文件获取 //我这里就直接写死了 return "C:/Program Files (x86)/OpenOffice 4"; } /** * 连接OpenOffice.org 并且启动OpenOffice.org * @return */ public static OfficeManager getOfficeManager() { DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 设置OpenOffice.org 4的安装目录 config.setOfficeHome(getOfficeHome()); // 启动OpenOffice的服务 OfficeManager officeManager = config.buildOfficeManager(); officeManager.start(); return officeManager; } /** * 转换文件 * @param inputFile * @param outputFilePath_end * @param inputFilePath * @param outputFilePath * @param converter */ public static File converterFile(File inputFile,String outputFilePath_end,String inputFilePath, OfficeDocumentConverter converter) { File outputFile = new File(outputFilePath_end); //判断目标路径是否存在,如不存在则创建该路径 if (!outputFile.getParentFile().exists()){ outputFile.getParentFile().mkdirs(); } converter.convert(inputFile, outputFile);//转换 System.out.println("文件:"+inputFilePath+"\n转换为\n目标文件:"+outputFile+"\n成功!"); return outputFile; } /** * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为html文件 * @param inputFilePath 源文件路径,如:"D:/论坛.docx" * @param outputFilePath 目标文件路径,如:"D:/论坛.html" * @return */ public static File office2pdf(String inputFilePath) { OfficeManager officeManager = null; try { if (inputFilePath==null||inputFilePath.trim().length()<=0) { System.out.println("输入文件地址为空,转换终止!"); return null; } File inputFile = new File(inputFilePath); //转换后的文件路径 String outputFilePath_end=getOutputFilePath(inputFilePath); if (!inputFile.exists()) { System.out.println("输入文件不存在,转换终止!"); return null; } //获取OpenOffice的安装路劲 officeManager = getOfficeManager(); //连接OpenOffice OfficeDocumentConverter converter=new OfficeDocumentConverter(officeManager); //转换并返回转换后的文件对象 return converterFile(inputFile,outputFilePath_end,inputFilePath,converter); } catch (Exception e) { System.out.println("转化出错!"); e.printStackTrace(); } finally { if (officeManager != null) { //停止openOffice officeManager.stop(); } } return null; } /** * 获取输出文件 * @param inputFilePath * @return */ public static String getOutputFilePath(String inputFilePath) { String outputFilePath=inputFilePath.replaceAll("."+getPostfix(inputFilePath),".html"); return outputFilePath; } /** * 获取inputFilePath的后缀名,如:"D:/论坛.docx"的后缀名为:"docx" * @param inputFilePath * @return */ public static String getPostfix(String inputFilePath) { return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); } //测试 public static void main(String[] args) { openOfficeToPDF("D:/论坛.docx"); } }

转换后如下:
将Office(如:Word、Excel、PPT 等)文件转html(通过OpenOffice实现)_第1张图片

你可能感兴趣的:(文件转换,OpenOffice,office转htm)