troubleshooting Question

Better way than this to export Datatable to Excel/Email?

Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland asked on
.NET ProgrammingVisual Basic.NET
7 Comments1 Solution361 ViewsLast Modified:
In the code below, I retrieve a DataTable from SQL, then enumerate into a text file, attach it to an email and send it:

Dim dt as DataTable = GetData()
Dim sb As New StringBuilder
sb.Append("<html>")
sb.Append("<body>")
sb.Append("<table>")
sb.Append("<tr>")
For Each dc As DataColumn In dt.Columns
	sb.Append("<td>" & dc.ColumnName & "</td>")
Next
sb.AppendLine("</tr>")
For Each dr As DataRow In dt.Rows
	sb.Append("<tr>")
	For Each dc As DataColumn In dt.Columns
		sb.Append("<td>" & dr.Item(dc.ColumnName).ToString & "</td>")
	Next
	sb.Append("</tr>")
Next
sb.Append("</table>")
sb.Append("</body>")
sb.Append("</html>")
Dim mm As New MailMessage
mm.To.Add(New MailAddress("Me@MyDomain.com", "Me"))
mm.From = New MailAddress("some@body.com")
mm.IsBodyHtml = False
mm.Body = "Your data is attached"
If File.Exists(Environment.CurrentDirectory & "\" & Me._SpreadSheetFile) Then
	File.Delete(Environment.CurrentDirectory & "\" & Me._SpreadSheetFile)
End If
Dim fs As FileStream = File.Create((Environment.CurrentDirectory & "\" & Me._SpreadSheetFile))
fs.Close()
fs.Dispose()
Dim writer As New StreamWriter((Environment.CurrentDirectory & "\" & Me._SpreadSheetFile), True)
writer.WriteLine(sb.ToString)
writer.Close()
writer.Dispose()
Try
	Dim att As New Attachment(Environment.CurrentDirectory & "\" & Me._SpreadSheetFile)
	att.ContentType = New Net.Mime.ContentType("application/vnd.ms-excel")
	mm.Attachments.Add(att)
Catch ex As ApplicationException
	Throw ex
End Try
Dim sc As New SmtpClient(System.Environment.MachineName, 25)
sc.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
sc.UseDefaultCredentials = False
sc.Credentials = Me._emailCredentials
Try
	sc.Send(mm)
Catch ex As Exception
	Throw ex
Finally
	sb = Nothing
	mm = Nothing
	sc = Nothing
End Try
dt.Dispose()

I am a VB.NET newbie, so have the following questions:

1) The attachment should be a Microsoft Excel spreadsheet.  Because Excel opens basic HTML, I've simply create a text file with HTML inside, and saved as XLS.  Is there a more efficient/better recommended way to export to Excel?  I've read dozens of tutorials on this today already so please don't post any more...!

2) Is it possible to attach the file directly from memory, to save creating the file in the operating system, then attaching it, then deleting next time it is executed?

3) At the risk of scrapping all my code, is there a more efficient/faster way to complete my logic?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros