当前位置:   article > 正文

PPT ,word .pdf.excle 文档相互转换的源码_is = asposeutils.class.getclassloader().getresourc

is = asposeutils.class.getclassloader().getresourceasstream("aspose-license.

第一步代码放进新建的类

 public static String word(String from,String to) throws Exception  {            
    if (!getWordLicense())   throw new Exception("com.aspose.words lic ERROR!");
    String suffixStr = to.substring(to.lastIndexOf(".")+1);
    int suffix=com.aspose.words.SaveFormat.UNKNOWN;
    if(suffixStr.equals("doc")) {
        suffix= com.aspose.words.SaveFormat.DOC;
    }else if(suffixStr.equals("docx")) {
        suffix= com.aspose.words.SaveFormat.DOCX;
    }else if(suffixStr.equals("pdf")) {
        suffix= com.aspose.words.SaveFormat.PDF;
    }else if(suffixStr.equals("html")) {
        suffix= com.aspose.words.SaveFormat.HTML;
    }else if(suffixStr.equals("txt")) {
        suffix= com.aspose.words.SaveFormat.TEXT;
    }
    File file = new File(to);  //新建一个空白pdf文档
    try {
        long old = System.currentTimeMillis();
        
        FileOutputStream os = new FileOutputStream(file);
        Document doc = new Document(from);                    //Address是将要被转化的word文档
        doc.save(to, suffix);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
       
    } catch (Exception e) {
        e.printStackTrace();
    }
    //return file.getName();
    return file.getPath();
}

public static String excel(String form,String to) throws Exception {
    if (!getExcelLicense())   throw new Exception("com.aspose.cells lic ERROR!");
    
    String suffixStr = to.substring(to.lastIndexOf(".")+1);
    int suffix=com.aspose.cells.SaveFormat.UNKNOWN;
    if(suffixStr.equals("xlsx")) {
        suffix= com.aspose.cells.SaveFormat.XLSX;
    }else if(suffixStr.equals("xls")) {
        suffix= com.aspose.cells.SaveFormat.EXCEL_97_TO_2003;
    }else if(suffixStr.equals("pdf")) {
        suffix= com.aspose.cells.SaveFormat.PDF;
    }else if(suffixStr.equals("html")) {
        suffix= com.aspose.cells.SaveFormat.HTML;
    }else if(suffixStr.equals("csv")) {
        suffix= com.aspose.cells.SaveFormat.CSV;
    }else if(suffixStr.equals("svg")) {
        suffix= com.aspose.cells.SaveFormat.SVG;
    }else {
        suffix= com.aspose.cells.SaveFormat.AUTO;
    }
    
    File file = new File(to);// 输出路径
    try {
        long old = System.currentTimeMillis();
        
        Workbook wb = new Workbook(form);// 原始excel路径
        FileOutputStream fileOS = new FileOutputStream(file);
        wb.save(fileOS,suffix);
        fileOS.close();
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath()); //转化过程耗时
        
    } catch (Exception e) {
        e.printStackTrace();
    }
    //return file.getName();
    return file.getPath();
}

public static String ppt(String form,String to) throws Exception {
    // 验证License
    if (!getPPTLicense())  throw new Exception("com.aspose.slides lic ERROR!");

    String suffixStr = to.substring(to.lastIndexOf(".")+1);
    int suffix=0;
    if(suffixStr.equals("ppt")) {
        suffix= com.aspose.slides.SaveFormat.Ppt;
    }else if(suffixStr.equals("pptx")) {
        suffix= com.aspose.slides.SaveFormat.Pptx;
    }else if(suffixStr.equals("pdf")) {
        suffix= com.aspose.slides.SaveFormat.Pdf;
    }else if(suffixStr.equals("html")) {
        suffix= com.aspose.slides.SaveFormat.Html;
    }else if(suffixStr.equals("swf")) {
        suffix= com.aspose.slides.SaveFormat.Swf;
    }
    
    File file = new File(to);// 输出pdf路径
    try {
        long old = System.currentTimeMillis();
        
        Presentation pres = new Presentation(form);//输入pdf路径
        FileOutputStream fileOS = new FileOutputStream(file);
        pres.save(fileOS, suffix);
        fileOS.close();
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath()); //转化过程耗时
        
     } catch (Exception e) {
         e.printStackTrace();
     }
    //return file.getName();
    return file.getPath();
}

private static boolean getWordLicense(){
    boolean result = false;
    try {
        InputStream is =AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");  
        com.aspose.words.License aposeLic = new com.aspose.words.License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        
    }
    return result;
}

private static boolean getExcelLicense(){
    boolean result = false;
    try {
        InputStream is =AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");  
        com.aspose.cells.License aposeLic = new com.aspose.cells.License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        
    }
    return result;
}

private static boolean getPPTLicense(){
    boolean result = false;
    try {
        InputStream is =AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");  
        com.aspose.slides.License aposeLic = new com.aspose.slides.License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        
    }
    return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145

第二步建个xml文件,把下面代码写进去

	 <xml version="1.0" encoding="UTF-8"?>
		<License>
		  <Data>
		    <Products>
		      <Product>Aspose.Words for Java</Product>
		      <Product>Aspose.Slides for Java</Product>
		      <Product>Aspose.Cells for Java</Product>
		    </Products>
		    <EditionType>Enterprise</EditionType>
		    <SubscriptionExpiry>29991231</SubscriptionExpiry>
		    <LicenseExpiry>29991231</LicenseExpiry>
		    <SerialNumber>---</SerialNumber>
		  </Data>
		  <Signature>---</Signature>
		</License>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

最后jar 所需要3个jar包 在我的资源里面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/743041
推荐阅读
相关标签
  

闽ICP备14008679号