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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
string strPageTitle = "HTML Report";
string filler = "none";
StringBuilder sb = new StringBuilder();
sb.Append("<html>");
sb.Append("<head>");
sb.Append("<title>");
sb.Append(strPageTitle);
sb.Append("</title>");
sb.Append("<style>");
sb.Append(".Header {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif;font-size: x-small;font-weight: bold;}");
sb.Append(".rows {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; }");
sb.Append("</style>");
sb.Append("</head>");
sb.Append("<body>");
sb.Append(@"<table width='90%' border='1' align='center' cellpadding='2' cellspacing='0'>");
if (dgv.Rows.Count > 0)
{
foreach (DataGridViewColumn col in dgv.Columns)
{
if (col.Index == dgv.Columns.Count - 1)
{
sb.Append("<td>");
sb.Append(string.Concat(col.HeaderText));
sb.Append("</td>");
}
else
{
sb.Append("<td>");
sb.Append(string.Concat(col.HeaderText));
sb.Append("</td>");
}
}
foreach (DataGridViewRow row in dgv.Rows)
{
//Add rows to the table for the length of the dataset
sb.Append(" <tr>");
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.OwningColumn.Index == dgv.Columns.Count - 1)
{
if (cell.Value != null)
{
sb.Append("<td>");
sb.Append(string.Concat(cell.Value.ToString(), ", "));
sb.Append("</td>");
}
else
{
sb.Append("<td>");
sb.Append(filler);
sb.Append("</td>");
}
}
else
{
if (cell.Value != null)
{
sb.Append("<td>");
sb.Append(string.Concat(cell.Value.ToString(), ", "));
sb.Append("</td>");
}
else
{
sb.Append("<td>");
sb.Append(string.Concat("", "none"));
sb.Append("</td>");
}
}
}
}
}
sb.Append(" </tr>");
sb.Append("</table>");
sb.Append("</body>");
sb.Append("</html>");
// Get string
string myHtmlString = sb.ToString() ;
// or write to textfile
StreamWriter wr = new StreamWriter(txtFile);
wr.Write(sb);
wr.Close(); |
Partager