Gridview中的数据导出到excel中
来源:互联网 发布:ubuntu 设置默认路径 编辑:程序博客网 时间:2024/05/04 14:19
protected void btnExport_Click(object sender, EventArgs e)
{
//导出全部数据,取消分页
gvInfo.AllowPaging = false;
gvInfo.ShowFooter = false;
GetDataSource(condition);
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出" + System.DateTime.Now.Date.ToString("yyyyMMdd")) + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
ClearControls(gvInfo);
this.gvInfo.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
//还原分页显示
gvInfo.AllowPaging = true;
gvInfo.ShowFooter = true;
GetDataSource(condition);
}
/// <summary>
/// GridView如果需要实现导出Excel功能,则该函数需要重载
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
/// <summary>
/// 清除控件中的所有控件,以便导出Excel
/// </summary>
/// <param name="control"></param>
private void ClearControls(Control control)
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
control.Parent.Controls.Remove(control);
}
}
return;
}
0 0
- Gridview中的数据导出到excel中
- 将gridview中的数据导出到excel中
- 将GridView中的数据导出到Excel / Word中
- 将GridView中的数据导出到Excel / Word中
- 导出GridView中的数据到Excel
- GridView数据导出到Word/Excel中
- gridview 数据导出到excel中
- GridView数据导出到Excel中
- GridView导出数据到Excel
- GridView导出数据到Excel
- GridView导出数据到Excel
- GridView导出数据到Excel
- Gridview导出数据到Excel
- 导出gridview数据到Excel
- gridview导出数据到excel
- 导出GridView数据到Excel
- GridView导出数据到Excel
- GridView导出数据到Excel
- Android笔记 动画之tween(补间)动画demo
- IOS 实现录音PCM转MP3格式(边录音边转码)
- 王立平--图片加密
- 利用crsctl生产resource脚本,并生成新的资源
- 圆形进度条
- Gridview中的数据导出到excel中
- 条件编译
- Leetcode:Largest Rectangle in Histogram与Maximal Rectangle
- Stack和Heap
- centos6.5 ssh 连接及ftp服务建立~~~
- main函数执行前、后再执行的代码
- iOS沙盒机制及文件操作 NSFileManager的基本使用
- C语言宏定义的使用
- DateUtil