Hello!
I want to export listview to Excel. It is necessary that the Excel recognize first column as the text. I will make so, but it don't work
<style type="text/css"> .text {mso-number-format:\@; }</style><asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" onitemdatabound="ListView1_ItemDataBound"><LayoutTemplate><table id="Table1" runat="server" cellpadding="2" border="1" cellspacing="0"><tr id="Tr1" runat="server" ><td id="Td1" runat="server" class="text">Number</td><td id="Td2" runat="server">Name</td><td id="Td7" runat="server">Price</td></tr><tr id="ItemPlaceholder" runat="server"></tr></table> </LayoutTemplate><ItemTemplate><tr><td><asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label></td><td><%#Eval("Name") %></td><td><%#Eval("Price") %></td></tr></ItemTemplate></asp:ListView>
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { Label text = (Label)e.Item.FindControl("Label1"); string id = text.Text; if (id.Length == 1) {text.Text = "00000" + text.Text; } if (id.Length == 2) { text.Text = "0000" + text.Text; } if (id.Length == 3) { text.Text = "000" + text.Text; } if (id.Length == 4) { text.Text = "00" + text.Text; } if (id.Length == 5) { text.Text = "0" + text.Text; } ListView1.DataBind(); System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw); ListView1.RenderControl(htw); string renderedGridView = sw.ToString(); File.WriteAllText(@"D:\new.xls", renderedGridView); }