• 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

    *

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


  • 相关阅读:
    对象与内存控制1---实例变量和类变量
    数组与内存控制2--数组使用
    数组与内存控制1--数组初始化
    Java 三大特征之--多态
    简述Java面向对象三大特征:封装、继承、多态
    java程序初始化的顺序
    关于public static void main(String[] args)相关知识
    Java的优点
    前端面试攻略3------HTML和CSS部分
    前端面试攻略2------计算机网络部分
  • 原文地址:https://www.cnblogs.com/qingmaple/p/6644530.html
Copyright © 2020-2023  润新知