zoukankan      html  css  js  c++  java
  • 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

    *

    有问题在公众号【清汤袭人】找我,时常冒出各种傻问题,然一通百通,其乐无穷,一起探讨


  • 相关阅读:
    oc之数组反序输出示例
    OC--有这么一个 整数 123456789,如何将这个整数的每一位数,从末位开始依次放入数组中,并遍历 倒序输出字符串
    oc--截取字符串(从网址中截取用户名和密码)
    iOS 第七期考核题(字符串以及字典的使用 数组内容转换成字符串)
    iOS 第六期考核题(字典的使用)
    iOS 第五期考核题(字典与数组嵌套,字典的排序/删除)
    Linux服务启动报错日志分析
    新手Linux命令-1
    新手Linux命令-2
    计划任务服务
  • 原文地址:https://www.cnblogs.com/qingmaple/p/6644530.html
Copyright © 2011-2022 走看看