java excel轉pdf 工具類


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.cells.SaveFormat;



public class PdfUtilExcel {
    /**
     * EXCEL 轉  PDF
     * 
     * @param filepath
     */
    public void excelOfPdf(String filepath,HttpServletRequest request) {
        boolean result = false;
        try {
            InputStream license = PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
            if (result == true) {
                Workbook wb = new Workbook(request.getSession().getServletContext().getRealPath("\\" +filepath));//原始excel路徑
                String fles = filepath.substring(0, filepath.lastIndexOf("."));
                File file = new File(request.getSession().getServletContext()
                        .getRealPath("\\" +fles+".pdf"));// 輸出路徑
                FileOutputStream fileOS = new FileOutputStream(file);
                wb.save(fileOS, SaveFormat.PDF);
                fileOS.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
使用方法:

PdfUtilExcel excel = new PdfUtilExcel();
excel.excelOfPdf(filePaths, request);//filePaths為存儲地址

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM