方法一:
public void ToExcel(System.Web.UI.WebControls.GridView  gw, string filename)
        
{
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.AddHeader(
"content-disposition",
            
"attachment;filename=FileName.xls");
            System.Web.HttpContext.Current.Response.Charset 
= "";
            
// If you want the option to open the Excel file without saving than
            
// comment out the line below
            
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
            System.Web.HttpContext.Current.Response.ContentType = "application/" + filename;
            System.IO.StringWriter stringWrite 
= new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite 
= new HtmlTextWriter(stringWrite);
            
            gw.RenderControl(htmlWrite);
            System.Web.HttpContext.Current.Response.Write(stringWrite.ToString());
            System.Web.HttpContext.Current.Response.End();
            
        }


方法二:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void ExportToExcel(string Filename, GridView gridview,Page page) 

page.Response.Clear(); 
// 防止中文内容为乱码 
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
//可令中文文件名不为乱码 
page.Response.AppendHeader("content-disposition""attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".xls\""); 
StringWriter sw 
= new StringWriter(); 
HtmlTextWriter htw 
= new HtmlTextWriter(sw); 
gridview.RenderControl(htw); 
page.Response.Write(sw.ToString()); 
page.Response.End(); 
}
 

调用方法:类.ExportToExcel(
"中文文件名",GridView1,this.Page);
常见问题及解决办法:
1、如果出现下面的错误提示可用重载VerifyRenderingInServerForm方法解决。

错误提示:
类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内

在后台文件中重载VerifyRenderingInServerForm方法,如:

public override void VerifyRenderingInServerForm(Control control)
{
     
//base.VerifyRenderingInServerForm(control);
}


2、如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。

可用Response.ContentEncoding = System.Text.Encoding.UTF7;
或者Encoding.UTF8等来解决,不过导入格式和字体上个人感觉UTF7比UTF8效果好些;
因人而异了:)
---------------------------------------------------------------------------------------
如何设置导出的数据字段是文本格式(如001  导出的结果是 1 )
解决方法:(加入红色字体的两个语句就可以解决了)
参考:http://hi.baidu.com/erics_lele/blog/item/8d2db2ec2ed3b73c269791a9.html
protected void Btn_ExportClick(object sender, EventArgs e)

 

{
string style = @"<style> .text { mso-number-format:\@; } </script> " ;

 

Response.ClearContent();

 

Response.AddHeader("content-disposition" , "attachment; filename=MyExcelFile.xls" );

 

Response.ContentType = "application/excel" ;

 

StringWriter sw = new StringWriter ();

 

HtmlTextWriter htw = new HtmlTextWriter (sw);

 

gvUsers.RenderControl(htw);

 

// Style is added dynamically

 

Response.Write(style);

 

Response.Write(sw.ToString());

 

Response.End();

 

}

posted on 2007-01-04 22:35  ipusr  阅读(511)  评论(0)    收藏  举报