本人菜鸟,哪位大神在,求帮忙.net里做一个导出excel的操作,在线等。可谈报酬。

2025-06-28 22:41:45
推荐回答(1个)
回答1:

        ///  
        /// 导出Excel文件,自动返回可下载的文件流 
        ///  
        public static void DataTable1Excel(System.Data.DataTable dtData)
        {
            GridView gvExport = null;
            HttpContext curContext = HttpContext.Current;
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;
            if (dtData != null)
            {
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                curContext.Response.Charset = "utf-8";
                strWriter = new StringWriter();
                htmlWriter = new HtmlTextWriter(strWriter);
                gvExport = new GridView();
                gvExport.DataSource = dtData.DefaultView;
                gvExport.AllowPaging = false;
                gvExport.DataBind();
                gvExport.RenderControl(htmlWriter);
                curContext.Response.Write("" + strWriter.ToString());
                curContext.Response.End();
            }
        }
        //http://tool.sufeinet.com/CodePreview/CodeView.aspx?action=view&file=Excel/ExcelHelper.cs