views:

687

answers:

8

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.)

+1  A: 

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.

Espo
+1  A: 

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.

Mark Glorie
No need to screen-scrape - just use the RenderControl function to an HtmlWriter.
samjudson
Exactly my thoughts on this one Sam!
Rob Cooper
Render control will give you perfect HTML this is that is the solution.
David Basarab
I used 'screen' scraping once because it was the easiest way to embed images in the email. I had some code that converted a whole HTML URL into the various MIME parts for the HTML email. So, if you want images too then scraping might work out easier.
Drew Noakes
A: 

Loop through the datatable and send it as HTML email - generating html table from datatable & sending it as body of email.

Bermo
+1  A: 

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.

Mendelt
A: 

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()

Resource.

Or you can even generate an CSV file from your DataTable.

Lukas Šalkauskas
A: 

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.

Radu094
A: 

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.

Joel Coehoorn
A: 

Radu094, I would like to see the code.

Brianedow