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

 

//对已有方法进行重写

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个人的看法,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。