下载DataGrid内容,作为Excel可打开的文件
2006-09-10 18:46
543 查看
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AIS.Web
{
/// <summary>
/// 把GridView作为文件下载,可供Excel打开
/// </summary>
public class DataGridToFile
{
public DataGridToFile()
{}
public static void Download(DataGrid dg)
{
Download(dg, "report_" + DateTime.Now.ToString("yyyyMMdd") +".xls" );
}
public static void Download(DataGrid dg, string fileName)
{
TranComplexCtlToLiteral(dg);
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw=new HtmlTextWriter(sw);
dg.RenderControl(htw);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer=false;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+fileName);
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.Charset="GB2312";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
/// <summary>
/// 把复杂控件都变为只显示文字的控件
/// </summary>
private static void TranComplexCtlToLiteral(WebControl ctl)
{
if(ctl==null) return;
if(ctl.HasControls()==false) return;
for(int i =0; i < ctl.Controls.Count; i++)
{
Literal l=new Literal();
WebControl curChild=ctl.Controls[i] as WebControl;
if(curChild is HyperLink)
{
HyperLink hl=curChild as HyperLink;
l.Text = hl.Text;
ctl.Controls.Remove(curChild);
ctl.Controls.AddAt(i,l);
}
else if(curChild is DropDownList)
{
DropDownList ddl=curChild as DropDownList;
l.Text=ddl.SelectedItem.Text;
ctl.Controls.Remove(curChild);
ctl.Controls.AddAt(i,l);
}
TranComplexCtlToLiteral(curChild);
}
}
}
}
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AIS.Web
{
/// <summary>
/// 把GridView作为文件下载,可供Excel打开
/// </summary>
public class DataGridToFile
{
public DataGridToFile()
{}
public static void Download(DataGrid dg)
{
Download(dg, "report_" + DateTime.Now.ToString("yyyyMMdd") +".xls" );
}
public static void Download(DataGrid dg, string fileName)
{
TranComplexCtlToLiteral(dg);
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw=new HtmlTextWriter(sw);
dg.RenderControl(htw);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer=false;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+fileName);
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.Charset="GB2312";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
/// <summary>
/// 把复杂控件都变为只显示文字的控件
/// </summary>
private static void TranComplexCtlToLiteral(WebControl ctl)
{
if(ctl==null) return;
if(ctl.HasControls()==false) return;
for(int i =0; i < ctl.Controls.Count; i++)
{
Literal l=new Literal();
WebControl curChild=ctl.Controls[i] as WebControl;
if(curChild is HyperLink)
{
HyperLink hl=curChild as HyperLink;
l.Text = hl.Text;
ctl.Controls.Remove(curChild);
ctl.Controls.AddAt(i,l);
}
else if(curChild is DropDownList)
{
DropDownList ddl=curChild as DropDownList;
l.Text=ddl.SelectedItem.Text;
ctl.Controls.Remove(curChild);
ctl.Controls.AddAt(i,l);
}
TranComplexCtlToLiteral(curChild);
}
}
}
}
相关文章推荐
- 解决PHP文件(word/excel)下载 内容打开乱码问题
- JAVA JXL -----excel文件生成、在线打开和下载
- weblogic服务上传word等文件直接打开问题解决 博客分类: web应用服务器 在weblogic上发布的web项目,测试中发现出现当上传word、excel、pdf等文件在下载的时候出现
- 将DataGrid中数据倒出Excel文件并下载
- DataGrid内容导出Excel文件(C#)
- 解决tomcat中rar、excel格式文件在下载时直接以乱码打开
- datagrid数据导出到excel文件给客户端下载的几种方法
- 解决ie下载excel文件直接在浏览器打开的问题(tomcat,web项目)
- 解决使用超链接下载word/EXCEL文件时被IE打开的问题
- 将DataGrid内容生成标准的Excel格式文件
- datagrid数据导出到excel文件给客户端下载的几种方法 (转)
- 将DataGrid中数据倒出Excel文件并下载
- [转]datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- (转载)datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- IE中点击链接下载EXCEL文件直接以乱码方式打开解决
- 打开网上下载的CHM文件什么内容也看不到
- ios 使用NSURlSession 下载文件并且使用进度条进行文件下载进度读取以及打开附件(word,excel,ppt,pdf)显示
- asp.net中将DataGrid的内容导出为excel文件