从vb.net datagridview生成html表

时间:2014-08-15 12:44:41

标签: html vb.net datagridview

我需要根据datagridview中的数据生成HTML表格。 (如果有人想知道,我使用vb.net)

datagridview由5列组成,而行数可以根据最终用户记录的操作数量来计算。

我试图生成的表代码的结构如下:

    <tr>
<td>column 1 row1</td>
<td>column 2 row1</td>
<td>column 3 row1</td>
<td>column 4 row1</td>
<td>column 5 row1</td>
</tr>
    <tr>
<td>column 1 row2</td>
<td>column 2 row2</td>
<td>column 3 row2</td>
<td>column 4 row2</td>
<td>column 5 row2</td>
</tr>
    <tr>
<td>column 1 row3</td>
<td>column 2 row3</td>
<td>column 3 row3</td>
<td>column 4 row3</td>
<td>column 5 row3</td>
</tr>

ect....

我遇到的麻烦就是在保存完整行之前和之后放置和标签。以下代码显示我试图解决此问题,但我已经迷失了方向,然后发现是时候在论坛上寻求帮助了

注意:(请原谅代码中出现的错误,这些错误是在尝试让事情发生后调整的。)

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim col1 As String
    Dim col2 As String

    Dim Count As Integer = 1
    For i As Integer = 0 To DataGridView1.Rows.Count - 1

        If Count = 5 Then

            For Each row As DataGridViewRow In DataGridView1.Rows

                If Not row.IsNewRow Then

                    col1 = row.Cells(0).Value.ToString
                    col2 = row.Cells(1).Value.ToString

                    MsgBox("<tr><td>" & col1 & " " & col2 &"</td></tr>")
                    Count = 0
                End If
            Next

        ElseIf Not Count = 5 Then

            For j As Integer = 0 To DataGridView1.Rows.Count - 1
                For Each row2 As DataGridViewRow In DataGridView1.Rows

                    If Not row2.IsNewRow Then

                        col1 = row2.Cells(0).Value.ToString
                        col2 = row2.Cells(1).Value.ToString

                        MsgBox("<td>" & col1 & " " & col2 & "</td>")

                    End If
                Next
                Count = +1
            Next
        End If
    Next

    End

1 个答案:

答案 0 :(得分:0)

以下是我最近使用的一些代码示例,它将DataGridView呈现为HTML表格以进行电子邮件发送。它在C#中,但翻译起来应该不会太难。它应该非常简单,您要添加自动将HTML应用于数据的HTML元素。

 using System.Web.UI;
 using Web = System.Web.UI.WebControls;

// ...

 private string DataGridViewToHTML()
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        Web.Label introBody = new Web.Label();
        introBody.Text = "";
        introBody.ID = "bodyText";
        introBody.RenderControl(hw);

        hw.WriteBreak();
        hw.WriteBreak();

        //Table for data.
        Web.Table tbl = new Web.Table();
        tbl.GridLines = Web.GridLines.Both;
        Web.TableHeaderRow thr = new Web.TableHeaderRow();
        // add column headers
        foreach(DataGridViewColumn col in dgvRcds.Columns)
        {
            Web.TableHeaderCell thc = new Web.TableHeaderCell();
            thc.Text = col.HeaderText;
            thr.Controls.Add(thc);
        }
        tbl.Controls.Add(thr);
        //add each row
        foreach(DataGridViewRow row in dgvRcds.Rows)
        {
            Web.TableRow tr = new Web.TableRow();
            foreach(DataGridViewCell val in row.Cells)
            {
                Web.TableCell tc = new Web.TableCell();
                tc.Text = val.EditedFormattedValue.ToString();
                tr.Controls.Add(tc);
            }
            tbl.Controls.Add(tr);
        }
        //render table in html
        tbl.RenderControl(hw);

        //return raw html string.
        return sw.ToString();
    }