Skip to main content Skip to footer

SpreadJS Excel Import and Export

You can use the Excel Import and Export Component to import and export Excel files into the SpreadJS widget. The component resides on your application server. This example shows how to use the Excel Import and Export component in a Visual Studio 2015 Windows Forms application. Use the following steps to create the sample:

  1. Install the product (run SpreadJS ExcelIO Server Component.exe or check the Excel IO Server Component in the web installer). The program files are installed to “C:\Program Files (x86)\GrapeCity\Spread Studio 9\SpreadJS\ExcelIO”. SpreadJSInstalled Installed Files
  2. Create a Windows Forms application in Visual Studio.
  3. Add the ExcelIO assembly references from the setup folder. SpreadJSRef References
  4. Add two Button controls, a RichTextBox control, an OpenFileDialog component, and a SaveFileDialog component. Set the name and text for each control.
  5. Use the following steps to create a run time license file, add the license information to the file, and then add the file as an Embedded Resource in the Properties folder (or the project folder in a VB project).
  6. Open notepad.exe and add "GrapeCity.Spread.Sheets.ExcelIO.Spread, GrapeCity.Spread.Sheets.ExcelIO, Version=9.40.20153.0, Culture=neutral, PublicKeyToken=3ef656c0a61aba3a" to the file.
  7. Use Save As to save the file.
  8. Rename the file to "licenses.licx".
  9. Copy the file to the main project folder for a VB project and add the file as a resource. Set the Build Action to Embedded Resource to include the file as an embedded resource. SpreadJSAddResource Add Resource File to Project
  10. Copy the file to the Properties folder for a C# project and include it as an Embedded Resource in the Properties folder. SpreadJSEmbeddedResource Change Build Action
  11. Add import and export logic in the two button click events. For example: C#

    using GrapeCity.Spread.Sheets.ExcelIO;  
    using GrapeCity.Windows.SpreadSheet.Data;  
    using System;  
    using System.IO;  
    using System.Windows.Forms;  
    
    private void Import_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.richTextBox1.Text = importer.ImportExcel(stream);  
           }  
       }  
    }  
    private void Export_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);                }  
       }  
    }  
    
    

    VB

    Imports GrapeCity.Spread.Sheets.ExcelIO  
    Imports GrapeCity.Windows.SpreadSheet.Data  
    Imports System  
    Imports System.IO  
    Imports System.Windows.Forms  
    
    Public Class Form1  
       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
       End Sub  
    
       Private Sub Import_Click(sender As Object, e As EventArgs) Handles Import.Click  
           Dim result As DialogResult = Me.OpenFileDialog1.ShowDialog()  
           If result = DialogResult.OK Then  
               Using stream As Stream = File.Open(Me.OpenFileDialog1.FileName, FileMode.Open)  
                   Dim importer As New Importer()  
                   Me.RichTextBox1.Text = importer.ImportExcel(stream)  
               End Using  
           End If  
       End Sub  
    
       Private Sub Export_Click(sender As Object, e As EventArgs) Handles Export.Click  
           Dim result As DialogResult = Me.SaveFileDialog1.ShowDialog()  
           If result = DialogResult.OK Then  
               Using fs As FileStream = File.Create(Me.SaveFileDialog1.FileName)  
                   Dim exporter As New Exporter(Me.RichTextBox1.Text)  
                   exporter.SaveExcel(fs, ExcelFileFormat.XLSX, ExcelSaveFlags.NoFlagsSet)  
               End Using  
           End If  
       End Sub  
    End Class  
    
    
  12. Build and Press F5 to run. Select the Import Excel button to import an Excel-formatted file.

  13. Select the Export Excel button to export the JSON string to an Excel file.

MESCIUS inc.

comments powered by Disqus