locked
How do I export DATATABLE to EXCEL. RRS feed

  • Question

  • User-507786106 posted

    I need to export a data table to an MS EXCEL File. Please help. I do have a grid view on the page but the gridview uses pages and Iwant the table export to have all the rows no page size set.

    NOTE: This is my export to excel - it works find when I am getting data from the grid vew. But I don't want to data to export from the GRIDVIEW I want it to export FROM eExcelData varible. 

    I am trying to export a short List and a long list of records.

    'Working Fine

    'Populate gridview (my work order(s))
        Private Sub Bindgrid()
            Dim getWorkOrder As New WorkOrder_Class()
           
            If getWorkOrder.errorFlag = True Then
                lblerrorMsg.Text = getWorkOrder.ErrorMessage
            Else
                GVWorkOrder.DataSource = getWorkOrder.WorkOrderData

                GVWorkOrder.DataBind()

                Dim eExcelData As DataTable
                eExcelData = getWorkOrder.WorkOrderData                  'Would like to export this table to EXCEL
            End If
        End Sub

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Private Sub ExportGridView()
            Response.Clear()
          
            '*************************************************************
            Dim attachment As String = "attachment; filename=" + "WSP_Engineer_Tool_.xls"
            Response.ClearContent()
            Response.AddHeader("content-disposition", attachment)
            Response.ContentType = "application/ms-excel"
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            '********************************************
            'Report Header
            sw.WriteLine("<b><u><font size='3'>Cook & Bait Report </font></u></b>")
            sw.WriteLine("<br />")
            '*******************************************
            GVWorkOrder.RenderControl(htw)                 'Works fine export from GridView (SHORT LIST)
            eExcelData.Rows.Add(htw)                     'Not working  MUST EXPORT TO EXCEL - ALL RECORDS (LONG LIST)
            Response.Write(sw.ToString())
            Response.[End]()
        End Sub



    Monday, March 15, 2010 1:36 PM

Answers

  • User-507786106 posted

    this is a web application but here is the correct answer. http://forums.asp.net/p/1412788/3714140.aspx

    Thanks all of you viewers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2010 12:14 PM

All replies

  • User197322208 posted

    shameless plug :

    http://exporter.codeplex.com/

    Monday, March 15, 2010 5:27 PM
  • User-507786106 posted

    this is a web application but here is the correct answer. http://forums.asp.net/p/1412788/3714140.aspx

    Thanks all of you viewers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2010 12:14 PM