web 導出 csv

public void ProcessRequest(HttpContext context)
        {
            //DownloadFile("教程.csv", "a.csv");服務器

            context.Response.Charset = "UTF-8";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.HeaderEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("測試.csv"));
            context.Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });  app

            context.Response.Write("編號,姓名\n1,邸萌");測試

            context.Response.Flush();
            context.Response.End();
        }code

        #region 提供文件下載的方法【只限於服務器端已經存在的文件】
        /// <summary>
        /// 提供文件下載的方法【只限於服務器端已經存在的文件】
        /// </summary>
        /// <param name="fileName">必定要有後綴名。下載保存時文件的名稱,能夠與實際文件名稱不相同,能夠是中文</param>
        /// <param name="filePath">要下載文件的虛擬路徑</param>
        public void DownloadFile(string fileName, string filePath)
        {
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
            HttpContext.Current.Response.WriteFile(filePath);
        }
        #endregion教程

相關文章
相關標籤/搜索