刚才路过个狠人的BLOG,看到他有应用,就给你转过来了,希望对你有帮助!
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace DY.BLL
{
 /// <summary>
 /// BLL_Excel 的摘要说明。
 /// </summary>
 public class BLL_Excel
 {  public BLL_Excel(){}     #region 导出Exce
  /// <summary>
  /// 导出Excel
  /// </summary>
  /// <param name="dr">OleDbDataReader 数据源</param>
  /// <param name="FileName">文件名</param>
  /// <param name="biaotou">表头</param>
  public void EduceExcel(OleDbDataReader dr,string FileName,string[] biaotou) 
  { 
   HttpContext.Current.Response.Clear(); 
   HttpContext.Current.Response.Buffer= true;//设置缓冲输出 
   HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\""); 
   HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
   HttpContext.Current.Response.ContentType = "application/ms-";     
   //   _page.EnableViewState = false;//是否保持视图状态    
   HttpContext.Current.Response.Write( HTML(dr,biaotou) );
   
   HttpContext.Current.Response.End ();
  }
  /// <summary>
  /// 导出Excel
  /// </summary>
  /// <param name="_page">this</param>
  /// <param name="DB">DataGrid控件名称</param>
  /// <param name="FileName">要导出的文件名</param>
  public void EduceExcel(Page _page, DataGrid DB,string FileName) 
  { 
   HttpContext.Current.Response.Clear(); 
   HttpContext.Current.Response.Buffer= true;//设置缓冲输出 
   HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\""); 
   HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
   HttpContext.Current.Response.ContentType = "application/ms-";     
   _page.EnableViewState = false;//是否保持视图状态   System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); //将信息写入字符串
   System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); //在WEB窗体上写出一系列的HTML特定字符和文本
         
   DB.RenderControl (oHtmlTextWriter);
   HttpContext.Current.Response.Write(oStringWriter.ToString ());
   
   HttpContext.Current.Response.End ();
  }
  /// <summary>
  /// 导出Excel
  /// </summary>
  /// <param name="ds">DataSet 数据源</param>
  /// <param name="FileName">文件名</param>
  /// <param name="biaotou">表头</param>
  public void EduceExcel(DataSet ds,string FileName,string[] biaotou) 
  { 
   HttpContext.Current.Response.Clear(); 
   HttpContext.Current.Response.Buffer= true;//设置缓冲输出 
   HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\""); 
   HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
   HttpContext.Current.Response.ContentType = "application/ms-";     
   //   _page.EnableViewState = false;//是否保持视图状态    
   HttpContext.Current.Response.Write( HTML(ds,biaotou) );
   
   HttpContext.Current.Response.End ();
  }
  
  /// <summary>
  /// 导出Excel
  /// </summary>
  /// <param name="ds">DataSet 数据源</param>
  /// <param name="FileName">文件名</param>
  /// <param name="biaotou">表头</param>
  public void EduceExcel(DataTable dt,string FileName,string[] biaotou) 
  { 
   HttpContext.Current.Response.Clear(); 
   HttpContext.Current.Response.Buffer= true;//设置缓冲输出 
   HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\""); 
   HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
   HttpContext.Current.Response.ContentType = "application/ms-";     
   //   _page.EnableViewState = false;//是否保持视图状态    
   HttpContext.Current.Response.Write( HTML(dt,biaotou) );
   
   HttpContext.Current.Response.End ();
  }
  #endregion  #region 构造HTML
  /// <summary>
  /// 使用DataSet蔉据源
  /// </summary>
  /// <param name="ds"></param>
  /// <param name="biaotou"></param>
  /// <returns></returns>
  private string HTML (DataSet ds,string[] biaotou)
  {
   StringBuilder ss = new StringBuilder();
   ss.Append("<table>");
   ss.Append("<tr>");
    ss.Append(" <td>序号</td>");
   foreach(string str in biaotou)
   {
    ss.Append(" <td>&nbsp;"+ str +"</td>");
   }
   ss.Append("</tr>");
   int ii=1;
   foreach (DataRow dr in ds.Tables[0].Rows)
   {
    ss.Append("<tr>");
    ss.Append(" <td>&nbsp;"+ (ii++).ToString() +"</td>");
    int I = dr.Table.Columns.Count;
    for (int i=0;i<I;i++)
    {
     ss.Append(" <td>&nbsp;"+ dr[i].ToString() +"</td>");
    }    ss.Append("</tr>");
   }
   ss.Append ("</table>");   return ss.ToString();
  }
  /// <summary>
  /// 使用OleDbDataReader 数据源
  /// </summary>
  /// <param name="dr"></param>
  /// <param name="biaotou"></param>
  /// <returns></returns>
  private string HTML (OleDbDataReader dr,string[] biaotou)
  {
   StringBuilder ss = new StringBuilder();
   ss.Append("<table>");
   
   ss.Append("<tr>");
   ss.Append(" <td>序号</td>");
   foreach(string str in biaotou)
   {
    ss.Append(" <td>&nbsp;"+ str +"</td>");
   }
   ss.Append("</tr>");   int ii=1;
   while( dr.Read() )
   {
    ss.Append("<tr>");
    ss.Append(" <td>&nbsp;"+ (ii++).ToString() +"</td>");
    int I = dr.FieldCount;
    for (int i=0;i<I;i++)
    {
     ss.Append(" <td>&nbsp;"+ dr[i].ToString() +"</td>");
    }    ss.Append("</tr>");
   }
   ss.Append ("</table>");
   dr.Close();
   return ss.ToString();
  }  /// <summary>
  /// 使用DataTable数据源
  /// </summary>
  /// <param name="dt"></param>
  /// <param name="biaotou"></param>
  /// <returns></returns>
  private string HTML (DataTable dt,string[] biaotou)
  {
   StringBuilder ss = new StringBuilder();
   ss.Append("<table>");
   
   ss.Append("<tr>");
   ss.Append(" <td>序号</td>");
   foreach(string str in biaotou)
   {
    ss.Append(" <td>&nbsp;"+ str +"</td>");
   }
   ss.Append("</tr>");   int ii=dt.Rows.Count;
   foreach (DataRow dr in dt.Rows)
   {
    ss.Append("<tr>");
    ss.Append(" <td>&nbsp;"+ (ii++).ToString() +"</td>");
    int I = dr.Table.Columns.Count;
    for (int i=0;i<I;i++)
    {
     ss.Append(" <td>&nbsp;"+ dr[i].ToString() +"</td>");
    }    ss.Append("</tr>");
   }
   ss.Append ("</table>");   
   return ss.ToString();
  }
  #endregion
 }

解决方案 »

  1.   

    能否给我他的blog地址呢,谢谢了。
      

  2.   

    我现在在项目开发过程中,所采用的一般都是程序实现的,即在程序中使用dataTable,Repeater等来控制数据的显示什么的,没有用过datagrid,所以不知道一般这种项目里是否都需要datagrid等控件,是否采用了这些控件做相关的功能,是否就是很简单了。
      

  3.   

    他的BLOG上就写了这么多。我刚才看了一样就给关了。。
    而且没有相关联的文章,就这一篇^_^
    等我要是找到了,我就再给你发上来OK? 
      

  4.   

    貌似找到了
    http://blog.csdn.net/winner2050
      

  5.   

    我突然想起来了,我所需要的效果不是把数据库内的数据转化为excel的内容,并保存,因为我看上面的代码需要提供一个文件的名称,但是我所需要的效果,就是导出的时候,需要打开excel软件,然后所有的数据内容会导入到excel表中,然后用户可以打印这个excel表格,至于有些用户需要继续使用这个excel文件的话,那么就再进一步保存这个excel文件,,这里还要涉及到所谓的把excel表格能够提供打印的功能,这个是否会涉及到另外的控件,麻烦高手高知,谢谢。
      

  6.   

    而且,产生的相应的表格还会带有相应的表格边框,请问这些东西是怎么导入到excel表里的,是不是就是报表?还是,请达人指点一二。
      

  7.   

    看来我们遇到了差不多的问题啊
    我也是用repeater显示数据的
    想请教下,我导出的数据没有网格线,改怎样让显示的数据有网格线,
    最上面的那位贴出来的应该就是吧。
      

  8.   

    我特别想知道,为啥用DataGrid控件绑定的,没有tiaotou 的参数
    用DataGrid控件绑定的导出excel后也没有网格线啊,
    请帮忙看看