officefileapi-119922-spreadsheet-document-api-examples-printing-how-to-print-a-workbook.md
Use the following methods to print a document:
| Method | Description |
|---|---|
| Workbook.Print | Prints the entire workbook or individual sheets and allows you to specify printer settings. |
| Sheet.Print | Prints the current sheet. |
Warning
The Print method overloads with PrinterSettings printerSettings parameter work only on Windows OS. The PlatformNotSupportedException is thrown on other operating systems. Use the Print method overloads with DXPrinterSettings printerSettings parameter to print on other operating systems.
Use the WorksheetView and WorksheetPrintOptions objects’ properties to define page options and specify print settings.
Tip
Handle the Workbook.BeforePrintSheet event to cancel printing the specific worksheet(s).
using DevExpress.Spreadsheet;
using System.Drawing.Printing;
// ...
// Create a new Workbook object.
Workbook workbook = new Workbook();
// Load a document from a file.
workbook.LoadDocument("Documents\\Document.xlsx");
// Create an object that contains printer settings.
PrinterSettings printerSettings = new PrinterSettings();
// Define the printer to use.
printerSettings.PrinterName = "Microsoft Print to PDF";
printerSettings.PrintToFile = true;
printerSettings.PrintFileName = "Documents\\PrintedDocument.pdf";
// Specify that the first three pages should be printed.
printerSettings.PrintRange = PrintRange.SomePages;
printerSettings.FromPage = 1;
printerSettings.ToPage = 3;
// Set the number of copies to print.
printerSettings.Copies = 1;
// Print the workbook using the specified printer settings.
workbook.Print(printerSettings);
Imports DevExpress.Spreadsheet
Imports System.Drawing.Printing
' ...
' Create a new Workbook object.
Dim workbook As New Workbook()
' Load a document from a file.
workbook.LoadDocument("Documents\Document.xlsx")
' Create an object that contains printer settings.
Dim printerSettings As New PrinterSettings()
' Define the printer to use.
printerSettings.PrinterName = "Microsoft Print to PDF"
printerSettings.PrintToFile = True
printerSettings.PrintFileName = "Documents\PrintedDocument.pdf"
' Specify that the first three pages should be printed.
printerSettings.PrintRange = PrintRange.SomePages
printerSettings.FromPage = 1
printerSettings.ToPage = 3
' Set the number of copies to print.
printerSettings.Copies = 1
' Print the workbook using the specified printer settings.
workbook.Print(printerSettings)
Note
The Margins or Landscape property (accessible by the PrinterSettings.DefaultPageSettings property) do not affect the printed workbook’s layout. Use the WorksheetView and WorksheetPrintOptions class properties specify various printout options to control how the document is printed. Refer to the following article for a code sample: How to: Specify Print Settings.
using DevExpress.Spreadsheet;
using System.Drawing.Printing;
// ...
// Create a new Workbook object.
Workbook workbook = new Workbook();
// Load a document from a file.
workbook.LoadDocument("Documents\\Document.xlsx");
// Create an object that contains printer settings.
PrinterSettings printerSettings = new PrinterSettings();
// Define the printer to use.
printerSettings.PrinterName = "Microsoft Print to PDF";
printerSettings.PrintToFile = true;
printerSettings.PrintFileName = "PrintedDocument.pdf";
// Print specific worksheets in the document.
workbook.Print(printerSettings, "Sheet1", "Sheet2");
Imports DevExpress.Spreadsheet
Imports System.Drawing.Printing
' ...
' Create a new Workbook object.
Dim workbook As New Workbook()
' Load a document from a file.
workbook.LoadDocument("Documents\Document.xlsx")
' Create an object that contains printer settings.
Dim printerSettings As New PrinterSettings()
' Define the printer to use.
printerSettings.PrinterName = "Microsoft Print to PDF"
printerSettings.PrintToFile = True
printerSettings.PrintFileName = "PrintedDocument.pdf"
' Print specific worksheets in the document.
workbook.Print(printerSettings, "Sheet1", "Sheet2")
Use the Print method overloads with the DXPrinterSettings printerSettings parameter to print workbooks and worksheets in non-Windows environments (macOS and Unix-based systems that support printing through Common UNIX Printing System (CUPS)).
Note
Install the libcups2 package separately to enable printing.
The following code sample uses the DXPrinterSettings options to print a workbook and individual worksheets:
using DevExpress.Drawing.Printing;
using DevExpress.Spreadsheet;
// ...
// Create a new Workbook object.
Workbook workbook = new Workbook();
// Load a document from a file.
workbook.LoadDocument("Documents\\Document.xlsx");
// Create an object that contains printer settings.
DXPrinterSettings printerSettings = new DXPrinterSettings();
// Define the printer to use.
printerSettings.PrinterName = "Microsoft Print to PDF";
// Specify that the first three pages should be printed.
printerSettings.PageRange = "1-3";
// Set the number of copies to print.
printerSettings.Copies = 1;
// Print the workbook using the specified printer settings.
workbook.Print(printerSettings);
// Print specific worksheets in the document.
workbook.Print(printerSettings, "Sheet1", "Sheet2");
Imports DevExpress.Drawing.Printing
Imports DevExpress.Spreadsheet
' ...
' Create a new Workbook object.
Private workbook As New Workbook()
' Load a document from a file.
workbook.LoadDocument("Documents\Document.xlsx")
' Create an object that contains printer settings.
Dim printerSettings As New DXPrinterSettings()
' Define the printer to use.
printerSettings.PrinterName = "Microsoft Print to PDF"
' Specify that the first three pages should be printed.
printerSettings.PageRange = "1-3"
' Set the number of copies to print.
printerSettings.Copies = 1
' Print the workbook using the specified printer settings.
workbook.Print(printerSettings)
' Print specific worksheets in the document.
workbook.Print(printerSettings, "Sheet1", "Sheet2")
The default calculation mode for a Workbook is Manual. This mode implies that the Spreadsheet component does not calculate formulas before it prints a document. Call the Workbook.Calculate or Workbook.CalculateFull method to calculate all formulas in the workbook.
using (Workbook workbook = new Workbook())
{
// Load a document.
// ...
// Modify the document.
// ...
// Calculate formulas in the document.
workbook.Calculate();
// Print the document.
workbook.Print();
}
Using workbook As New Workbook()
' Load a document.
' ...
' Modify the document.
' ...
' Calculate formulas in the document.
workbook.Calculate()
' Print the document.
workbook.Print()
End Using
See Also
How to: Specify Print Settings