简体   繁体   中英

Listview to Excel HTML markup issue

I'm using code similar to code mention at http://www.dutch-creatives.com/post/2009/08/11/Export-ListView-to-Excel.aspx to export Listview to excel

My listview

<asp:ListView ID="lv" runat="server">                         
      <LayoutTemplate>
          <table runat="server" id="table1" class="datatable" >
               <tr class="row" id="tableRow" runat="server">
                 <th rowspan="2"></th>
               </tr>
                <tr runat="server" id="itemPlaceholder"></tr>
          </table>
      </LayoutTemplate> 
      <ItemTemplate>
          <tr id="Tr1" runat="server"  class="row">
               <td id="Td1" runat="server">
                    <asp:Label ID="Label6" runat="server" 
                              Text='<%#Eval("xyz") %>' />
               </td>
          </tr>
       </ItemTemplate>    
  </asp:ListView>

I'm adding header as follows

 ' Create a form to contain the grid
                Dim table = New HtmlTable()
                Dim tCell = New HtmlTableCell()
                Dim tRow = New HtmlTableRow()

                'Add the header row
                Dim header As HtmlTableRow = DirectCast(_listview.FindControl("tableRow"), HtmlTableRow)
                ExlUtil.ConvertControl(header)
                table.Rows.Add(header)

And then each item as

For Each item As ListViewDataItem In _listview.Items
                    tCell.Controls.Add(item)
                    tRow.Controls.Add(tCell)
                    ExlUtil.ConvertControl(item)
                    table.Rows.Add(tRow)
                Next



table.RenderControl(htw) //htw is htmltextwriter object

But this(table.RenderControl) is rendering final html code as

<table>
  <tr>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  <tr> 'problem here
    <td> 'problem here

        <tr> <td></td> </tr>
        <tr> <td></td> </tr>

    </td>
  </tr>
</table>

I tried to debug, understand why it is generating an extra tr td but couldn't. Can anyone tell me why it is generating that extra tr,td markup?

Fixed this by changing evrything to just two lines of code

ExlUtil.ConvertControl(_listview)
                _listview.RenderControl(htw)

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