Skip to main content

How to: Print a Workbook

  • 3 minutes to read

Warning

The Print method works only on Windows OS. The PlatformNotSupportedException is thrown on other operating systems.

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.

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

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

Calculate Formulas Before Print Operation

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();
}
See Also