Print Excel Documents in Just 4 Steps Using C#
Tags: fileformats, csharp, excel, ui
Published: 2024-01-31 07:29:26

---
title: Print Excel Documents in Just 4 Steps Using C#
published: true
date: 2024-01-23 16:58:38 UTC
tags: fileformats, csharp, excel, ui
canonical_url: https://www.syncfusion.com/blogs/post/print-excel-in-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5llozn6os5phniggoi1c.png
---
Microsoft Excel is a trusted tool for organizing, calculating, and visualizing data. However, the process of converting spreadsheets into tangible documents can sometimes be less straightforward. This is where the Syncfusion [Excel Library](https://www.syncfusion.com/document-processing/excel-framework/net ".NET Excel Library") becomes invaluable, allowing you to print Excel documents without relying on any Microsoft Excel dependencies.

The [Syncfusion Excel Library](https://help.syncfusion.com/file-formats/xlsio/overview "Getting started with Syncfusion Excel Library") (Essential XlsIO) is a robust tool that facilitates the smooth creation, reading, and editing of Excel documents using C#. It supports Excel printing options by converting Excel files to PDF and printing that PDF document. The Excel document can be printed with a specified page setup and printer settings in XlsIO.

Let’s see how to print your Excel documents with the help of the Syncfusion Excel Library using C#!

## Excel to PDF

Our XlsIO library supports converting an [entire workbook or a single worksheet into a PDF document](https://www.syncfusion.com/document-processing/excel-framework/net/excel-to-pdf-conversion "Convert Excel to PDF in C# and VB.NET"), using the Syncfusion Excel-to-PDF converter. You can also employ various customization options such as embedding fonts, ignoring empty pages or worksheets, and showing or hiding headers and footers. This process is fast, reliable, and supported in hosting environments such as AWS, Google Cloud App, and Microsoft Azure web services.

This comprehensive library supports the following key features **:**

- Excel [workbook-to-PDF](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion "Workbook to PDF Conversion") conversion.
- Excel workbooks with [charts conversion to PDF](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion#excel-with-chart-to-pdf "Excel with Charts to PDF conversion").
- [Font substitution](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion#substitute-font-in-excel-to-pdf-conversion "Substitute Font in Excel to PDF Conversion") during Excel-to-PDF conversion.
- Multiple Excel-to-PDF [conversion options](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-converter-settings "Excel to PDF Converter Settings").

## Steps to print Excel documents using C#

1. Create a new WPF application.
2. Install and configure the Excel-to-PDF converter library.
3. Design the UI for selecting Excel documents.
4. Convert Excel file to PDF and print the document.

### Step 1: Create a new WPF app

First, we are going to [create a new WPF application in Visual Studio](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-wpf?view=vs-2022 "Create a new WPF app in Visual Studio").

Refer to the following image.

![Create a new WPF app in Visual Studio](https://www.syncfusion.com/blogs/wp-content/uploads/2024/01/Create-a-new-WPF-app-in-Visual-Studio.png)

### Step 2: Install and configure the Excel-to-PDF converter library

Then, add the following NuGet packages to your WPF app:

- [Syncfusion.ExcelToPDFConverter.Wpf](https://www.nuget.org/packages/Syncfusion.ExcelToPDFConverter.Wpf "Syncfusion.ExcelToPDFConverter.Wpf NuGet package")
- [System.Drawing.Common](https://www.nuget.org/packages/System.Drawing.Common "System.Drawing.Common NuGet package")

![Add the necessary Excel NuGet packages](https://www.syncfusion.com/blogs/wp-content/uploads/2024/01/Add-the-necessary-Excel-NuGet-packages.png)

### Step 3: Design the UI for selecting Excel documents

Add the required UI buttons for selecting Excel documents and performing the print operation in the **MainWindow.xaml**  file.

Refer to the following code example.

```xml
<Window x:Name="Print_Excel" x:Class="PrintExcelDocuments.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PrintExcelDocuments"
        mc:Ignorable="d"
        Title="MainWindow" Height="250" Width="532">
    <Grid Margin="3,-1,0,-6" Background="#FFE0E4EB">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="37*"/>
            <ColumnDefinition Width="21*"/>
            <ColumnDefinition Width="81*"/>
            <ColumnDefinition Width="68*"/>
            <ColumnDefinition Width="278*"/>
            <ColumnDefinition Width="44*"/>
        </Grid.ColumnDefinitions>
        <Button Click="PrintExcel" Content="Print" HorizontalAlignment="Left" VerticalAlignment="Top" Width="110" Height="27" Margin="47,111,0,0" FontWeight="Bold" Grid.Column="3" Grid.ColumnSpan="2"/>
        <Label Content="Select Excel File" HorizontalAlignment="Left" Margin="24,60,0,0" VerticalAlignment="Top" Width="114" Height="25" FontSize="14" Grid.ColumnSpan="3"/>
        <TextBox Name="filePath" HorizontalAlignment="Left" Margin="4,61,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="195" Height="24" Grid.Column="3" Grid.ColumnSpan="2"/>
        <Button Content="Browse" HorizontalAlignment="Left" Margin="152,61,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Click="SelectFile" Width="68" Height="24" FontWeight="Bold" FontStyle="Italic" Grid.Column="4"/>
    </Grid>
</Window>
```

After executing the previous code example, the UI will look like the following image.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/01/Print-window.png" alt="Print window" style="width:100%">

  <figcaption>Print window</figcaption>

</figure>

You can choose the Excel document by clicking **Browse** and then click **Print** to start the printing process.

### Step 4: Convert Excel file to PDF and print the document

Finally, add the following code in the **MainWindow.xaml.cs** file to convert the Excel document to PDF using the [Syncfusion Excel-to-PDF converter](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion "Excel to PDF Conversion") and print the document.

```csharp
using System.Drawing.Printing;
using System.Windows;
using Syncfusion.ExcelToPdfConverter;
using Syncfusion.XlsIO;

namespace PrintExcelDocuments
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SelectFile(object sender, RoutedEventArgs e)
        {
            //Initializes FileSavePicker.
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm,*.xltm,*.csv,*.tsv";
            openFileDialog.Title = "Select a Excel File";
            openFileDialog.ShowDialog();

            //Gets the path of specified file.
            filePath.Text = openFileDialog.FileName;            
        }

        private void PrintExcel(object sender, RoutedEventArgs e)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;

                //Loads or opens an existing workbook through Open method of IWorkbooks.
                IWorkbook workbook = application.Workbooks.Open(filePath.Text);

                //Initialize the printer settings.
                PrinterSettings printerSettings = new PrinterSettings();

                //Customizing the printer settings.
                printerSettings.PrinterName = "HP LaserJet Pro MFP M127-M128 PCLmS";
                printerSettings.Copies = 2;
                printerSettings.FromPage = 1;
                printerSettings.ToPage = 3;
                printerSettings.DefaultPageSettings.Color = true;
                printerSettings.Duplex = Duplex.Vertical;
                printerSettings.Collate = true;

                ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);

                converter.Print();
            }
        }
    }
}
```

The following image shows the input Excel document.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/01/Input-Excel-document-5.png" alt="Input Excel document" style="width:100%">

  <figcaption>Input Excel document</figcaption>

</figure>

By executing this code example, you will get a printed Excel document as shown in the following image.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2024/01/Printed-Excel-document.png" alt="Printed Excel document" style="width:100%">

  <figcaption>Printed Excel document</figcaption>

</figure>

## References

For more details, refer to the [print Excel documents using C# documentation](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion#print-excel-document "Printing Excel documents using C#") and [GitHub demos](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Real%20Time%20Examples%20in%20Excel/PrintExcelDocuments "Print Excel Documents in Just 4 Steps Using C# GitHub demo").

## Conclusion

Thanks for reading! In this blog, we explored the steps involved in printing Excel documents using C# with the [Syncfusion Excel Library](https://www.syncfusion.com/excel-framework/net ".NET Excel Library"). Additionally, we learned how to convert an Excel document to a PDF document in C# using our Syncfusion [Excel-to-PDF converter library](https://www.syncfusion.com/document-processing/excel-framework/net/excel-to-pdf-conversion "Excel to PDF Conversion"). With Essential XlsIO, you also have the ability to export Excel data to [images](https://help.syncfusion.com/file-formats/xlsio/worksheet-to-image-conversion "Worksheet to Image conversion in Excel Library"), [data tables](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-data-table "Exporting from Worksheet to Data Table"), [CSV](https://www.syncfusion.com/kb/9092/convert-excel-file-to-csv-in-c-vb-net "KB: Convert Excel file to CSV in C#, VB.NET"), [TSV](https://www.syncfusion.com/kb/8556/does-xlsio-support-tsv-files "KB: Does XlsIO support TSV files?"), [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet?_ga=2.19528508.561950334.1598519523-255771547.1598519523#save-worksheet-as-html "Save Worksheet as HTML"), [collections of objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-clr-objects "Working with Excel Data"), [ODS](https://help.syncfusion.com/file-formats/xlsio/excel-to-ods-conversion "Excel to ODS Conversion") , [JSON](https://help.syncfusion.com/file-formats/xlsio/excel-to-json-conversion "Excel to JSON Conversion"), and other file formats.

Feel free to try out these methods and share your feedback in the comments section of this blog post!

For existing customers, the new version of Essential Studio is available for download from the [License and Downloads](https://www.syncfusion.com/account/downloads "Essential Studio License and Downloads page") page. If you are not yet a Syncfusion customer, you can try our 30-day [free trial](https://www.syncfusion.com/downloads "Get the free 30-day evaluation of Essential Studio products") to check out our available features.

For questions, you can contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/excel "Syncfusion Feedback Portal"). We are happy to assist you!

## Related blogs

- [Merge Multiple Excel Files into One in Just 3 Steps Using C#](https://www.syncfusion.com/blogs/post/merge-multiple-excel-csharp.aspx "Blog: Merge Multiple Excel Files into One in Just 3 Steps Using C#")
- [6 Easy Ways to Export Data to Excel in C#](https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx "Blog: 6 Easy Ways to Export Data to Excel in C#")
- [Seamlessly Import and Export CSV Data in Excel Using C#](https://www.syncfusion.com/blogs/post/import-export-csv-data-in-excel-csharp.aspx "Blog: Seamlessly Import and Export CSV Data in Excel Using C#")
- [How to Export Data from SQL Server to Excel Table in C#](https://www.syncfusion.com/blogs/post/export-data-from-sql-server-to-excel-in-c-sharp.aspx "Blog: How to Export Data from SQL Server to Excel Table in C#")
- [Export Data from Collection to Excel and Group It in C# ](https://www.syncfusion.com/blogs/post/export-data-from-collection-to-excel-and-group-csharp.aspx "Blog: Export Data from Collection to Excel and Group It in C#")
- [Export Data to a Predefined Excel Template in C#](https://www.syncfusion.com/blogs/post/export-data-to-a-predefined-excel-template-in-c.aspx "Blog: Export Data to a Predefined Excel Template in C#")
- [Easy Steps to Export HTML Tables to an Excel in C#](https://www.syncfusion.com/blogs/post/easy-steps-to-export-html-tables-to-an-excel-worksheet-in-c.aspx "Blog: Easy Steps to Export HTML Tables to an Excel in C#")