快意人生

 

Sub ToExcel()
        
Try
            
Dim filename As String
            
Dim strpath As String
            strpath 
= Server.MapPath("\Excel\" & filename)
            filename 
= strpath & Date.Now.ToString("yyyyMMddHHmmss"& ".xls"
            HttpContext.Current.Response.AddHeader(
"Content-Disposition""attachment;filename=" & HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8))
            
'HttpContext.Current.Response.Charset = "gb2312"
            'HttpContext.Current.Response.Charset = "utf8"
            'HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default
            'HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8")
            HttpContext.Current.Response.ContentType 
= "application/vnd.ms-excel"
            
Dim tw As System.IO.StringWriter
            
Dim hw As System.Web.UI.HtmlTextWriter
            hw 
= New System.Web.UI.HtmlTextWriter(tw)
            myhtmltable.RenderControl(hw)
            HttpContext.Current.Response.Write(tw.ToString)
            DownFile(filename, strpath)
            HttpContext.Current.Response.End()
        
Catch ex As Exception
            ShowMessage(
"无法下载!")
        
End Try
    
End Sub

    
Sub DownFile(ByVal filename As StringByVal filepath As String)
        
Try
            
Dim Response As HttpResponse
            Response 
= Page.Response
            Response.Clear()
            Response.ContentType 
= "application/octet-stream"
            Response.AppendHeader(
"Content-Disposition""attachment;filename=" & HttpUtility.UrlEncode(filename, System.Text.Encoding.Default) & ";charset=GB2312")
            
' Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) & ";charset=utf8")
            Response.Flush()
            Response.WriteFile(filepath)
        
Catch ex As Exception
            ShowMessage(
"无法下载!")
        
End Try
    
End Sub
posted on 2006-08-10 13:03  快意人生  阅读(160)  评论(0)    收藏  举报