ワークブックの印刷とプレビュー

印刷の概要

Microsoft Excelは、選択を指定しない限り、ワークシート全体を印刷すると仮定しています。Aspose.Cellsを使用して印刷するには、まずAspose.Cells.Rendering名前空間をプログラムにインポートします。これにはいくつかの有用なクラスがあり、たとえば、SheetRenderWorkbookRenderがあります。

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または画像に変換する必要がある場合があります。このようなファイルを処理すると、多くの時間とリソースが消費されます。そのような場合に、ワークブックおよびワークシートの印刷プレビュー機能が役立つ可能性があります。ファイルを変換する前に、ユーザーは総ページ数を確認し、ファイルを変換するかどうかを決定できます。この記事では、WorkbookPrintingPreviewSheetPrintingPreview クラスを使用して、生成されたプレビューの総ページ数を調べる方法に焦点を当てています。

Aspose.Cellsは印刷プレビュー機能を提供しています。APIはWorkbookPrintingPreviewおよびSheetPrintingPreviewクラスを提供しています。ワークブック全体の印刷プレビューを作成するには、WorkbookImageOrPrintOptionsオブジェクトをコンストラクタに渡してWorkbookPrintingPreviewクラスのインスタンスを作成します。WorkbookPrintingPreviewクラスは、生成されたプレビューのページ数を返すEvaluatedPageCountメソッドを提供します。WorkbookPrintingPreviewクラスに類似して、SheetPrintingPreviewクラスは特定のワークシートの印刷プレビューを生成するために使用されます。ワークシートの印刷プレビューを作成するには、WorksheetImageOrPrintOptionsオブジェクトをコンストラクタに渡してSheetPrintingPreviewクラスのインスタンスを作成します。SheetPrintingPreviewクラスもまた、生成されたプレビューのページ数を返すEvaluatedPageCountメソッドを提供します。

次のコードスニペットは、サンプルExcelファイルを使用して、WorkbookPrintingPreviewSheetPrintingPreviewクラスの両方の使用方法を示しています。

サンプルコード

// 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

高度なトピック