using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Data; using System.Data.OleDb; //using Microsoft.Office.Interop.Excel; using Ccdodo.Common; namespace Ccdodo.Shop.ExcelFactory { public class ExcelSource { #region 导出到excel表格 public static void outToExcel(System.Data.DataTable dt) { // #region XXX System.Web.UI.WebControls.GridView gv = new System.Web.UI.WebControls.GridView(); gv.AllowPaging = false; gv.DataSource = dt; gv.DataBind(); //创建会话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; //IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; if (gv.Rows.Count > 0) { curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.UTF8; //curContext.Response.Charset = "gb2312"; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyMMddhhmmss") + ".xls"); //导出Excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // gv.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); } #endregion } #endregion // } } //这个方法导出来的excel并不完整,所以如果导出之后还想导回去,建议不要考虑这个方法