SpreadsheetGear 2017
ASP.NET
SpreadsheetGear 2017 > Tutorials > ASP.NET

Follow these steps to generate a simple Excel Report from ASP.NET using SpreadsheetGear 2017

Create a new ASP.NET Web Site

  1. Launch Visual Studio 2008, Visual Studio 2010, Visual Studio 2012, Visual Studio 2013, Visual Studio 2015 or Visual Studio 2017.
  2. On the File menu, point to New and click Web Site. The New Web Site dialog box appears.
  3. Under Templates, click ASP.NET Web Forms Site in Visual Studio 2012, Visual Studio 2013, Visual Studio 2015 and Visual Studio 2017, or click ASP.NET Web Site in earlier versions of Visual Studio.
  4. Change the Location as desired and set the Language to Visual C#.
  5. Click OK to create the web site.

Add a reference to SpreadsheetGear 2017

  1. In Solution Explorer, select the web site you just created.
  2. On the Website menu, click Add Reference. The Add Reference dialog box appears.
  3. In Visual Studio 2015 and Visual Studio 2017 click on the Extensions item under Assemblies in the Reference Manager. In older versions of Visual Studio click the .NET tab in the Add Reference dialog box. A list of .NET components appears.
  4. In the list of .NET components, select SpreadsheetGear 2017 Core Engine and SpreadsheetGear 2017 GDI+ Drawing Library for use with .NET 4.0+, or select SpreadsheetGear 2017 for .NET 2.0 for use with .NET 2.0+.
  5. Click OK to add the reference. 

Add code to create a workbook and return the workbook to Excel on the client

  1. In Solution Explorer, right click on Default.aspx and choose View Markup.
  2. Replace the Default.aspx markup with:
    Simple ASP.NET Excel Report using SpreadsheetGear 2017
    Copy Code
    <%
    @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE
    html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html
    xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>My First ASP.NET Excel Report</title>
    <script language="C#" runat="server">
    void Page_Load(Object sender, EventArgs e)
    {
      // Create a new workbook.
      SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
      SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
      SpreadsheetGear.IRange cells = worksheet.Cells;
      // Set the worksheet name.
      worksheet.Name = "2017 Sales";
      // Load column titles and center.
      cells["B1"].Formula = "North";
      cells["C1"].Formula = "South";
      cells["D1"].Formula = "East";
      cells["E1"].Formula = "West";
      cells["B1:E1"].HorizontalAlignment = SpreadsheetGear.HAlign.Center;
      // Load row titles using multiple cell text reference and iteration.
      int quarter = 1;
      foreach (SpreadsheetGear.IRange cell in cells["A2:A5"])
        cell.Formula = "Q" + quarter++;
      // Load random data and format as $ using a multiple cell range.
      SpreadsheetGear.IRange body = cells[1, 1, 4, 4];
      body.Formula = "=RAND() * 10000";
      body.NumberFormat = "$#,##0_);($#,##0)";
      // Stream the Excel workbook to the client in a format
      // compatible with Excel 2010, Excel 2013, Excel 2016, Excel 2019 and Excel for Office 365.
      Response.Clear();
      Response.ContentType = "application/vnd.ms-excel";
      Response.AddHeader("Content-Disposition", "attachment; filename=report.xls");
      workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8);
      Response.End();
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
    </body>
    </html>
    

Run the web page

  1. Right click on the markup of Default.aspx and choose View in Browser.
  2. Internet Explorer will be launched and a "File Download" dialog box will ask whether you want to open or save the file.
  3. You may have different results with older versions of Internet Explorer or Excel, or if Internet Explorer or Excel settings have been changed.