<p>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(). </p>

<p>Formatting is a big issue though. Any tips/ideas on how to make this look "readable" in a text format ? </p>

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

[{'answer_id': 53656, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 2, 'selected': True, 'text': '<p>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.</p>\n'}, {'answer_id': 53659, 'author': 'Mark Glorie', 'author_id': 952, 'author_profile': 'https://Stackoverflow.com/users/952', 'pm_score': 1, 'selected': False, 'text': '<p>This will sound like a really horrible solution, but it just might work:  </p>\n\n<p>Render the DataTable contents into a DataGrid/GridView (assuming ASP.NET) and then screen scrape that.  </p>\n\n<p>I told you it would be messy.</p>\n'}, {'answer_id': 53662, 'author': 'Bermo', 'author_id': 5110, 'author_profile': 'https://Stackoverflow.com/users/5110', 'pm_score': 0, 'selected': False, 'text': '<p>Loop through the datatable and send it as HTML email - <a href="http://forums.asp.net/t/1046932.aspx" rel="nofollow noreferrer">generating html table from datatable &amp; sending it as body of email</a>.</p>\n'}, {'answer_id': 53663, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': False, 'text': "<p>Get the max size for each column first. That way a varchar(255) column containing postal codes won't take up too much space.</p>\n\n<p>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.</p>\n\n<p>You can also give the user the option to create comma delimited text so the receiver can import the table into a spreadsheet.</p>\n"}, {'answer_id': 53665, 'author': 'Lukas Šalkauskas', 'author_id': 5369, 'author_profile': 'https://Stackoverflow.com/users/5369', 'pm_score': -1, 'selected': False, 'text': '<p>You can do smth like this (if VB):</p>\n\n<pre><code>Dim Str As String = ""\n    \'Create File if doesn\'t exist\n        Dim FILE_NAME As String = "C:\\temp\\Custom.txt"\n        If System.IO.File.Exists(FILE_NAME) = False Then\n            System.IO.File.Create(FILE_NAME)\n        End If\n\n        Dim objWriter As System.IO.StreamWriter\n        Try\n            objWriter = New System.IO.StreamWriter(FILE_NAME)\n        Catch ex As System.IO.IOException\n            MsgBox("Please close the file: (C:\\temp\\Custom.txt) before proceeding" &amp; vbCrLf &amp; ex.Message.ToString, MsgBoxStyle.Exclamation)\n            objWriter = Nothing\n            Err = True\n        End Try\n\n\n\'I assume you know how to write to text file.\n\'Say my datagridview is named "dgrid"\n\nDim x,y as integer\n\nFor x = 0 to dgrid.rows.count -1\n    For y = 0 to dgrid.columns.count - 1\n       Str = dgrid.Rows(x).Cells(y).Values &amp; " "\n   Next y\nNext x\n\nobjWriter.Close()\n</code></pre>\n\n<p><a href="http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23041873.html" rel="nofollow noreferrer">Resource</a>.</p>\n\n<p>Or you can even generate an <a href="http://en.wikipedia.org/wiki/Comma-separated_values" rel="nofollow noreferrer">CSV</a> file from your DataTable.</p>\n'}, {'answer_id': 53725, 'author': 'Radu094', 'author_id': 3263, 'author_profile': 'https://Stackoverflow.com/users/3263', 'pm_score': 0, 'selected': False, 'text': "<p>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!) .</p>\n\n<p>Anyway, if anyone is interested in this, let me know and I'll provide the code.</p>\n"}, {'answer_id': 54105, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/53652', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3263/']