word、execl等文件转换pdf、html

众所周知,我们想要word预览,要么转成pdf、要么成html!

POI提供了execl、word的转换方式,但是内容复杂而且容易出错!并且转换的也不完整!

还有一种方式,是直接在Linux服务器上装openOfiice插件,这样只需写一些简单的代码就很容易就能转换!

但是今天介绍第三种方式,

aspose.cells
aspose.words

只要加入这两个包,就能转换,而且 代码非常简单,在Linux和windows上都能用,废话不多说,直接上代码

package aspose;

import com.aspose.cells.*;
import com.aspose.words.Document;
import com.aspose.words.ExportHeadersFootersMode;
import com.aspose.words.FontSettings;
import woldpdf.FontProvider;

import javax.crypto.Cipher;
import java.io.*;

/**
 * @ClassName wordPDF
 * @Description TODO
 * @Author zhurongfei
 * @Data 2020/6/22 9:45
 * Version 1.0
 **/
public class wordPDF {
    /**
     * 获取license许可凭证
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            String licenseStr = "<License>\n"
                    + " <Data>\n"
                    + " <Products>\n"
                    + " <Product>Aspose.Total for Java</Product>\n"
                    + " <Product>Aspose.Words for Java</Product>\n"
                    + " </Products>\n"
                    + " <EditionType>Enterprise</EditionType>\n"
                    + " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
                    + " <LicenseExpiry>20991231</LicenseExpiry>\n"
                    + " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n"
                    + " </Data>\n"
                    + " <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>\n"
                    + "</License>";
            InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
            License asposeLic = new License();
            asposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * word文档  转换为 PDF
     * @param inPath 源文件
     * @param outPath 目标文件
     */
    public static void doc2pdf(String inPath, String outPath) {

        //验证License,获取许可凭证
        if (!getLicense()) {
            return;
        }

        try {

            //新建一个PDF文档
            File file = new File(outPath);
            if(!file.exists()){
                file.createNewFile();
            }
            //新建一个IO输出流
            FileOutputStream os = new FileOutputStream(file);
            //获取将要被转化的word文档
            FileInputStream inputStream = new FileInputStream(new File(inPath));
            Document doc = new Document(inputStream);
//            HtmlSaveOptions hso = new HtmlSaveOptions();
//            hso.setExportRoundtripInformation(true);
//            String filePath = FontProvider.class.getClassLoader().getResource("simsunb.ttf").getFile();
//            FontSettings.getDefaultInstance().setFontsFolder(filePath, false);
            // 全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /// <summary>
    /// excel转pdf
    /// </summary>
    /// <param name="path">文件地址</param>
    /// <param name="newFilePath">转换后的文件地址</param>
    /// <returns></returns>
    public static void ExcelToPdf(String path, String newFilePath) throws Exception {
        //验证License,获取许可凭证
        if (!getLicense()) {
            return;
        }
//        FTPClient ftpClient = new FTPClient();
        Workbook wb = new Workbook(path);// 原始excel路径
        HtmlSaveOptions options = new HtmlSaveOptions();
        options.setExportDocumentProperties(false);
        options.setExportWorkbookProperties(false);
        options.setExportWorksheetProperties(false);
        options.setExportSimilarBorderStyle(true);
        options.setExportImagesAsBase64(false);
        options.setExcludeUnusedStyles(true);
        options.setExportHiddenWorksheet(false);
        options.setWidthScalable(false);
        options.setPresentationPreference(false);
        options.setHtmlCrossStringType(HtmlCrossType.CROSS_HIDE_RIGHT);
        FileOutputStream fileOS = new FileOutputStream(newFilePath);
        wb.calculateFormula();
        wb.save(fileOS, options);
        fileOS.close();
    }
    public static void main(String[] args) throws Exception {

        ExcelToPdf("C:\\Users\\98790\\Desktop\\a.xlsx", "C:\\Users\\98790\\Desktop\\a.html");
//        doc2pdf("C:\\Users\\98790\\Desktop\\a.docx", "C:\\Users\\98790\\Desktop\\a.pdf");
    }
}

不过我写execl只能转换简单的html,复杂的如有多个execl的sheet,就会报错,具体还不知道怎么搞,有兴趣的话可以讨论

下面是我git上的代码:

https://github.com/zrfgame/wordPdf

java的jar:

https://download.csdn.net/download/qq_31741189/12570029

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值