将DataGridView导出到HTML页面 [英] Export DataGridView to HTML Page
本文介绍了将DataGridView导出到HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
private StringBuilder DataGridtoHTML(DataGridView dg)
{
StringBuilder strB = new StringBuilder();
//创建html& table
strB.AppendLine(< html>< body>< center><+
table border ='1'cellpadding ='0'cellspacing ='0'> );
strB.AppendLine(< tr>);
// cteate table header
for(int i = 0; i< dg.Columns.Count; i ++)
{
strB.AppendLine(< td align = 'center'valign ='middle'>+
dg.Columns [i] .HeaderText +< / td>);
}
//创建表体
strB.AppendLine(< tr>); (int i = 0; i< dg.Rows.Count; i ++)
{
strB.AppendLine(< tr>);
foreach(DataGridViewCell dgvc in dg.Rows [i] .Cells)
{
strB.AppendLine(< td align ='center'valign ='middle'>+
dgvc.Value.ToString()+< / td>);
}
strB.AppendLine(< / tr>);
}
// table footer&结束html文件
strB.AppendLine(< / table>< / center>< / body>< / html>);
return strB;}
上面的代码来自下面的链接,但我只是给你需要哪些是DataGridView进行HTML转换。
I have tried to find an answer to this question and I have not had any success. My issue is this. I have a comboBox that lists different formats to export a report to. One of those options is HTML. Basically what I want to do is take the dataGridView in a windows form and export as is to an HTML page. I would like to just export to an HTML table. I don't even know how to start on this so I don't have any sample code to provide. I am using c sharp 2008 windows app. Any advise would be greatly appreciated.
解决方案
private StringBuilder DataGridtoHTML(DataGridView dg)
{
StringBuilder strB = new StringBuilder();
//create html & table
strB.AppendLine("<html><body><center><" +
"table border='1' cellpadding='0' cellspacing='0'>");
strB.AppendLine("<tr>");
//cteate table header
for (int i = 0; i < dg.Columns.Count; i++)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dg.Columns[i].HeaderText + "</td>");
}
//create table body
strB.AppendLine("<tr>");
for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine("<tr>");
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dgvc.Value.ToString() + "</td>");
}
strB.AppendLine("</tr>");
}
//table footer & end of html file
strB.AppendLine("</table></center></body></html>");
return strB;}
The code above is from the link below but I am just giving you what you need which is DataGridView to HTML conversion.
这篇关于将DataGridView导出到HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文