将DataGridView导出到HTML页面 [英] Export DataGridView to HTML Page

查看:144
本文介绍了将DataGridView导出到HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到这个问题的答案,我没有取得任何成功。我的问题是这样的。我有一个comboBox列出不同的格式导出报告。其中一个选项是HTML。基本上我想做的是将dataGridView以Windows窗体的形式导出为HTML页面。我想要导出到HTML表。我甚至不知道如何开始,所以我没有提供任何示例代码。我正在使用c sharp 2008 windows应用程序。任何建议将不胜感激。

解决方案

  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转换。



DataGridView to Email


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 to Email

这篇关于将DataGridView导出到HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆