打印和预览工作簿
打印简介
Microsoft Excel假定您想打印整个工作表区域,除非您指定了选择区域。要使用Aspose.Cells进行打印,首先将Aspose.Cells.Rendering命名空间导入到程序中。它有几个有用的类,例如SheetRender和WorkbookRender
使用SheetRender进行打印
Aspose.Cells.Rendering.SheetRender类表示一个工作表,其中包含一个用于打印工作表的ToPrinter方法。以下示例代码显示如何打印一个工作表
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a workbook with Excel file. | |
Workbook workbook = new Workbook(dataDir + "SampleBook.xlsx"); | |
string printerName = ""; | |
while (string.IsNullOrEmpty(printerName) && string.IsNullOrWhiteSpace(printerName)) | |
{ | |
Console.WriteLine("Please Enter Your Printer Name:"); | |
printerName = Console.ReadLine(); | |
} | |
// Define a worksheet. | |
Worksheet worksheet; | |
// Get the second sheet. | |
worksheet = workbook.Worksheets[1]; | |
// Apply different Image/Print options. | |
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions(); | |
options.PrintingPage = PrintingPageType.Default; | |
SheetRender sr = new SheetRender(worksheet, options); | |
Console.WriteLine("Printing SampleBook.xlsx"); | |
// Print the sheet. | |
try | |
{ | |
sr.ToPrinter(printerName); | |
Console.WriteLine("Pinting finished."); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} |
使用WorkbookRender进行打印
要打印整个工作簿,迭代工作表并打印它们,或使用WorkbookRender类。
// 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(); | |
// Instantiate a workbook with an Excel file. | |
Workbook workbook = new Workbook(sourceDir + "samplePrintingUsingWorkbookRender.xlsx"); | |
string printerName = "doPDF 8"; | |
// Apply different Image/Print options. | |
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions(); | |
options.ImageType = Drawing.ImageType.Tiff; | |
options.PrintingPage = PrintingPageType.Default; | |
// To print a whole workbook, iterate through the sheets and print them, or use the WorkbookRender class. | |
WorkbookRender wr = new WorkbookRender(workbook, options); | |
try | |
{ | |
// Print the workbook. | |
wr.ToPrinter(printerName); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} |
打印预览
可能有一些情况下,需要将数百万页的Excel文件转换为PDF或图像。处理这些文件将消耗大量时间和资源。在这种情况下,工作簿和工作表打印预览功能可能会很有用。在转换这样的文件之前,用户可以检查总页数,然后决定是否转换文件。本文侧重于使用WorkbookPrintingPreview和SheetPrintingPreview类来查找总页数。
Aspose.Cells提供打印预览功能。为此,API提供WorkbookPrintingPreview和SheetPrintingPreview类。要创建整个工作簿的打印预览,通过向构造函数传递Workbook和ImageOrPrintOptions对象创建WorkbookPrintingPreview类的实例。WorkbookPrintingPreview类提供了一个EvaluatedPageCount方法,返回生成预览中的页数。类似于WorkbookPrintingPreview类,SheetPrintingPreview类用于为特定工作表生成打印预览。要创建工作表的打印预览,通过向构造函数传递Worksheet和ImageOrPrintOptions对象创建SheetPrintingPreview类的实例。SheetPrintingPreview类还提供一个EvaluatedPageCount方法,返回生成预览的页数。
以下代码片段演示了通过使用示例Excel文件(94896177.xlsx)使用WorkbookPrintingPreview和SheetPrintingPreview类。
示例代码
// 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(); | |
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx"); | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
WorkbookPrintingPreview preview = new WorkbookPrintingPreview(workbook, imgOptions); | |
Console.WriteLine("Workbook page count: " + preview.EvaluatedPageCount); | |
SheetPrintingPreview preview2 = new SheetPrintingPreview(workbook.Worksheets[0], imgOptions); | |
Console.WriteLine("Worksheet page count: " + preview2.EvaluatedPageCount); |
执行上述示例代码生成的输出如下。
控制台输出
Workbook page count: 1
Worksheet page count: 1
高级主题
- 为呈现电子表的字体进行配置
- 将工作表转换为图像-去除数据周围的空白
- 将工作表转为图像以及按页面转为图像
- 使用ImageOrPrint Options将工作表转换为图像
- 导出工作表中的单元格范围为图像
- 使用所需的宽度和高度将工作表或图表导出为图像
- 使用ImageOrPrintOptions从工作表中提取图像
- 生成工作表的缩略图
- 当没有要打印的内容时输出空白页
- 页面设置和打印选项
- 使用SheetRender和WorkbookRender打印一系列页面
- 使用ImageOrPrintOptions的PageIndex和PageCount属性呈现页面序列
- 将工作表渲染到图形上下文
- 指定工作簿渲染的个体或私有字体集
- 在使用 Aspose.Cells 打印时指定作业或文档名称