I'm adding a function to my own personal toolkit lib to do simple CSV to HTML table conversion.
I would like the smallest possible piece of code to do this in C#, and it needs to be able to handle CSV files in excess of ~500mb.
So far my two contenders are
splitting csv into arrays by delimiters and building HTML output
search-replace delimiters with table th tr td tags
Assume that the file/read/disk operations are already handled... i.e., i'm passing a string containing the contents of said CSV into this function. The output will consist of straight up simple HTML style-free markup, and yes the data may have stray commas and breaks therein.
update: some folks asked. 100% of the CSV i deal with comes straight out of excel if that helps.
a1,b1,c1\r\n a2,b2,c2\r\n
Read All Lines into Memory
var lines =File.ReadAllLines(args[0]);
using (var outfs = File.AppendText(args[1]))
{
outfs.Write("<html><body><table>");
foreach (var line in lines)
outfs.Write("<tr><td>" + string.Join("</td><td>", line.Split(',')) + "</td></tr>");
outfs.Write("</table></body></html>");
}
or Read one line at a time
using (var inFs = File.OpenText(args[0]))
using (var outfs = File.AppendText(args[1]))
{
outfs.Write("<html><body><table>");
while (!inFs.EndOfStream )
outfs.Write("<tr><td>" + string.Join("</td><td>", inFs.ReadLine().Split(',')) + "</td></tr>");
outfs.Write("</table></body></html>");
}
... @Jimmy ... I created an extended version using LINQ. Here is the highlight ... (lazy eval for line reading)
using (var lp = args[0].Load())
lp.Select(l => "<tr><td>" + string.Join("</td><td>", l.Split(',')) + "</td></tr>")
.Write("<html><body><table>", "</table></body></html>", args[1]);