|
-
March 1st, 2010, 08:20 AM
#4
Re: Multiline in a Excel Cell, When exporting data from datatable to Excel
***EDIT**** Moved from vb Net Forum
I am not able to see the two colums you're concatenating
in any case:
you're making it with tabs... try with a table, it might suite you a bit better.
Ie: in an App_code Class
Code:
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Text
Public Class DtToExcel
Public Shared Function GenerateReport(ByVal dt As DataTable, ByVal Sv As System.Web.HttpServerUtility) As String
Dim sb As New StringBuilder()
sb.AppendLine("<table><tr>")
For Each dc As DataColumn In dt.Columns
sb.AppendLine("<td>" + Sv.HtmlEncode(dc.ColumnName) + "</td>")
Next
sb.AppendLine("</tr>")
For Each dr As DataRow In dt.Rows
sb.AppendLine("<tr>")
For Each dc As DataColumn In dt.Columns
If dr(dc) IsNot Nothing AndAlso dr(dc) IsNot DBNull.Value Then
sb.AppendLine("<td>" + Sv.HtmlEncode(dr(dc).ToString()) & "</td>")
Else
sb.AppendLine("<td></td>")
End If
Next
sb.AppendLine("</tr>")
Next
sb.AppendLine("</table>")
Return sb.ToString()
End Function
End Class
in a web form
Code:
Private Sub showReport()
'
Response.AddHeader("Content-Disposition", "attachment;filename=Report_" & Now.Ticks & ".xls")
Response.Buffer = True
Response.Charset = String.Empty
Response.ContentType = "application/vnd.ms-excel"
'a method that give me back the datatable I need here
'you should get your own dt...
Dim dt As System.Data.DataTable = Manager.GetDt("Customers")
Response.Write(DtToExcel.GenerateReport(dt, Server))
Response.Flush()
Response.End()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
showReport()
End Sub
Last edited by Cimperiali; March 1st, 2010 at 08:29 AM.
Reason: inform this is a moved thread from Vb Net forum
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|