首页 > 编程语言 > 详细

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

时间:2020-09-08 15:09:01      阅读:347      评论:0      收藏:0      [点我收藏+]

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>    

  

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

个人笔记,仅供参考

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

原文:https://www.cnblogs.com/1427wsl/p/13632342.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!