944,008 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 30395
  • ASP.NET RSS
Sep 4th, 2006
0

Need help on export asp.net table to excel

Expand Post »
Hi im developing an asp.net report using vb language. I created the report previously using datagrid,which works well when i click the export button. It downloads the datagrid to excel format. But now i need to add anew header to the report,so i changed to using table which seems as a solution to the problem. I've managed to create the table and produce the report,but now i've to export th etable to excel format same as the one i did for my datagrid. I truied to just modify the code to my table name,but when it downloads, there is only a single empty cell in the excel file.
I paste my code so that anyone could point where is my mistake.
My aspx page

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. <form id="form1" runat="server">
  3. <table style="width: 600px">
  4. <tr>
  5. <td class="headertext" style="width: 798px" >
  6. SUMMARY REPORT SINGAPORE ( ALL)
  7. </td>
  8. </tr>
  9. <tr><td class="textfont" style="width: 798px; height: 17px;" >
  10. <asp:Label ID="lblMessage" runat="server" Text="Label" Width="264px"></asp:Label></td></tr>
  11. </table>
  12.  
  13. <br />
  14. <asp:Table ID="tblSummAllSg" runat="server" Font-Names="Verdana" BorderWidth="1"
  15. Font-Size="XX-Small">
  16. </asp:Table>
  17. <br />
  18. <br />
  19.  
  20.  
  21. <table align="center">
  22. <tr><td>
  23. <asp:Button ID="btnExport" runat="server" Text="Export As Excel" BorderColor="#C00000" />
  24. </td></tr>
  25. </table>
  26.  
  27. </form>


My code behing page (on page load method)

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. Dim tableHeading As TableRow = New TableRow()
  3.  
  4. 'Create and add the cells that contain the CallDate column heading text.
  5. Dim DateHeading As TableHeaderCell = New TableHeaderCell()
  6. DateHeading.Text = "Call In Date"
  7. DateHeading.HorizontalAlign = HorizontalAlign.Center
  8. tableHeading.Cells.Add(DateHeading)
  9.  
  10. 'Create and add the cells that contain the Calls column heading text.
  11. Dim TotalCallHeading As TableHeaderCell = New TableHeaderCell()
  12. TotalCallHeading.Text = "Total Calls"
  13. TotalCallHeading.ColumnSpan = 5
  14. TotalCallHeading.HorizontalAlign = HorizontalAlign.Center
  15. tableHeading.Cells.Add(TotalCallHeading)
  16. Dim tableHeading2 As TableRow = New TableRow()
  17.  
  18. Dim CallInDateHeading As TableCell = New TableCell()
  19. CallInDateHeading.Text = "Call In Date"
  20. CallInDateHeading.HorizontalAlign = HorizontalAlign.Center
  21. tableHeading2.Cells.Add(CallInDateHeading)
  22. Dim InBoundOutBoundHeading As TableCell = New TableCell()
  23. InBoundOutBoundHeading.Text = "InBound/OutBound"
  24. tableHeading2.Cells.Add(InBoundOutBoundHeading)
  25. tblSummAllSg.Rows.Add(tableHeading)
  26. tblSummAllSg.Rows.Add(tableHeading2)
  27. While (objreader.Read())
  28.  
  29. Dim detailsRow As TableRow = New TableRow()
  30.  
  31. Dim CallInDateCell As TableCell = New TableCell()
  32. CallInDateCell.Text = objreader("CallInDate").ToString()
  33. detailsRow.Cells.Add(CallInDateCell)
  34. Dim InBoundOutBoundCell As TableCell = New TableCell()
  35. InBoundOutBoundCell.Text = objreader("InBoundOutBound").ToString()
  36. detailsRow.Cells.Add(InBoundOutBoundCell)
  37. Dim IncompletePrankCell As TableCell = New TableCell()
  38. IncompletePrankCell.Text = objreader("IncompletePrank").ToString()
  39. detailsRow.Cells.Add(IncompletePrankCell)
  40. tblSummAllSg.Rows.Add(detailsRow)
  41. End While


My code behing page (on export button click)

ASP.NET Syntax (Toggle Plain Text)
  1. Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
  2. ' binding datagrid result to export list
  3. Response.Clear()
  4. Response.AddHeader("content-disposition", "attachment;filename=DetailReport.xls")
  5. Response.Charset = ""
  6. Response.Cache.SetCacheability(HttpCacheability.NoCache)
  7. Response.ContentType = "application/vnd.xls"
  8. Dim stringWrite As IO.StringWriter = New System.IO.StringWriter()
  9. Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
  10. tblSummAllSg.RenderControl(htmlWrite)
  11. Response.Write(stringWrite.ToString())
  12. Response.End()
  13. End Sub
  14.  




I really hope someone can help me solve this. Im not sure now if doing this report as table is a solution to the datagrid problem. Im running out of idea and time. Any help is really appreciated. Thank you in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newMeg is offline Offline
21 posts
since Jul 2006
Oct 6th, 2006
0

Re: Need help on export asp.net table to excel

ciao ,

what you need to do is refill your tblsummallsg ....and then call
tblSummAllSg.RenderControl(htmlWrite)

because on click of the button al the data is lost ...

hope it helps...

Gauri
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gauri_milind is offline Offline
1 posts
since Oct 2006
Jul 11th, 2008
0

Re: Need help on export asp.net table to excel

ciao ,

what you need to do is refill your tblsummallsg ....and then call
tblSummAllSg.RenderControl(htmlWrite)

because on click of the button al the data is lost ...

hope it helps...

Gauri

Hi Ciao,

Even i am also facing same problem. Have you solved the problem? Tones of advance thanks if you post your code.

Have a nice weekend

Giri
Reputation Points: 10
Solved Threads: 0
Newbie Poster
giri.hanchate is offline Offline
1 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: tabindex
Next Thread in ASP.NET Forum Timeline: image uploading in c# +asp.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC