• excel转pdf(使用linux)


    重点注意():验证License时引入的包别用word的包,否则还是有水印

    import com.aspose.cells.License;

    1、license.xml  放在resources路径下

    <?xml version="1.0" encoding="UTF-8" ?>
    <License>
    <Data>
    <Products>
    <Product>Aspose.Total for Java</Product>
    <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
    </License>

    2、Util

    package com.rhtms.common.utils.word;

    import com.aspose.cells.PdfSaveOptions;
    import com.aspose.cells.Workbook;
    import com.aspose.cells.License;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.stereotype.Component;
    import java.io.*;

    @Component
    public class Excel2PdfAsposeUtil {

    public static boolean getLicense() {
    boolean result = false;
    InputStream is = null;
    try {
    Resource resource = new ClassPathResource("license.xml");
    is = resource.getInputStream();
    License aposeLic = new License();
    aposeLic.setLicense(is);
    result = true;
    } catch (
    Exception e) {
    e.printStackTrace();
    } finally {
    if (is != null) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    return result;
    }

    public static boolean excel2pdf(String inPath, String outPath) {
    if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
    return false;
    }
    try {
    Workbook wb = new Workbook(inPath);// 原始excel路径

    FileOutputStream fileOS = new FileOutputStream(outPath);
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    pdfSaveOptions.setOnePagePerSheet(true);
    int sheetCount = wb.getWorksheets().getCount();
    int[] showSheets=new int[sheetCount];
    for (int i = 0; i < sheetCount; i++) {
    showSheets[i]=i;
    }
    //隐藏workbook中不需要的sheet页。
    autoDraw(wb,showSheets);
    printSheetPage(wb,showSheets);
    wb.save(fileOS, pdfSaveOptions);
    fileOS.flush();
    fileOS.close();
    System.out.println("完毕");

    } catch (Exception e) {
    e.printStackTrace();
    return false;
    }
    return true;
    }

    /**
    * 设置打印的sheet 自动拉伸比例
    * @param wb
    * @param page 自动拉伸的页的sheet数组
    */
    public static void autoDraw(Workbook wb,int[] page){
    if(null!=page&&page.length>0){
    for (int i = 0; i < page.length; i++) {
    wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
    wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
    }
    }
    }
    /**
    * 隐藏workbook中不需要的sheet页。
    * @param wb
    * @param page 显示页的sheet数组
    */
    public static void printSheetPage(Workbook wb,int[] page){
    for (int i= 1; i < wb.getWorksheets().getCount(); i++) {
    wb.getWorksheets().get(i).setVisible(false);
    }
    if(null==page||page.length==0){
    wb.getWorksheets().get(0).setVisible(true);
    }else{
    for (int i = 0; i < page.length; i++) {
    wb.getWorksheets().get(i).setVisible(true);
    }
    }
    }
    }
  • 相关阅读:
    HDU 1269 迷宫城堡 tarjan算法求强连通分量
    hrbust 1721 A + B = 0 map的应用
    关于vis标记
    poj 1703
    poj1961 kmp
    acm poj1260 dp
    矩阵链乘 hrbust 1600
    单源最短路径 hdu 2066
    最小生成树
    多维背包 hrbudt 1335 算法与追MM
  • 原文地址:https://www.cnblogs.com/xlj227/p/16426544.html
Copyright © 2020-2023  润新知