源码:
private void saveListView2HtmlTable(ListView listview)
{
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("<html><head><title>Reports{0}</title></head><body><table border=\"1\"><thead>", DateTime.Now.ToString()));
foreach (ColumnHeader ch in listview.Columns)
{
sb.Append(string.Format("<th> {0} </th>", ch.Text));
}
sb.Append("</thead>");
foreach (ListViewItem item in listview.Items)
{
sb.Append("<tr>");
foreach(System.Windows.Forms.ListViewItem.ListViewSubItem subitem in item.SubItems)
{
sb.Append("<td>");
sb.Append(subitem.Text);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("<tbody></body>");
FileStream fs = new FileStream("a.html", FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(sb.ToString());
sw.Close();
fs.Close();
}