示例代码如下:
using System.IO;
using System.Text;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Business.Conn con=new ............System.Configuration.ConfigurationSettings.AppSettings["Conn"]);
DataSet da=new DataSet();
da=con.Read("tbl_company","CompanyID,CompanyName,AddedDate","''",30,1,0,1,"ID<=125");
DataGridHuashanlin.DataSource=da.Tables[0].DefaultView;
DataGridHuashanlin.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear(); //将缓冲区中的所有数据流内容全部清除
Response.Buffer= true; //设置是否将数据流缓冲输出
Response.Charset="GB2312";//设置输出流的http字符集格式
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode("这里可以用中文名.xls",System.Text.Encoding.UTF8)); //在数据流前添加http头
//attachment --- 意为作为附件下载
//inline --- 意为在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)进行编码,以解决文件名乱码的问题
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType = "Response.ContentType";
//application/ms-word --- word文件
//application/vnd.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);
this.DataGridHuashanlin.RenderControl(oHtmlTextWriter);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Write(oStringWriter.ToString());
Response.End();
}
using System.IO;
using System.Text;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Business.Conn con=new ............System.Configuration.ConfigurationSettings.AppSettings["Conn"]);
DataSet da=new DataSet();
da=con.Read("tbl_company","CompanyID,CompanyName,AddedDate","''",30,1,0,1,"ID<=125");
DataGridHuashanlin.DataSource=da.Tables[0].DefaultView;
DataGridHuashanlin.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear(); //将缓冲区中的所有数据流内容全部清除
Response.Buffer= true; //设置是否将数据流缓冲输出
Response.Charset="GB2312";//设置输出流的http字符集格式
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode("这里可以用中文名.xls",System.Text.Encoding.UTF8)); //在数据流前添加http头
//attachment --- 意为作为附件下载
//inline --- 意为在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)进行编码,以解决文件名乱码的问题
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType = "Response.ContentType";
//application/ms-word --- word文件
//application/vnd.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);
this.DataGridHuashanlin.RenderControl(oHtmlTextWriter);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Write(oStringWriter.ToString());
Response.End();
}