使用ASpose Cells for java,將excel 轉成pdf,會出現一個sheet分頁成多頁,並且規律奇怪java
能夠設置spa
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.net
參考Aspose官網文檔:convert excel to pdfexcel
*code