aspose excel2pdf

使用ASpose Cells for java,将excel 转成pdf,会出现一个sheet分页成多页,而且规律奇怪

可以设置

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.bi.wms.kory.status.ConvertStatus;
import com.bi.wms.kory.util.AsposeLicenseUtil;

public class AsposeTest {
    SimpleDateFormat sdf=new SimpleDateFormat("MMddHHmm");

    public static void main(String[] args) throws Exception {
        AsposeTest asposeTest=new AsposeTest();
        asposeTest.excel2pdf();
    }
    public ConvertStatus excel2pdf() throws Exception {
        String time=sdf.format(new Date());
        String excelpath="C://111.xls";
        String pdfpath="C://Users//"+time+".pdf";
        if (AsposeLicenseUtil.setCellsLicense()) {
            long start = System.currentTimeMillis();
            InputStream inputStream = new FileInputStream(new File(excelpath));
            OutputStream outputStream = new FileOutputStream(new File(pdfpath));
                
            Workbook workbook = new Workbook(inputStream);
            
            //workbook.Worksheets[0].VerticalPageBreaks
            
            Worksheet ws = workbook.getWorksheets().get(0);
            //ws.setPageBreakPreview(false);
            System.out.println("getHorizontalPageBreaks:"+ws.getHorizontalPageBreaks().getCount());
            System.out.println("vertical:"+ws.getVerticalPageBreaks().getCount());
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            
            
            ws.getHorizontalPageBreaks().clear();
            ws.getVerticalPageBreaks().clear();
            //workbook.save(outputStream, SaveFormat.PDF);
            workbook.save(outputStream, pdfSaveOptions);
            outputStream.flush();
            outputStream.close();

            // TODO 当excel宽度太大时,在PDF中会拆断并分页。此处如何等比缩放。
            
            // 将不同的sheet单独保存为pdf
            //Get the count of the worksheets in the workbook
            int sheetCount = workbook.getWorksheets().getCount();

            //Make all sheets invisible except first worksheet
            /*for (int i = 1; i < workbook.getWorksheets().getCount(); i++)
            {
                workbook.getWorksheets().get(i).setVisible(false);
            }*/
            // workbook.save(outputStream, SaveFormat.PDF);

            //Take Pdfs of each sheet
            /*for (int j = 0; j < workbook.getWorksheets().getCount(); j++)
            {
                Worksheet ws = workbook.getWorksheets().get(j);
                workbook.save("D:/Kory" + ws.getName() + ".pdf");

                if (j < workbook.getWorksheets().getCount() - 1)
                {
                    workbook.getWorksheets().get(j + 1).setVisible(true);
                    workbook.getWorksheets().get(j).setVisible(false);
                }
            }*/
            
            long end = System.currentTimeMillis();
            System.out.println("excel to pdf success");
            return ConvertStatus.SUCCESS;
            
        } else {
            System.out.println("excel to pdf error");
            return ConvertStatus.LICENSE_ERROR;
        }
    }
    
}

资源地址:excel 和doc 转换成pdf需要的jar

 

参考Aspose官网文档:convert excel to pdf

 

*

 

转载于:https://www.cnblogs.com/qingmaple/p/6644530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值