Reputation: 28434
This might be a bit on the silly side of things but I need to send the contents of a DataTable (unknown columns, unknown contents) via a text e-mail. Basic idea is to loop over rows and columns and output all cell contents into a StringBuilder using .ToString().
Formatting is a big issue though. Any tips/ideas on how to make this look "readable" in a text format ?
I'm thinking on "padding" each cell with empty spaces, but I also need to split some cells into multiple lines, and this makes the StringBuilder approach a bit messy ( because the second line of text from the first column comes after the first line of text in the last column,etc.)
Upvotes: 2
Views: 1703
Reputation: 416141
Does it need to be formatted nicely, or will an automated system pick up the mail message on the other end? If the latter, just use the datatable's .WriteXml() method.
Upvotes: 0
Reputation: 28434
I got this working by writing a custom formatter specifically for this task. The code is about 120 -130 lines long, so I don't know if I should post it here as an answer (maybe a feature to attach .cs files to a topic would be a good ideea!) .
Anyway, if anyone is interested in this, let me know and I'll provide the code.
Upvotes: 0
Reputation: 14361
You can do smth like this (if VB):
Dim Str As String = ""
'Create File if doesn't exist
Dim FILE_NAME As String = "C:\temp\Custom.txt"
If System.IO.File.Exists(FILE_NAME) = False Then
System.IO.File.Create(FILE_NAME)
End If
Dim objWriter As System.IO.StreamWriter
Try
objWriter = New System.IO.StreamWriter(FILE_NAME)
Catch ex As System.IO.IOException
MsgBox("Please close the file: (C:\temp\Custom.txt) before proceeding" & vbCrLf & ex.Message.ToString, MsgBoxStyle.Exclamation)
objWriter = Nothing
Err = True
End Try
'I assume you know how to write to text file.
'Say my datagridview is named "dgrid"
Dim x,y as integer
For x = 0 to dgrid.rows.count -1
For y = 0 to dgrid.columns.count - 1
Str = dgrid.Rows(x).Cells(y).Values & " "
Next y
Next x
objWriter.Close()
Or you can even generate an CSV file from your DataTable.
Upvotes: -2
Reputation: 37503
Get the max size for each column first. That way a varchar(255) column containing postal codes won't take up too much space.
Maybe you can split the complete table instead of splitting single lines. Put the complete right part of the table in a second stringbuilder and put it beneath the first table.
You can also give the user the option to create comma delimited text so the receiver can import the table into a spreadsheet.
Upvotes: 1
Reputation: 4931
Loop through the datatable and send it as HTML email - generating html table from datatable & sending it as body of email.
Upvotes: 0
Reputation: 3473
This will sound like a really horrible solution, but it just might work:
Render the DataTable contents into a DataGrid/GridView (assuming ASP.NET) and then screen scrape that.
I told you it would be messy.
Upvotes: 1
Reputation: 41939
Would converting the datatable to a HTML-table and sending HTML-mail be an alternative? That would make it much nicer on the receiving end if their client supports it.
Upvotes: 1