.net - Error when converting a csv file to html using c# -
i trying convert csv file html using stream reader , writing stream writer. problem is missing last line of csv file. debugged , tried tracing stream reader unable see stream reader output in watch @ each loop.
string line, path = @"c:\vertex\nov\source\reports\rpt_disconn_and_reconn_list_for_customer_operations_results.csv"; system.io.streamreader objectstreamreader = null; system.io.streamwriter objectstreamwriter = null; objectstreamreader = new system.io.streamreader(path); objectstreamwriter = new system.io.streamwriter("result.html"); line = objectstreamreader.readline(); objectstreamwriter.write("<html><head><table border =1><>"); objectstream.writer.write(""): while (objectstreamreader.peek() > -1) { line = objectstreamreader.readline(); objectstreamwriter.writeline("<tr><td>" + string.join("</td><td>", line.split('\t')) + "</td></tr>"); } objectstreamwriter.writeline("</table></body></html>");
i suggest using file.readalllines in this:
const string path = @"c:\temp\data.csv"; const char token = '\t'; string[] lines = file.readalllines(path); stringbuilder result = new stringbuilder(); result.append("<html><head><table border =1>"); foreach (string line in lines) { string[] parts = line.split(token); string row = "<tr><td>" + string.join("</td><td>", parts) + "</td></tr>"; result.append(row); } result.append("</table></body></html>");
Comments
Post a Comment