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

This example shows how to use the Excel Import and Export component in an ASP.NET web form application.

Use the following steps to create the example:

  1. Install the Excel Import and Export component if you have not already done so. For more information, see Installing and Configuring the Excel Import and Export Component.
  2. Create an ASP.NET Empty Web Application in Visual Studio.

  3. Add the ExcelIO assembly references from the setup folder: "ExcelService".

  4. Add a WebForm aspx item to the project.
  5. Add a license for the control before the import or exporter and add the license key after the Spread.Sheets script reference. For more information, see Adding a License for the Excel Import and Export Component.
  6. Add two Button controls to the project, a FileUpload control, two HiddenField components, and a div dom element to the form. Add code to the page. For example:
    JavaScript
    Copy Code
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestExcelIO_WebForm.WebForm1" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <!--Spread.Sheets Widgets JavaScript-->
        <link href="css/gc.spread.sheets.excel2013white.10.x.x.css" rel="stylesheet" type="text/css" />
        <script src="scripts/gc.spread.sheets.all.10.x.x.min.js" type="text/javascript"></script>
    <script>GC.Spread.Sheets.LicenseKey = "your key";</script>
        <script>
            function onLoad() {
                var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
                var hiddenFile = document.getElementById("HiddenField1");
                if (hiddenFile && hiddenFile.value !== "") {
                    spread.fromJSON(JSON.parse(hiddenFile.value));
                }
            }
            function exportExcel() {
                var spread = GC.Spread.Sheets.findControl(document.getElementById("ss"));
                var hiddenFile2 = document.getElementById("HiddenField2");
                hiddenFile2.value = JSON.stringify(spread.toJSON());
            }
        </script>
    </head>
    <body onload="onLoad()">
        <form id="form1" runat="server">
            <asp:FileUpload ID="FileUpload1" runat="server" Height="24px" Width="223px" />
            <asp:Button ID="ImportBtn" runat="server" Text="Import Excel" OnClick="ImportBtn_Click" />
            <asp:Button ID="ExportBtn" runat="server" Text="Export Excel" OnClick="ExportBtn_Click" OnClientClick="exportExcel()"/>
            <div id="ss" style="width:100%;height:500px">
                
            </div>
            <asp:HiddenField ID="HiddenField1" runat="server" />
            <asp:HiddenField ID="HiddenField2" runat="server" />
        </form>
    </body>
    </html>
    
  7. Add the server side code in the server button click event. For example:
    C#
    Copy Code
    // Add your license key
    // GrapeCity.Spread.Sheets.License.LicenseKey = "Your Key";
    
    protected void ImportBtn_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.HasFile)
        {
            GrapeCity.Spread.Sheets.ExcelIO.Importer excelImporter = new GrapeCity.Spread.Sheets.ExcelIO.Importer();
            this.HiddenField1.Value = excelImporter.ImportExcel(this.FileUpload1.FileContent);
        }
    }
    protected void ExportBtn_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.HiddenField2.Value))
        {
            GrapeCity.Spread.Sheets.ExcelIO.Exporter excelExporter = new GrapeCity.Spread.Sheets.ExcelIO.Exporter(this.HiddenField2.Value);
            excelExporter.SaveExcel(Response.OutputStream);
            Response.AddHeader("content-disposition", "attachment; filename= test.xlsx");
            Response.End();
        }
    }
    
  8. Build and Press F5 to run. Select Choose File to select an Excel file, then select the Import Excel button.
  9. Select the Export Excel button to export the current Spread.Sheets to an Excel file.

 

 


Copyright © GrapeCity, inc. All rights reserved.

Send comments on this topic.