Document Solutions for Excel, .NET Edition | Document Solutions
Features / Print
In This Topic
    Print
    In This Topic

    DsExcel.NET allows you to output a workbook or worksheet to a printer using the PrintOut method provided by IWorkbook and IWorksheet interface respectively. This method accepts object of PrintOutOptions class as a parameter. In the background, PrintOut method uses DsPdf to print workbook to a printer. Hence, you need to reference DS.Documents.Imaging.Windows to implement the feature.

    Note: The PrintOut method works only on the Windows environment. To print in other environments, you can export the document to PDF and then take a print of the converted document. For information on exporting a spreadsheet to PDF, see Export to PDF.

    Print Options

    DsExcel.NET provides PrintOutOptions class that contains various properties and events to set printing options such as printer name, number of copies, double-sided printing, first and last page to be printed etc. To print a document with specified printing options, you need to create an instance of the PrintOutOptions class, define the options for that instance and pass that instance while calling the PrintOut method.

    Refer to the following example code to generate multiple copies of double-sided print for a workbook:

    C#
    Copy Code
    // Create a workbook instance.
    var workbook = new Workbook();
    
    // Add data for table.
    object[,] data = new object[,]{
    {"Name", "City", "Birthday", "Eye color", "Weight", "Height"},
    {"Richard", "New York", new DateTime(1968, 6, 8), "Blue", 67, 165},
    {"Nia", "New York", new DateTime(1972, 7, 3), "Brown", 62, 134},
    {"Jared", "New York", new DateTime(1964, 3, 2), "Hazel", 72, 180},
    {"Natalie", "Washington", new DateTime(1972, 8, 8), "Blue", 66, 163},
    {"Damon", "Washington", new DateTime(1986, 2, 2), "Hazel", 76, 176},
    {"Angela", "Washington", new DateTime(1993, 2, 15), "Brown", 68, 145}
    };
    // Create a worksheet instance.
    IWorksheet worksheet = workbook.Worksheets[0];
    
    // Bind data with table.
    worksheet.Range["A1:F7"].Value = data;
    worksheet.Range["A:F"].ColumnWidth = 12;
    
    // Add table.
    worksheet.Tables.Add(worksheet.Range["A1:F7"], true);
    
    // Create an instance of PrintOutOptions class.
    PrintOutOptions options = new PrintOutOptions();
    
    // Set name of the target printer.
    options.ActivePrinter = "[Real printer name]";
    
    // Print 3 copies.
    options.Copies = 3;
    
    // Print on double sides vertically.
    options.Duplex = Duplex.Vertical;
    
    // Print this workbook.
    workbook.PrintOut(options);
    
    // Save to an excel file.
    workbook.Save("printworkbook.xlsx");
    Note: The double-sided printing option only works if the printer has double-sided printing functionality.