最近没事Reflect了一个dll,发现数据导出到Excel的方法。写的还不错,可以在我的博客里下载Reflector
public void DataBindTitleExcel(Page pPage, DataTable dt, string ExcelTitle, string strUserMsg)
{
        HttpResponse response = pPage.Response;
         if (dt.Rows.Count == 0)
        {
                response.Write( "");
                response.End();
        }
        response.ContentEncoding = Encoding.GetEncoding( "GB2312");
        response.ContentType = "application/ms-excel";
        response.AppendHeader( "Content-Disposition", "p_w_upload;filename=Export.xls");
         int count = dt.Columns.Count;
        StringBuilder builder = new StringBuilder();
        builder.Append( "\n");
        builder.Append( "\n");
        builder.Append( "\n");
        builder.Append( "\n");
        builder.Append( "");
         if (ExcelTitle != "")
        {
                 string str = "" + ExcelTitle + "";
                 if (strUserMsg != "")
                {
                        str = str + "(" + strUserMsg + ")";
                }
                builder.Append( string.Concat( newobject[] { "" }));
        }
        builder.Append( "");
        builder.Append( "\n");
         for ( int i = 0; i < count; i++)
        {
                 if (dt.Columns[i].Caption.ToString().ToLower() != "id")
                {
                        builder.Append( "\n");
                }
        }
         foreach (DataRow row in dt.Rows)
        {
                builder.Append( "");
                 for ( int j = 0; j < count; j++)
                {
                         if (dt.Columns[j].Caption.ToString().ToLower() != "id")
                        {
                                builder.Append( "");
                        }
                }
                builder.Append( "\n");
        }
        builder.Append( "
">", str, "
" valign=middle height=24>");
        builder.Append( "查询时间:" + DateTime.Now.ToString( "G") + "
" + dt.Columns[i].Caption.ToString() + "
" + row[j].ToString() + "
\n");
        response.Write(builder.ToString());
        response.End();
}

再看看下面这个简单的,需要引入Microsoft Excel 11.0 liberary
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Utility.Util
{

public class Excelcs #region
        
public class Excelcs
        {
                 public static bool DataGridviewShowToExcel(DataTable dt, bool isShowExcle)
                {
                         if (dt.Rows.Count == 0)
                                 return false;
                         //建¡§立¢¡éExcel对?象¨®        
                        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                        excel.Application.Workbooks.Add( true);
                        excel.Visible = isShowExcle;
                         //生¦¨²成¨¦字Á?段?名?称?        
                         for ( int i = 0; i < dt.Columns.Count; i++)
                        {
                                excel.Cells[1, i + 1] = dt.Columns[i].Caption;
                        }
                         //填¬?充?数ºy据Y        
                         for ( int i = 0; i < dt.Rows.Count - 1; i++)
                        {
                                 for ( int j = 0; j < dt.Columns.Count; j++)
                                {
                                         if (dt.Rows[j][i].GetType() == typeof( string))
                                        {
                                                excel.Cells[i + 2, j + 1] = "'" + dt.Rows[j][i].ToString();
                                        }
                                         else
                                        {
                                                excel.Cells[i + 2, j + 1] = dt.Rows[j][i].ToString();
                                        }
                                }
                        }
                         return true;
                }
        }
        #endregion
}
第一种只适用于web,第二种winform和web都可