首页 > Web开发 > 详细

.net web端导出Excel个人的看法

时间:2014-03-06 22:37:45      阅读:501      评论:0      收藏:0      [点我收藏+]

 

//对已有方法进行重写

public override void VerifyRenderingInServerForm(Control control)
{ }

 

 

//设置文件名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");

//HTTP请求内容
System.Web.HttpContext curContext = System.Web.HttpContext.Current;

// 输出文件流
System.IO.StringWriter strWriter = null;

//服务端控件输出流
System.Web.UI.HtmlTextWriter htmlWriter = null;

// 清空当前 Response
HttpContext.Current.Response.Clear();

//设置缓冲方式输出
HttpContext.Current.Response.Buffer = true;

//添加HTTP标头到输出流
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
curContext.Response.ContentType = "application/vnd.ms-excel"; //设置输出文件类型为xls文件

/**如果要输出doc文件,请用下面这段代码**/
//HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.doc", fileName));
//curContext.Response.ContentType = "application/ms-word"; //设置输出文件类型为word文件

//设置编码方式
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
curContext.Response.Charset = "";

// 导出文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

htmlWriter.Write("测试标题1");
//for (int i = 0; i < ds.Columns.Count; i++)
//{
// htmlWriter.Write(ds.Columns[i].Caption.ToString());
//}
//
// 返回客户端
// DataList1.RenderControl
GridView1.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString().Replace("<br/>", "<br style=‘mso-data-placement:same-cell;‘/> "));

curContext.Response.End();

//设置文件名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");

//HTTP请求内容
System.Web.HttpContext curContext = System.Web.HttpContext.Current;

// 输出文件流
System.IO.StringWriter strWriter = null;

//服务端控件输出流
System.Web.UI.HtmlTextWriter htmlWriter = null;

// 清空当前 Response
HttpContext.Current.Response.Clear();

//设置缓冲方式输出
HttpContext.Current.Response.Buffer = true;

//添加HTTP标头到输出流
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
curContext.Response.ContentType = "application/vnd.ms-excel"; //设置输出文件类型为xls文件

/**如果要输出doc文件,请用下面这段代码**/
//HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.doc", fileName));
//curContext.Response.ContentType = "application/ms-word"; //设置输出文件类型为word文件

//设置编码方式
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
curContext.Response.Charset = "";

// 导出文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

htmlWriter.Write("测试标题1");
//for (int i = 0; i < ds.Columns.Count; i++)
//{
// htmlWriter.Write(ds.Columns[i].Caption.ToString());
//}
//
// 返回客户端

GridView1.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString().Replace("<br/>", "<br style=‘mso-data-placement:same-cell;‘/> "));

curContext.Response.End();

.net web端导出Excel个人的看法,布布扣,bubuko.com

.net web端导出Excel个人的看法

原文:http://www.cnblogs.com/chenchenJJ/p/3583844.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!