Stampa e anteprima del libro

Introduzione alla stampa

Microsoft Excel presume che si desideri stampare l’intera area del foglio di lavoro a meno che non si specifichi una selezione. Per stampare utilizzando Aspose.Cells, prima importa il namespace Aspose.Cells.Rendering nel programma. Ha diverse classi utili, ad esempio, SheetRender e WorkbookRender.

Stampa utilizzando SheetRender

La classe Aspose.Cells.Rendering.SheetRender rappresenta un foglio di lavoro e ha il metodo ToPrinter che può stampare un foglio di lavoro. Il seguente codice di esempio mostra come stampare un foglio di lavoro.

// 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);
}

Stampa utilizzando WorkbookRender

Per stampare un intero workbook, iterare attraverso i fogli e stamparli, o utilizzare la classe 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);
}

Anteprima di stampa

Potrebbero esserci casi in cui file di Excel con milioni di pagine devono essere convertiti in PDF o immagini. Il processo di tali file consumerà molto tempo e risorse. In tali situazioni, la funzione di Anteprima di stampa del Workbook e del Foglio di lavoro potrebbe rivelarsi utile. Prima di convertire tali file, l’utente può controllare il numero totale di pagine e poi decidere se convertire il file o meno. Questo articolo si concentra sull’utilizzo delle classi WorkbookPrintingPreview e SheetPrintingPreview per scoprire il numero totale di pagine.

Aspose.Cells fornisce la funzione di anteprima di stampa. A questo scopo, l’API fornisce le classi WorkbookPrintingPreview e SheetPrintingPreview. Per creare l’anteprima di stampa dell’intero workbook, creare un’istanza della classe WorkbookPrintingPreview passando gli oggetti Workbook e ImageOrPrintOptions al costruttore. La classe WorkbookPrintingPreview fornisce un metodo EvaluatedPageCount che restituisce il numero di pagine nell’anteprima generata. Similmente alla classe WorkbookPrintingPreview, la classe SheetPrintingPreview viene utilizzata per generare un’anteprima di stampa per un foglio di lavoro specifico. Per creare l’anteprima di stampa di un foglio di lavoro, creare un’istanza della classe SheetPrintingPreview passando gli oggetti Worksheet e ImageOrPrintOptions al costruttore. La classe SheetPrintingPreview fornisce anche un metodo EvaluatedPageCount che restituisce il numero di pagine nell’anteprima generata.

Il seguente frammento di codice dimostra l’utilizzo sia delle classi WorkbookPrintingPreview che SheetPrintingPreview utilizzando il file Excel di esempio.

Codice di Esempio

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

Quello che segue è l’output generato eseguendo il codice sopra.

Output della console

Workbook page count: 1
Worksheet page count: 1

Argomenti avanzati