导出列表视图数据在asp.net中擅长
我想从列表视图导出数据。
我已经尝试了以下内容:
protected void btnExporttoExcel_Click(object sender, EventArgs e) { try { Response.Buffer = true; Response.ContentType = "application/x-msexcel"; Response.AddHeader("Content-Disposition", "attachment; filename=asdf.xls"); Response.ContentEncoding = Encoding.UTF8; Response.Charset = ""; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); if (dropdown1.SelectedValue=="1") { listview.RenderControl(hw); } Response.Write(sw.ToString()); Response.End(); } catch (Exception ex) { //catching exception } }
在这我获得输出,但数据导出为<HTML>
标签格式。帮我导出只有数据从列表视图。
<thead> <tr> </tr> </thead> <tr> <td>data</td> <td>data</td> <td style="text-align:center;">data</td> <td style="text-align:right;">data</td> </tr> <tr> <td>data</td> <td>data</td> <td style="text-align:center;">data</td> <td style="text-align:right;">data</td> </tr> <tr> <td>data</td> <td>data</td> <td style="text-align:center;">data</td> <td style="text-align:right;">data</td> </tr> <tr> <td>data</td> <td>data</td> <td style="text-align:center;">data</td> <td style="text-align:right;">data</td> </tr>
请build议一个解决scheme,只导出到excel listview
的内容。
我已经成功地使用了这个GridView
但我相信它可以用于ListView
相同的方式。
Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=asdf.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); listview.DataBind(); if (dropdown1.SelectedValue == "1") listview.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End();
这是我的ListView代码
<asp:ListView ID="paidlv" DataSourceID="SqlDataSource1" runat="server" Visible="true"> <LayoutTemplate> <table class="detail"> <tr> <th>First Name</th> <th>Last Name</th> <th>Telephone</th> <th>Email</th> </tr> <tr id="itemPlaceholder" runat="server"></tr> </table> </LayoutTemplate> <ItemTemplate> <tr class="altRow rowcolor"> <td><asp:Label runat="server" ID="lblFName" Text='<%#Eval("fname") %>'></asp:Label></td> <td><asp:Label runat="server" ID="lblLName" Text='<%#Eval("lname") %>'></asp:Label></td> <td><asp:Label runat="server" ID="lblTelephone" Text='<%#Eval("telephone") %>'></asp:Label></td> <td><asp:Label runat="server" ID="lblEmail" Text='<%#Eval("email") %>'></asp:Label></td> </tr> </ItemTemplate> </asp:ListView>
我build议你在ListView中创build一个DataTable数据表,然后将该DataTable绑定到新的GridView对象,然后导出该GridView对象。
这里是示例代码:
private DataTable GetDataTable() { DataTable table = new DataTable(); table.Columns.Add("FirstName"); table.Columns.Add("LastName"); table.Columns.Add("Telephone"); table.Columns.Add("Email"); foreach (ListViewItem lvi in paidlv.Items) { Label lblFName = lvi.FindControl("lblFName") as Label; Label lblLName = lvi.FindControl("lblLName") as Label; Label lblTelephone = lvi.FindControl("lblTelephone") as Label; Label lblEmail = lvi.FindControl("lblEmail") as Label; DataRow row = table.NewRow(); row[0] = lblFName.Text; row[1] = lblLName.Text; row[2] = lblTelephone.Text; row[3] = lblEmail.Text; table.Rows.Add(row); } return table; } private void DataTableToExcel(DataTable dataTable) { GridView tempGrid = new GridView(); tempGrid.AutoGenerateColumns = true; tempGrid.DataSource = dataTable; tempGrid.DataBind(); try { Response.Clear(); Response.ContentType = "application/application/ms-excel"; Response.AddHeader("content-disposition", "attachment;filename=asdf.xls"); Response.ContentEncoding = Encoding.UTF8; Response.BinaryWrite(Encoding.UTF8.GetPreamble()); Response.Charset = ""; using (StringWriter sw = new StringWriter()) { // You could also write other text here using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { //You could also set cell style here if (dropdown1.SelectedValue == "1") { tempGrid.RenderControl(hw); } } Response.Write(sw.ToString()); } Response.End(); } catch (Exception ex) { //catching exception } } protected void btnExporttoExcel_Click(object sender, EventArgs e) { DataTableToExcel(GetDataTable()); }