跟踪文档转换进度
Contents
[
Hide
]
可能的使用场景
有时转换大型Excel文件可能需要一些时间。在此期间,您可能希望显示文档转换进度,而不只是加载屏幕,以增强应用程序的可用性。Aspose.Cells支持通过提供IPageSavingCallback接口来跟踪文档转换进程。IPageSavingCallback接口提供了PageStartSaving和PageEndSaving方法,您可以在自定义类中实现。还可以控制呈现哪些页面,如TestPageSavingCallback自定义类所示。
跟踪文档转换进度
以下代码示例加载源Excel文件并通过使用实现IPageSavingCallback接口的TestPageSavingCallback自定义类将其转换进度打印到控制台。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
Workbook workbook = new Workbook(sourceDir + "PagesBook1.xlsx"); | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
pdfSaveOptions.PageSavingCallback = new TestPageSavingCallback(); | |
workbook.Save(outputDir + "DocumentConversionProgress.pdf", pdfSaveOptions); |
以下是TestPageSavingCallback自定义类的代码。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public class TestPageSavingCallback : IPageSavingCallback | |
{ | |
public void PageStartSaving(PageStartSavingArgs args) | |
{ | |
Console.WriteLine("Start saving page index {0} of pages {1}", args.PageIndex, args.PageCount); | |
//don't output pages before page index 2. | |
if (args.PageIndex < 2) | |
{ | |
args.IsToOutput = false; | |
} | |
} | |
public void PageEndSaving(PageEndSavingArgs args) | |
{ | |
Console.WriteLine("End saving page index {0} of pages {1}", args.PageIndex, args.PageCount); | |
//don't output pages after page index 8. | |
if (args.PageIndex >= 8) | |
{ | |
args.HasMorePages = false; | |
} | |
} | |
} |
控制台输出
Start saving page index 0 of pages 11</br>
End saving page index 0 of pages 11</br>
Start saving page index 1 of pages 11</br>
End saving page index 1 of pages 11</br>
Start saving page index 2 of pages 11</br>
End saving page index 2 of pages 11</br>
Start saving page index 3 of pages 11</br>
End saving page index 3 of pages 11</br>
Start saving page index 4 of pages 11</br>
End saving page index 4 of pages 11</br>
Start saving page index 5 of pages 11</br>
End saving page index 5 of pages 11</br>
Start saving page index 6 of pages 11</br>
End saving page index 6 of pages 11</br>
Start saving page index 7 of pages 11</br>
End saving page index 7 of pages 11</br>
Start saving page index 8 of pages 11</br>
End saving page index 8 of pages 11