Convert csv to Html format

When design selenium object management tool, need to generate html report from csv file, implement as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private static string ConvertCsvToHtml(string csvText)
{
//split the CSV, assume no commas or line breaks in text
List<List<string>> splitString = new List<List<string>>();
List<string> lineSplit = csvText.Split('\n').ToList();
foreach (string line in lineSplit)
{
splitString.Add(line.Split(',').ToList());
}

//encode text safely, and create table
string htmlReport = "<html>";
htmlReport += "<h1 style='text-align:center'> Test Report</h1>";
htmlReport += "<body>";
htmlReport += "<table border = '1' cellspacing='0' cellpadding='2' width='100%'>";
foreach(List<string> splitLine in splitString)
{
htmlReport += "<tr>";
foreach(string splitText in splitLine)
{
htmlReport += "<td>" + WebUtility.HtmlEncode(splitText) + "</td>";
}
htmlReport += "</tr>";
}
htmlReport += "</table>";
htmlReport += "</body>";
htmlReport += "</html>";
return htmlReport;
}

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!