problem exporting datagrid to excel

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default problem exporting datagrid to excel

    I am using the following code to export a datagrid to excel from an asp
    page:

    private void exportButton_Click(object sender, System.EventArgs e)
    {
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";
    this.EnableViewState = false;
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag. Html);
    this.ClearControls(reportGrid);
    reportGrid.RenderControl(hw);
    hw.RenderEndTag();
    Response.Write(tw);
    Response.End();
    }

    When I run this code I only get the first column returned to the
    spreadsheet.

    The is no other control on the datagrid so I am at a loss. Any suggestions
    would be appreciated.

    Thanks,

    Dave

    --
    Message posted via [url]http://www.dotnetmonster.com[/url]
    Dave Bailey via DotNetMonster.com Guest

  2. Similar Questions and Discussions

    1. Problem transfering datagrid to Excel
      Hi, I have read a lot of articles in this newsgroup about how to solve this problem but found no solution. I'm trying to export a C# datagrid to...
    2. Handling special characters when exporting DataGrid to Excel
      I am trying to export a DataGrid to Excel using a HtmlTextWriter and the DataGrid.RenderControl method. Everything is working fine except that...
    3. exporting a datagrid to excel
      Can anyone tell me how to export data contained in a datagrid to excel using MC++? -------------------------------- From: Jonathan McMorris ...
    4. Datagrid to Excel Export Problem while adding summation values
      We are getting a strange problem in exporting data grid to excel. We have a data grid in hand with basic data bounded to it and adding some...
    5. exporting webpage to excel formatting problem
      Hi, I have a webpage that is taking input from a form and using it as criteria to select data out of an sql database. The page displays an html...
  3. #2

    Default Re: problem exporting datagrid to excel

    I'm not too sure how to solve your problem.
    I store the dataset, filters, sorts and pageindex of the datagrid in
    httpcache and session vars and then create a new datagrid using those
    stored objects. I then use that for my Excel output.
    It's in vb (easily translatable to c#, but I'm not gonna do it for you,
    sorry)

    Private Sub ExcelOut()

    Page.Response.Clear()
    Page.Response.AppendHeader("Content-Disposition",
    "attachment; filename=")
    Page.Response.ContentType = "application/vnd.ms-excel"
    'Response.ContentType = "text/csv"

    ' Remove the charset from the Content-Type header.
    Page.Response.Charset = ""

    Dim dsAPL As DataSet =
    MyCustomClass.DataAccess.SQLDataSet(_strDataContex t, "dbo." &
    _strDataContext & "_GET", True), _
    ds As DataSet = New DataSet
    ds = dsAPL.Clone
    ds.Merge(dsAPL)

    If Not Page.Session(_strDataContext & "_Filter") Is Nothing
    Then
    ds.Tables(0).DefaultView.RowFilter =
    Page.Session(_strDataContext & "_Filter")
    End If

    If Not Page.Session(_strDataContext & "_Sort") Is Nothing
    Then
    ds.Tables(0).DefaultView.Sort =
    Page.Session(_strDataContext & "_Sort")
    End If

    Dim oItem As ListItem
    For Each oItem In lstDisplayFields.Items
    If Not oItem.Selected Then
    ds.Tables(0).Columns.Remove(oItem.Value)
    End If
    Next

    Dim tw As New System.IO.StringWriter

    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    Dim dgGrid As New DataGrid

    dgGrid.DataSource = ds.Tables(0).DefaultView

    dgGrid.DataBind()

    ' Get the HTML for the control.
    dgGrid.RenderControl(hw)

    ' Write the HTML back to the browser.
    Page.Response.Write(tw.ToString())

    ' End the response.
    Page.Response.End()

    ds.Dispose()
    ds = Nothing

    End Sub

    ajamrozek Guest

  4. #3

    Default Re: problem exporting datagrid to excel

    Quote Originally Posted by Dave Bailey via DotNetMonster.com View Post
    I am using the following code to export a datagrid to excel from an asp
    page:

    private void exportButton_Click(object sender, System.EventArgs e)
    {
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";
    this.EnableViewState = false;
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag. Html);
    this.ClearControls(reportGrid);
    reportGrid.RenderControl(hw);
    hw.RenderEndTag();
    Response.Write(tw);
    Response.End();
    }

    When I run this code I only get the first column returned to the
    spreadsheet.

    The is no other control on the datagrid so I am at a loss. Any suggestions
    would be appreciated.

    Thanks,

    Dave

    --
    Message posted via [url]http://www.dotnetmonster.com[/url]
    http://aspnet-ajax-aspnetmvc.blogspot.com/2011/06/export-datatable-to-excel-with-special.html
    Mhan Prajapati Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139