.net 导出Excel报表

来源:互联网 发布:淘宝网手机app下载 编辑:程序博客网 时间:2024/03/29 04:13

        /// <summary>
        /// 将DataTable导出到Excel表
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="FileName"></param>
        public void ToExcel(DataTable dt, string FileName)
        {
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
            HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
            int i = 0, j;
            for (i = 0; i < dt.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write(dt.Columns[i].ToString() + Constants.vbTab);
            }
            HttpContext.Current.Response.Write(Constants.vbCrLf);
            foreach (DataRow dr in dt.Rows)
            {
                for (j = 0; j < dr.ItemArray.Length; j++)
                    HttpContext.Current.Response.Write(dr.ItemArray[j].ToString() + Constants.vbTab);
                HttpContext.Current.Response.Write(Constants.vbCrLf);
            }
            HttpContext.Current.Response.End();
        }

原创粉丝点击