PPT ,word .pdf.excle 文档相互转换的源码

本文介绍了一种使用Java实现的办公文档转换方法,包括Word、Excel及PPT等格式之间的相互转换。通过引入Aspose库并设置许可证,实现了不同格式文档的高效转换,并附带了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步代码放进新建的类

 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;
}

第二步建个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>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值