简体   繁体   中英

Exporting to from HTML table to excel in vb.net

I am a newbie in the asp.net world.
I have any asp.net application where I get data from a stored procedure in a datatable.
I would like to populate the data in an HTML table and then export it to excel.

Unfortunately these have to be done the long winded way (each column individually), since the data is modified based on user login credentials, before it is exported to excel.

Here is what I have (pretty basic)

<table>
  <tr>
    <td>EmployeeID</td>
    <td>EmployeeFirstName</td>
    <td>EmployeeLastName</td>
    <td>EmployeeLastName</td>
  </tr>
</table>  

If DataTable.HasRows Then
.....
.....
End If

To populate it to the HTML table you must create the table dynamically from the VB server side code. you must define amount of cells and rows from your stored procedure.

you basically have to define your Html table like this example and populate it with the info from your stored procedure.

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
    ' Create a new HtmlTable object.
    Dim table1 As New HtmlTable()



    ' Start adding content to the table.
    Dim row As HtmlTableRow
    Dim cell As HtmlTableCell
    For i As Integer = 1 To 8
        ' Create a new row and set its background color.
        row = New HtmlTableRow()

        For j As Integer = 1 To 8
            ' Create a cell and set its text.
            cell = New HtmlTableCell()
            cell.InnerHtml = "Row: " & i.ToString() & "<br />Cell: " & j.ToString()
            ' Add the cell to the current row.
            row.Cells.Add(cell)
        Next

        ' Add the row to the table.
        table1.Rows.Add(row)
    Next

    ' Add the table to the page.
    Me.Controls.Add(table1)
End Sub

and than add it to the Me panel.

here is the link how to export html to excel http://www.devx.com/tips/Tip/14235

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM