/// <summary>
        /// 文本流送到客户端,关显示为另存为
        /// </summary>
        /// <param name=”response”>页面response类</param>
        /// <param name=”fileName”>另存为的文件名</param>
        public void FileWebSaveAs(HttpResponseBase response, string fileName)
        {
            response.Clear();
            response.ClearHeaders();
            response.Buffer = false;
            response.AddHeader(“Content-Disposition”, “attachment; filename=” + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + “.xls”);
            response.ContentType = “application/ms-excel”;
            response.ContentEncoding = System.Text.Encoding.UTF8;

            //response.Write(DataTableToExcelStr(_dt));
            System.IO.MemoryStream ms;
            string strExcel = ListToExcelStr(_listItem);
            int Step = 1024000;
            int startIndex = 0;
            int excelLength = strExcel.Length – Step;
            while (startIndex < excelLength)
            {
                ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(strExcel.Substring(startIndex, Step)));
                response.BinaryWrite(ms.ToArray());
                startIndex += Step;
            }
            response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(strExcel.Substring(startIndex, strExcel.Length – startIndex)));
            response.Flush();
            response.End();
        }

版权声明:本文为guanjie20原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/guanjie20/archive/2010/03/18/2050272.html