我有Gridview
,我有三个选项可以将数据导出到Excel工作表:
它与当前有效但其他选项无效。
代码:
protected void btnExportGrid_Click(object sender, EventArgs e)
{
Table table = new Table
{
GridLines = this.gv_RquestedOrdres.GridLines
};
if (this.rdoBtnListExportOptions.SelectedIndex == 1)
{
this.gv_RquestedOrdres.AllowPaging = false;
this.gv_RquestedOrdres.DataBind();
}
else if (this.rdoBtnListExportOptions.SelectedIndex == 2)
{
this.gv_RquestedOrdres.PageSize = 100;
this.gv_RquestedOrdres.DataBind();
}
GridViewExportUtil.Export("Orders.xls", this.gv_RquestedOrdres);
}
private string Gridview(Panel gv)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv.RenderControl(hw);
return sb.ToString();
}
GridViewExportUtil类:
public class GridViewExportUtil
{
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
using (StringWriter writer = new StringWriter())
{
using (HtmlTextWriter writer2 = new HtmlTextWriter(writer))
{
Table table = new Table();
if (gv.HeaderRow != null)
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
foreach (GridViewRow row in gv.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
if (gv.FooterRow != null)
{
PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.RenderControl(writer2);
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
}
}
}
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control control2 = control.Controls[i];
if (control2 is LinkButton)
{
control.Controls.Remove(control2);
control.Controls.AddAt(i, new LiteralControl((control2 as LinkButton).Text));
}
else if (control2 is ImageButton)
{
control.Controls.Remove(control2);
control.Controls.AddAt(i, new LiteralControl((control2 as ImageButton).AlternateText));
}
else if (control2 is HyperLink)
{
control.Controls.Remove(control2);
control.Controls.AddAt(i, new LiteralControl((control2 as HyperLink).Text));
}
else if (control2 is DropDownList)
{
control.Controls.Remove(control2);
control.Controls.AddAt(i, new LiteralControl((control2 as DropDownList).SelectedItem.Text));
}
else if (control2 is CheckBox)
{
control.Controls.Remove(control2);
control.Controls.AddAt(i, new LiteralControl((control2 as CheckBox).Checked ? "True" : "False"));
}
if (control2.HasControls())
{
PrepareControlForExport(control2);
}
}
}
}
答案 0 :(得分:0)
您正在使用网格视图导出数据 - 这只意味着网格视图的当前页面将呈现为导出的html。要呈现完整数据(或前100行),请相应地设置页面大小。因此页面大小为100行,页面索引为1将导致前100行导出,而禁用分页应该为您提供所有页面 - 一旦导出结束,则还原页面。
另一个替代方法是使用实际数据(您已绑定到网格)进行CSV导出(可以在Excel中打开)(或使用Table
和Row
进行html导出但是而不是使用网格视图行,使用数据将单元格添加到html Row
)