Link to home
Start Free TrialLog in
Avatar of Term56
Term56

asked on

Exporting to Excell Help(ASP.net/VB.net)

I have the following sub to export to excell. The problem is this :

The below code used to work when I had bound columns, but now I have template columns like this :

<asp:TemplateColumn HeaderText="Job #">
       <HeaderStyle Width="16%"></HeaderStyle>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.job_id") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Wrap=True id="Textbox1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.job_id") %>' Width="100%">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>


obviously this doesnt work anymore :

        For Each item In DataGrid1.Items
            colRow = item.Cells(0).Text & Chr(9) & item.Cells(1).Text & Chr(9) & item.Cells(2).Text & Chr(9) & item.Cells(3).Text & Chr(9) & item.Cells(4).Text & Chr(9) & item.Cells(5).Text & Chr(9) & item.Cells(6).Text & Chr(9) & item.Cells(7).Text & Chr(9) & item.Cells(8).Text & Chr(9) & item.Cells(9).Text & Chr(9) & item.Cells(10).Text & Chr(9) & item.Cells(11).Text & Chr(9) & item.Cells(12).Text & Chr(9) & item.Cells(13).Text & Chr(9) & item.Cells(14).Text & Chr(13) & Chr(10)
            resp.Write(colRow)
        Next item

so I tried :

For Each item In DataGrid1.Items
            colRow = CType(item.Cells(0).FindControl("Textbox1"), TextBox).Text & Chr(9) & CType(item.Cells(1).FindControl("Textbox2"), TextBox).Text & Chr(9) & CType(item.Cells(2).FindControl("Textbox3"), TextBox).Text & Chr(9) & CType(item.Cells(3).FindControl("Textbox4"), TextBox).Text & Chr(9) & CType(item.Cells(4).FindControl("Textbox5"), TextBox).Text & Chr(9) & CType(item.Cells(5).FindControl("Textbox6"), TextBox).Text & Chr(9) & CType(item.Cells(6).FindControl("Textbox7"), TextBox).Text & Chr(9) & CType(item.Cells(7).FindControl("Textbox8"), TextBox).Text & Chr(9) & CType(item.Cells(8).FindControl("Textbox9"), TextBox).Text & Chr(9) & CType(item.Cells(9).FindControl("Textbox10"), TextBox).Text & Chr(9) & CType(item.Cells(10).FindControl("Textbox11"), TextBox).Text & Chr(9) & CType(item.Cells(11).FindControl("Textbox12"), TextBox).Text & Chr(9) & CType(item.Cells(12).FindControl("Textbox13"), TextBox).Text & Chr(9) & CType(item.Cells(13).FindControl("Textbox14"), TextBox).Text & Chr(9) & CType(item.Cells(14).FindControl("Textbox15"), TextBox).Text & Chr(13) & Chr(10)
            resp.Write(colRow)
        Next item

but getting an error :

Object reference not set to an instance of an object.


Private Sub ExportBut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExportBut.Click

        Dim resp As HttpResponse

        resp = Page.Response

        resp.ContentType = "application/download"

        resp.AppendHeader("Content-Disposition", "attachment;filename=GLdata.xls")

        Dim colHeaders As String

        Dim myCol As DataGridColumn

        Dim i As Integer

        For i = 0 To 16

            resp.Write(Chr(13) & Chr(10))

        Next

        For Each myCol In DataGrid1.Columns

            colHeaders = colHeaders & myCol.HeaderText & Chr(9)
        Next

        If colHeaders.Length > 0 Then

            colHeaders = colHeaders.Substring(0, colHeaders.LastIndexOf(Chr(9)))

        End If

        colHeaders = colHeaders & Chr(13) & Chr(10)

        resp.Write(colHeaders)

        Dim colRow As String

        Dim item As DataGridItem

        For Each item In DataGrid1.Items
            colRow = item.Cells(0).Text & Chr(9) & item.Cells(1).Text & Chr(9) & item.Cells(2).Text & Chr(9) & item.Cells(3).Text & Chr(9) & item.Cells(4).Text & Chr(9) & item.Cells(5).Text & Chr(9) & item.Cells(6).Text & Chr(9) & item.Cells(7).Text & Chr(9) & item.Cells(8).Text & Chr(9) & item.Cells(9).Text & Chr(9) & item.Cells(10).Text & Chr(9) & item.Cells(11).Text & Chr(9) & item.Cells(12).Text & Chr(9) & item.Cells(13).Text & Chr(9) & item.Cells(14).Text & Chr(13) & Chr(10)
            resp.Write(colRow)
        Next item

        resp.End()

    End Sub
Avatar of Torrwin
Torrwin
Flag of United States of America image

I have two questions:

Does it work if you set colRow to a constant value like colRow = "Test"?  (That would prove the error was in the item loop)

If so, does it work with just one of the cells?  
     colRow = CType(item.Cells(0).FindControl("Textbox1"), TextBox).Text & Chr(9)
     resp.write(colRow)
there is no need to explicity write code to export to excel from datagrid .. just set the response.contenttype to excel .. it will open the datagrid contents in a excel sheet ..

Rejo

hi
http://www.dotnetjohn.com/articles.aspx?articleid=78

This is what you want to do plz find the link

Thanks

ASKER CERTIFIED SOLUTION
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial