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).FindCo ntrol("Tex tbox1"), TextBox).Text & Chr(9) & CType(item.Cells(1).FindCo ntrol("Tex tbox2"), TextBox).Text & Chr(9) & CType(item.Cells(2).FindCo ntrol("Tex tbox3"), TextBox).Text & Chr(9) & CType(item.Cells(3).FindCo ntrol("Tex tbox4"), TextBox).Text & Chr(9) & CType(item.Cells(4).FindCo ntrol("Tex tbox5"), TextBox).Text & Chr(9) & CType(item.Cells(5).FindCo ntrol("Tex tbox6"), TextBox).Text & Chr(9) & CType(item.Cells(6).FindCo ntrol("Tex tbox7"), TextBox).Text & Chr(9) & CType(item.Cells(7).FindCo ntrol("Tex tbox8"), TextBox).Text & Chr(9) & CType(item.Cells(8).FindCo ntrol("Tex tbox9"), TextBox).Text & Chr(9) & CType(item.Cells(9).FindCo ntrol("Tex tbox10"), TextBox).Text & Chr(9) & CType(item.Cells(10).FindC ontrol("Te xtbox11"), TextBox).Text & Chr(9) & CType(item.Cells(11).FindC ontrol("Te xtbox12"), TextBox).Text & Chr(9) & CType(item.Cells(12).FindC ontrol("Te xtbox13"), TextBox).Text & Chr(9) & CType(item.Cells(13).FindC ontrol("Te xtbox14"), TextBox).Text & Chr(9) & CType(item.Cells(14).FindC ontrol("Te xtbox15"), 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 -Dispositi on", "attachment;filename=GLdat a.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
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,
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Wrap=True id="Textbox1" runat="server" Text='<%# DataBinder.Eval(Container,
</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).FindCo
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
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).FindCo
resp.write(colRow)