我想从列表视图中导出数据。
我尝试了以下内容:
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>
请建议一个解决方案,只将listview
的内容导出到 excel 。
答案 0 :(得分:0)
我已成功用于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();
答案 1 :(得分:0)
这是我的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>
我建议您在ListView中创建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());
}