excel转pdf——asposeCells

通过第三方插件aspose.Cells实现。

import com.aspose.cells.*;

public class CellsFileConvert{

    /**
     * pdf文件后缀
     */
    final String FILE_PDF_SUFFIX = ".pdf";

    private String excelToPdf(String filePath) {
        String str =
                "<License>\n" +
                        "    <Data>\n" +
                        "        <Products>\n" +
                        "            <Product>Aspose.Total for Java</Product>\n" +
                        "        </Products>\n" +
                        "        <EditionType>Enterprise</EditionType>\n" +
                        "        <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                        "        <LicenseExpiry>20991231</LicenseExpiry>\n" +
                        "        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                        "    </Data>\n" +
                        "    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                        "</License>";
        //pdf文件的保存路径
        String pdfPath = filePath.substring(0, filePath.lastIndexOf(".")) + FILE_PDF_SUFFIX;
        try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
             FileOutputStream os = new FileOutputStream(pdfPath)) {
            //支持正版
            License license = new License();
            license.setLicense(is);

            Workbook workbook = new Workbook(filePath);
            //pdf页面有其他要求可以用PdfSaveOptions进行调整
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //所有列缩放到一个页面
            pdfSaveOptions.setOnePagePerSheet(true);
            workbook.save(os, pdfSaveOptions);
            return pdfPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        CellsFileConvert cellsFileConvert = new CellsFileConvert();
        String pdfPath = cellsFileConvert.excelToPdf("D:\\file\\test.xlsx");
        System.out.println(pdfPath);
    }
}

通过PdfSaveOptions可以修改生成的pdf文件.

支持正版

(破)aspose-cells-20.7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值