Spread.Sheets Documentation
Using the Excel Import and Export Component in a Windows Forms Application

This example shows how to use the Excel Import and Export component in a Windows Forms application.

Use the following steps to create the sample:

  1. Install the component if you have not already done so. For more information, see Installing and Configuring the Excel Import and Export Component.
  2. Create a Windows Forms application in Visual Studio.
  3. Add the ExcelIO assembly references from the setup folder: "ExcelService".

  4. Add two Button controls, a RichTextBox control, an OpenFileDialog component, and a SaveFileDialog component. Set the name and text for each control.
  5. Add a license for the control before using the importer or exporter. For more information, see Adding a License for the Excel Import and Export Component.
  6. Add import and export logic in the two button click events. For example:
    C#
    Copy Code
    using GrapeCity.Spread.Sheets.ExcelIO;
    using GrapeCity.Windows.SpreadSheet.Data;
    using System;
    using System.IO;
    using System.Windows.Forms;
    
    
    // Add your license key
    // GrapeCity.Spread.Sheets.License.LicenseKey = "Your Key";
    
    private void importBtn_Click(object sender, EventArgs e)
    {
        DialogResult result = this.openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            using (Stream stream = File.Open(this.openFileDialog1.FileName, FileMode.Open))
            {
                Importer importer = new Importer();
                this.resultRtb.Text = importer.ImportExcel(stream);
            }
        }
    }
    private void exportBtn_Click(object sender, EventArgs e)
    {
        DialogResult result = this.saveFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            using (FileStream fs = File.Create(this.saveFileDialog1.FileName))
            {
                Exporter exporter = new Exporter(this.resultRtb.Text);
                exporter.SaveExcel(fs, ExcelFileFormat.XLSX, ExcelSaveFlags.NoFlagsSet);                }
        }
    }
    
  7. Build and Press F5 to run. Select the Import Excel button to import an Excel-formatted file.
  8. Select the Export Excel button to export the JSON string to an Excel file.

 

 


Copyright © GrapeCity, inc. All rights reserved.

Send comments on this topic.