When it comes to building a seamless Windows Desktop Application, C# is the language of choice by developers. Today we’ll show the benefits of using Spread.NET written in C# (specifically, the importance of how to import and export Excel Spreadsheets using Spread.NET). This functionality provides users the ability to import Excel spreadsheets and then export the sheets to other file formats. This becomes incredibly useful when referencing large data sources in your projects or converting projects. Not only does this provide a very useful functionality, but it will also save you and your users an exponential amount of time.

Import and Export Invoices

In this walkthrough, we’ll use a pre-existing template that is available in Excel, specifically for invoices. When working in any business setting, it is common for one to send and receive invoices. That being said, these are important documents that play a big part in keeping track of expenses. Not only will it be useful to import existing invoices so that you may make changes and then send off for approval, but you'll also be able to export said invoices to PDF so that they are not able to be changed/edited.

Let’s start with opening Visual Studio .NET. Next, from the File menu, choose New, and then Project. In the New Project dialog, in the Installed area, select a project type depending on the language environment in which you are developing. For example, choose Windows under C#. Choose the type of project such as Windows Forms Application. In the Name box, type the name of your new project. The default name is WindowsApplication1 for the first Windows Forms application that you create. Next, in the Location box, leave the location path as the designated path, or click Browse to change the path to a new directory. Lastly, click OK.

The project will display in the Solution Explorer. If your project does not display the Solution Explorer, from the View menu, choose Solution Explore. Choose Rename from the pop-up menu, and then type the new form name you prefer for the new form name.

Now, you are ready to add the control to Visual Studio. In the Solution Explorer, right-click on the form name, Form1. If the Toolbox is not displayed, go to the View menu choose Toolbox. Once the Toolbox is displayed, look in the GrapeCity Spread category (or in any other category if you have installed Spread and placed the toolbox icon in a different category).

The Spread component will appear in the Toolbox. If the Spread component is not in the Toolbox: Right-click in the Toolbox, pop-up menu select “Choose Items.” In the Choose Toolbox Items dialog, click the .NET Framework Components tab. Now, once in the .NET Framework Components tab, the FpSpread component (in the FarPoint.Win.Spread namespace) should be displayed in the list of components. First, select the Spread component check box and click OK. Then, select FpChart (FarPoint.Win.Chart namespace) for the chart control.

The Spread components will be displayed in the drop-down list of components. If the Spread component is not displayed in the list of components, click Browse -and browse to the installation path for the Spread Windows Forms component. Once there, select the FarPoint.Win.Spread.dll and click Open. The Spread component is now displayed in the list of components. Select it and click OK. Select FarPoint.Win.Chart.dll for the chart control.

The next step is to add the Spread component to a project. With an open project, in the Toolbox under GrapeCity Spread (or whatever category to which you added it), select the FpSpread component. On your Windows Forms page, draw a Spread component by dragging a rectangle the size that you would like the initial component or simply double-click on the page. The Spread component appears. The Spread Designer also appears by default. Close the designer.

Your project should look similar to this:

Drag Spread Control onto form. Now that the controls are added to the project, add two buttons to the window. One for importing and one for exporting files into Spread. First open your Toolbox and then under common controls, click and drag two buttons on the Form.cs[Design]. Then open the Form.Designer.cs and use the following code to sure that the buttons are formatted and labeled correctly:

            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(477, 431);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(108, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Open";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Anchor =    ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.button2.Location = new System.Drawing.Point(607, 431);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(108, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "Export to PDF";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(721, 431);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "Export to XLSX/XLS";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);

This is what your control should look like with the controls added onto the form:

Next, you will need to assign the import and export functionality to each button within the Form.cs sheet by implementing the following code:

            private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel 97-2007 Workbook(*.xls)|*.xls|Excel Workbook(*.xlsx)|*.xlsx";
            openFileDialog.FilterIndex = 2;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string _fileName = openFileDialog.FileName;
                bool ret = spread.OpenExcel(_fileName);


            }
        }
        SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Pdf file (*.pdf)| *.pdf";
            saveFileDialog.Title = "Save Spread to PDF Format.";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {

                __printInfo.Centering = Centering.Both;
                _printInfo.ShowColor = true;
                _printInfo.ShowColumnHeader = PrintHeader.Hide;
                _printInfo.ShowRowHeader = PrintHeader.Hide;
                _printInfo.SmartPrintPagesTall = 1;
                _printInfo.SmartPrintPagesWide = 1;
                _printInfo.UseSmartPrint = true;
                _printInfo.PrintToPdf = true;
                _printInfo.PdfWriteMode = PdfWriteMode.New;
                _printInfo.PdfWriteTo = PdfWriteTo.File;
                _printInfo.PdfFileName = saveFileDialog.FileName;
                _printInfo.PrintType = PrintType.All;
                spread.Sheets[0].PrintInfo = _printInfo;
                spread.PrintSheet(0);


                MessageBox.Show("Sheet exported as PDF ");
            }
        }
            private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog _saveFileDialog = new SaveFileDialog();
            _saveFileDialog.Filter = "Excel 97-2007 Workbook(*.xls)|*.xls|Excel Workbook(*.xlsx)|*.xlsx";
            _saveFileDialog.FilterIndex = -1;
            if (_saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string _fileName = _saveFileDialog.FileName;
                spread.SaveExcel(_fileName, (_fileName.ToUpper().EndsWith("XLSX") ? ExcelSaveFlags.UseOOXMLFormat : ExcelSaveFlags.NoFlagsSet));
                MessageBox.Show("Sheet exported as XLSX/XLS ");
            }
        }

Next, you will initialize the PrintInfo class prior to initializing your Spread component, as well as creating a new PrintInfo class within the form which is what allows you to be able to export to PDF. You can achieve this functionality by implementing the following code:

public partial class Form1 : Form
    {
        private PrintInfo _printInfo;

        public Form1()
        {
            _printInfo = new PrintInfo();
            InitializeComponent();
            Name = "Introduction";
            SetupSpreadSheet();
        }

After assigning functionality of each button, click Open/Export buttons on your control just to ensure that they are working as expected.

This is a screenshot of the Import:

Import Excel spreadsheets

XLSX/XLS Export:

XLSX/XLS Export

PDF Export:

PDF export

Next, set the formatting of the window:

            this.spread.AccessibleDescription = "Book1";
            this.spread.BackColor = System.Drawing.SystemColors.Control;
            this.spread.Location = new System.Drawing.Point(12, 12);
            this.spread.Name = "spread";
            this.spread.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.spread.Size = new System.Drawing.Size(1044, 403);
            this.spread.TabIndex = 0;

Lastly, since we're going to be working with a standard invoice template that Excel provides, you will also want to add in a Form_Load that will automatically import the template from where you have saved this template on your machine:

private void Form1_Load(object sender, EventArgs e)
        {
            spread.OpenExcel("C: \\Users\\christopher.good\\Desktop\\SamplesB\\AcmeProduce.xlsx");
        }

Now, when you build and run the application, you should see the following report:

Feel free to then to customize this invoice, and then export to PDF, .xlsx, or .xls!

This is just one of the many ways that you can use Spread.NET for Winforms to increase your projects features and functionality. You can also download the full project here.

In another article series, we demonstrate how to import and export Excel spreadsheets with C# and WPF, import/export Excel spreadsheets using JavaScript and how to import and export Excel spreadsheets in Angular.

Thanks for following along, happy coding!

Import and export Excel spreadsheets in your app

Download your free 30-day trial version of Spread.NET 12 Windows Forms

Download Now!