Back to Devexpress

How to: Print a Workbook

windowsforms-119920-controls-and-libraries-spreadsheet-examples-printing-how-to-print-a-workbook.md

latest4.4 KB
Original Source

How to: Print a Workbook

  • May 05, 2022
  • 2 minutes to read

The SpreadsheetControl provides the following methods to print a workbook or individual worksheets.

MethodDescription
SpreadsheetControl.PrintPrints a document using the default or custom printer settings defined by the PrinterSettings class instance.
Sheet.PrintPrints a particular sheet in the document.
SpreadsheetControl.ShowPrintDialogInvokes the Print dialog that allows you to select a printer and change print settings.
csharp
using DevExpress.Spreadsheet;
using System.Drawing.Printing;
// ...

// Load a document into SpreadsheetControl.
spreadsheetControl.Document.LoadDocument("Documents\\Document.xlsx");

// Create an object containing 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 document using the specified printer settings.
spreadsheetControl.Print(printerSettings);
vb
Imports DevExpress.Spreadsheet
Imports System.Drawing.Printing
' ...

' Load a document into SpreadsheetControl.
spreadsheetControl.Document.LoadDocument("Documents\Document.xlsx")

' Create an object containing 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 document using the specified printer settings.
spreadsheetControl.Print(printerSettings)

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.

Call the SpreadsheetControl.ShowPrintPreview or SpreadsheetControl.ShowRibbonPrintPreview method to invoke the Print Preview window that enables previewing the document before printing. See How to: Show a Print Preview Form for a Document for more details.

See Also

Print Spreadsheet Documents

How to: Specify Print Settings

How to: Show a Print Preview Form for a Document