java代码将word,excel转为pdf

苗换换

 | 我的经验 | 我的收入 | 私信(0) | 百度首页 百度经验

帮助

发布经验

百度经验 > 游戏/数码 > 互联网

java代码将word、excel文档转换成pdf

听语音

  •  
  • |
  • 浏览:1337
  • |
  • 更新:2017-03-17 10:48

java代码将word、excel文档转换成pdf

工具/原料

  • myeclipse 10

方法/步骤

  1. 新建javaweb工程

    下载aspouse-word-14.11.0-jdk16.jar包和aspose-cells-8.5.2.jar包并引入

    word转pdf的java函数

    package webViewer;

    import java.io.*; 

    import com.aspose.words.*;         //引入aspouse-word-14.11.0-jdk16.jar包

    public class Word2Pdf { 

    public static boolean getLicense() {       

    boolean result = false;       

    try {           

    InputStream is = Test.class.getClassLoader().getResourceAsStream("wordlicense.xml"); //  wordlicense.xml应放在..\WebRoot\WEB-INF\classes路径下           

    com.aspose.words.License aposeLic = new com.aspose.words.License();           

    aposeLic.setLicense(is);           

     result = true;       

     }

    catch (Exception e) {                          

    e.printStackTrace();       

    }       

    return result;   

    }  

    public static void word2pdf(String Address) {               

     if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生           

     return;       

     try {              

    File file = new File("C:/inetpub/wwwroot/web/file/pdf1.pdf");  //新建一个空白pdf文档           

    FileOutputStream os = new FileOutputStream(file);           

    Document doc = new Document(Address);                    //Address是将要被转化的word文档           

    doc.save(os, SaveFormat.PDF);                            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换            os.close();       

    }

    catch (Exception e) {           

    e.printStackTrace();       

    }    

    }

    }

  2. 2

    excel转换成pdf的java函数

    package webViewer;

    import java.io.*; 

    import com.aspose.cells.*;      //引入aspose-cells-8.5.2.jar包

    public class Excel2Pdf {

    public static boolean getLicense() {       

    boolean result = false;       

    try {           

    InputStream is = Test.class.getClassLoader().getResourceAsStream("xlsxlicense.xml"); //  license.xml应放在..\WebRoot\WEB-INF\classes路径下           

    License aposeLic = new License();           

    aposeLic.setLicense(is);           

    result = true;       

    }

    catch (Exception e) {                          

    e.printStackTrace();       

    }       

    return result;   

    }  

    public static void excel2pdf(String Address) {               

    if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生           

    return;       

    }       

    try {           

    File pdfFile = new File("C:/inetpub/wwwroot/web/file/pdf1.pdf");// 输出路径           

    Workbook wb = new Workbook(Address);// 原始excel路径                        FileOutputStream fileOS = new FileOutputStream(pdfFile);           

    wb.save(fileOS, SaveFormat.PDF);             

    fileOS.close();                 

    }

    catch (Exception e) {           

    e.printStackTrace();       

    }   

    }

    }

  3. 3

    再利用测试函数进行检测

    package webViewer;public class Test { public static void main(String[] args){

     Word2Pdf.word2pdf("C:/inetpub/wwwroot/web/file/4.docx");  

     Excel2Pdf. excel2pdf("C:/inetpub/wwwroot/web/file/5.xlsx"); }}

  4. 4

    还可以将函数运用到jsp页面再使用pdf.js插件实现页面预览

    END

经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。

举报作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。

投票(63)

有得(0)

我有疑问(0)

换一批相关经验

今日支出元

写经验 有钱赚 >>

起舞1314

作者的经验

延禧攻略

如要投诉,请到百度经验投诉中心,如要提出意见、建议, 请到百度经验管理吧反馈。

热门杂志

  • 第1期

    你不知道的iPad技巧

    3845次分享

  • 第1期

    win7电脑那些事

    6689次分享

  • 第2期

    新人玩转百度经验

    1433次分享

  • 第1期

    Win8.1实用小技巧

    2677次分享

  • 第1期

    小白装大神

    1973次分享

3

新手帮助意见反馈投诉举报

©2018Baidu  使用百度前必读  百度经验协议  作者创作作品协议  京ICP证030173号-1 京网文【2013】0934-983号

苗换换

1

猜你喜欢

转载自blog.csdn.net/xiaoxiangyeyuzhi/article/details/82830989