Print Excel Documents in Just 4 Steps Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Print Excel Documents in Just 4 Steps Using C#

Print Excel Documents in Just 4 Steps Using C#

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 becomes invaluable, allowing you to print Excel documents without relying on any Microsoft Excel dependencies.

The 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#!

Enjoy a smooth experience with Syncfusion’s Excel Library! Get started with a few lines of code and without Microsoft or interop dependencies.

Excel to PDF

Our XlsIO library supports converting an entire workbook or a single worksheet into a PDF document, 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:

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.

Refer to the following image.

Create a new WPF app in Visual Studio

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

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

Add the necessary Excel NuGet packages

Handle Excel files like a pro with Syncfusion’s C# Excel Library, offering well-documented APIs for each functionality.

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.

<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.

Print window
Print window

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

Witness the possibilities in demos showcasing the robust features of Syncfusion’s C# Excel Library.

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 and print the document.

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.

Input Excel document
Input Excel document

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

Printed Excel document
Printed Excel document

References

For more details, refer to the print Excel documents using C# documentation and GitHub demos.

Trusted by industry giants worldwide, Syncfusion's Excel Framework has a proven track record of reliability and excellence.

Conclusion

Thanks for reading! In this blog, we explored the steps involved in printing Excel documents using C# with the Syncfusion 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. With Essential XlsIO, you also have the ability to export Excel data to images, data tables, CSVTSVHTMLcollections of objectsODS , JSON, 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 page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out our available features.

For questions, you can contact us through our support forumsupport portal, or feedback portal. We are happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed