Powered by SpreadsheetGear
<%@ Page Language="C#" EnableViewState="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DataTable to Excel Workbook with Chart using ASP.NET and C# - Returns XLS Workbook Using SpreadsheetGear</title>
<meta content="DataTable to Excel Workbook with Chart with ASP.NET, C# and SpreadsheetGear, a royalty free Microsoft Excel compatible spreadsheet component for the Microsoft .NET Framework featuring the fastest and most complete calculation engine available. Create, read, modify, calculate and write Microsoft Excel workbooks from your Microsoft .NET, ASP.NET, C#, VB.NET and Microsoft Office solutions. Integrates with Microsoft Visual Studio .NET, including IntelliSense and Dynamic Help." name="description" />
<script language="C#" runat="server">
    
    void Page_Load(Object sender, EventArgs e)
    {
        // Open a workbook template containing a chart
        String ssFile = Server.MapPath("files/chartsalesbyquarter.xls");
        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(ssFile);
        SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["2005 Sales"];
         
        // Create a DataSet from an XML file.  Modify this code to use
        // any DataSet such as one returned from a database query.
        String xmlfile = Server.MapPath("files/spicesalestotal.xml");
        System.Data.DataSet dataset = new System.Data.DataSet();
        dataset.ReadXml(xmlfile);
        System.Data.DataTable datatable = dataset.Tables["Products"];
        
        // Copy the DataTable to the worksheet starting at the top left cell
        SpreadsheetGear.IRange range = worksheet.Cells[0, 0, 2, 0];
        range.CopyFromDataTable(datatable, SpreadsheetGear.Data.SetDataFlags.InsertCells);
        
        // Auto size all worksheet columns which contain data
        worksheet.UsedRange.Columns.AutoFit();
        
        // Stream the Excel workbook to the client in the Excel 97-2003 (xls) 
        // file format compatible with Excel 97-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>
</html>