Spread.Sheets Documentation > Developer's Guide > Managing Data > Using the Server-side Excel Import and Export Component > 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:
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> |
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(); } } |