OpenOffice的使用说明

来源:互联网 发布:fastcopy 网络拷贝 编辑:程序博客网 时间:2024/06/05 11:05

OpenOffice的使用说明

1. 概述

1.1 Java程序中可以利用OpenOffice,将办公文档(包括doc、docx、xls、xlsx等)转换为PDF格式的文档。由于PDF格式的文档可以达到在线预览的效果,而Office办公文档实现在线预览相对来说会比较麻烦,但曲线救国,通过文档格式的转换,从而也很容易地达到对Office办公文档实现在线预览的效果。

2. OpenOffice的安装

2.1 以Windows版本的OpenOffice安装为例,到OpenOffice官网下载最新版本的OpenOffice软件【[OpenOffice下载地址](http://www.openoffice.org/download/index.html】
2.2 下载完毕后进行安装,但需要记住软件的安装目录,如安装在D:/Program Files (x86)/OpenOffice 4,这个安装路径在文档格式转换的时候需要用到。

3. 文档格式转换

3.1 将Office文档转换为PDF格式的文档
    /**     *      * @description:将Office格式的文档转换为PDF格式的文档     * @author huang.j     * @date 2017年5月24日 上午11:44:03     * @param inputFilePath     *     */    public void convertWord2Pdf(String inputFilePath) {        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();        // OpenOffice安装在本地环境的目录        String officeHome = "D:/Program Files (x86)/OpenOffice 4";        config.setOfficeHome(officeHome);        OfficeManager officeManager = config.buildOfficeManager();        officeManager.start();        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);        String outputFilePath = getReplaceFileAbsolutePath(inputFilePath, ".pdf");        File inputFile = new File(inputFilePath);        if (inputFile.exists()) {            File outputFile = new File(outputFilePath);            if (!outputFile.getParentFile().exists()) {                outputFile.getParentFile().mkdirs();            }            // 进行PDF格式的转换            converter.convert(inputFile, outputFile);        }        officeManager.stop();    }
3.2 更改文档后缀为指定的后缀名
    /**     *      * @description:更改文档后缀为指定的后缀名     * @author huang.j     * @date 2017年5月24日 上午11:44:20     * @param inputFilePath 输入文档的绝对路径     * @param replaceEndWith 指定的后缀名     * @return 返回替换指定后缀名的文档的绝对路径     *     */    private String getReplaceFileAbsolutePath(String inputFilePath, String replaceEndWith) {        String replaceFilePath = null;        Pattern pattern = Pattern.compile("(\\.[a-zA-Z]+)");        Matcher matcher = pattern.matcher(inputFilePath);        String endWith = null;        if(matcher.find()) {            endWith = matcher.group(1);        }        if(StringUtil.isEmpty(endWith)) {            return null;        }        replaceFilePath = inputFilePath.replaceAll(endWith, replaceEndWith);        return replaceFilePath;    }

4. PDF文档的预览

4.1 完成PDF格式的转换后,下一步就可以进行PDF文件的预览
    /**     *      * @description:预览PDF文件     * @author huang.j     * @date 2017年5月23日 下午4:51:59     * @param attathFile     * @param response     *     */    public void previewPdf(File attathFile, HttpServletResponse response) {        response.setContentType("application/pdf");        try {            if(attathFile.exists()) {                DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());                DataInputStream dataInputStream = new DataInputStream(new FileInputStream(attathFile));                byte[] buffer = new byte[2048];                int len = buffer.length;                while((len = dataInputStream.read(buffer, 0, len)) != -1) {                    dataOutputStream.write(buffer);                    dataOutputStream.flush();                }                dataInputStream.close();                dataOutputStream.close();            }        } catch (Exception e) {            logger.error(e.getMessage(), e);        }    }

5. Office办公文档转换为PDF文档所需的jar包

1) commons-cli-xx.jar
2) commons-io-xx.jar
3) jodconverter-core-3.0-beta-4.jar
4) json-xxx.jar
5) juh-xx.jar
6) jurt-xx.jar
7) ridl-xx.jar
8) unoil-xx.jar

Jar包资源下载地址【点击连接】

原创粉丝点击