导出时先弹出一个文件保存框,但点保存后又弹出一个web页面保存框,也就时说只能保存web页面!!不知道何因!!
代码:
HttpResponse response = HttpContext.Current.Response; 
            
response.Charset = "GB2312"; 
response.ContentEncoding = Encoding.GetEncoding("GB2312"); 
response.ContentType = "application/ms-excel/msword"; 
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename)); 
 
CultureInfo cult = new CultureInfo("zh-CN", true); 
StringWriter sw = new StringWriter(cult);             
HtmlTextWriter writer = new HtmlTextWriter(sw); 
 
writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">"); 
 
DataGrid dGrid = (DataGrid)ct; 
             
if (dGrid != null) 

dGrid.RenderControl(writer); 
}  response.Write(sw); 
response.End();

解决方案 »

  1.   

    Public Function ExportToExcel(ByVal strFileName As String, ByVal Page As UI.Page) As Boolean
            Page.Response.Clear()
            Page.Response.Buffer = True
            Page.Response.Charset = "UTF8"        
            Page.Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(strFileName & ".xls"))
            Page.Response.ContentEncoding = System.Text.Encoding.UTF8
            Page.Response.ContentType = "application/ms-excel"            
            Page.EnableViewState = False
            Dim myCItrad As System.Globalization.CultureInfo
            myCItrad = New System.Globalization.CultureInfo("ZH-CN", True)
            Dim oStringWriter As System.IO.StringWriter
            oStringWriter = New System.IO.StringWriter(myCItrad)
            Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter
            oHtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
            FTable.RenderControl(oHtmlTextWriter)
            Page.Response.Write(oStringWriter.ToString())
            Page.Response.End()
        End Function
      

  2.   

    http://community.csdn.net/Expert/topic/4593/4593069.xml?temp=.4948542