java 中使用aspose 将Excel 表格转成 pdf 文件 不出现折行


excel 表格

转成功后的 pdf文件

 

  

 

话不多说直接上代码

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

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


public class Excel2PdfUtil {
    /**
     * @param args
     */
    public static void main(String[] args) {
        excel2Pdf("D://cccc/sss.xlsx","D://cccc/sss.pdf");
    }

    /**
     * 获取license
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            InputStream license = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");// license路径
            License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void excel2Pdf(String excelPath, String pdfPath) {
        long old = System.currentTimeMillis();
        // 验证License
        if (!getLicense()) {
            return;
        }
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File excelFile = new File(excelPath);
            if (excelFile.exists()) {
                fileInputStream = new FileInputStream(excelFile);
                Workbook workbook = new Workbook(fileInputStream);

                File pdfFile = new File(pdfPath);

                PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
                pdfSaveOptions.setOnePagePerSheet(true);//把内容放在一张PDF 页面上;
                FileOutputStream fileOS = new FileOutputStream(pdfFile);
                workbook.save(fileOS, pdfSaveOptions);//  只放一张纸;我的专为横向了

                long now = System.currentTimeMillis();
                System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + pdfFile.getPath());
            } else {
                System.out.println("文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    }

  需要依赖的maven jar包

        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>8.5.2</version>
        </dependency>    

  

-----------------------------------------------------------------------

个人笔记,仅供参考


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM