GcExcel在V4版本中提供了追踪导出进度的新功能。该功能可以在导出PDF的时候实时监听导出的进度。
GcExcel在PdfSaveOptions类中提供getPagePrinting和getPagePrinted事件来跟踪工作簿到PDF的导出进度。
下面的代码用于显示工作簿导出PDF的进度。
- //创建pdf文件流
- FileOutputStream outputStream = null;
- try {
- outputStream = new FileOutputStream("PagePrintEventsTrackProgress.pdf");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- // 创建新的工作簿
- Workbook workbook = new Workbook();
- IWorksheet activeSheet = workbook.getActiveSheet();
- activeSheet.getRange("A1").setValue(1);
- activeSheet.getRange("A2:A100").setFormulaR1C1("=R[-1]C+1");
- PdfSaveOptions options = new PdfSaveOptions();
- options.getPagePrintingEvent().addListener(
- (sender, e) -> System.out.println(String.format("Printing page %1$s of %2$s", e.getPageNumber(), e.getPageCount())));
- activeSheet.getPageSetup().setCenterHeader("Page &P of &N");
- workbook.save(outputStream, options);
- // 关闭文件流
- try {
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
复制代码 还可以用setSkipThisPage方法在导出时跳过页面。
例如:
- // 创建pdf文件流
- FileOutputStream outputStream = null;
- try {
- outputStream = new FileOutputStream("PagePrintEventsSkipPage.pdf");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- // 创建新的工作簿
- Workbook workbook = new Workbook();
- IWorksheet activeSheet = workbook.getActiveSheet();
- activeSheet.getRange("A1").setValue(1);
- activeSheet.getRange("A2:A100").setFormulaR1C1("=R[-1]C+1");
- PdfSaveOptions options = new PdfSaveOptions();
- options.getPagePrintingEvent().addListener((sender, e) -> {
- if (e.getPageNumber() == 2) {
- e.setSkipThisPage(true);
- }
- });
- activeSheet.getPageSetup().setCenterHeader("Page &P of &N");
- workbook.save(outputStream, options);
- // 关闭文件流
- try {
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
复制代码 最后提供了setHasMorePages方法退出PDF导出。
- // 创建pdf文件流
- FileOutputStream outputStream = null;
- try {
- outputStream = new FileOutputStream("PagePrintEventsExitPrinting.pdf");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- // 创建新的工作簿
- Workbook workbook = new Workbook();
- IWorksheet activeSheet = workbook.getActiveSheet();
- activeSheet.getRange("A1").setValue(1);
- activeSheet.getRange("A2:A100").setFormulaR1C1("=R[-1]C+1");
- PdfSaveOptions options = new PdfSaveOptions();
- options.getPagePrintedEvent().addListener((sender, e) -> {
- if (e.getPageNumber() == 2) {
- e.setHasMorePages(false);
- }
- });
- activeSheet.getPageSetup().setCenterHeader("Page &P of &N");
- workbook.save(outputStream, options);
- // 关闭文件流
- try {
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
复制代码
|
|