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; } } }
参考Aspose官网文档:convert excel to pdf
*