Powered by SpreadsheetGear
<%@ Page Language="VB" 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">
<title>DataTable to Excel Workbook using ASP.NET and VB.NET - SpreadsheetGear</title>
<meta content="DataTable to Excel Workbook using ASP.NET, VB.NET 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="VB" runat="server">

    Sub Page_Load(sender As Object, e As EventArgs)

        ' Create a DataSet from an XML file.  Modify this code to use
        ' any DataSet such as one returned from a database query.
        Dim xmlfile As String = Server.MapPath("files/spiceorder.xml")
        Dim dataset As New System.Data.DataSet()
        Dim datatable As System.Data.DataTable = dataset.Tables("OrderItems")

        ' Create a new workbook and worksheet.
        Dim workbook As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook()
        Dim worksheet As SpreadsheetGear.IWorksheet = workbook.Worksheets("Sheet1")
        worksheet.Name = "Spice Order"

        ' Get the top left cell for the DataTable.
        Dim range As SpreadsheetGear.IRange = worksheet.Cells("A1")

        ' Copy the DataTable to the worksheet range.
        range.CopyFromDataTable(datatable, SpreadsheetGear.Data.SetDataFlags.None)

        ' Auto size all worksheet columns which contain data

        ' 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.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment; filename=report.xls")
        workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8)

    End Sub 'Page_Load