DataTable数据导出到Excel

最近没事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( "<script>alert('对不起,没有可用于导出的数据!')</script>");
                response.End();
        }
        response.ContentEncoding = Encoding.GetEncoding( "GB2312");
        response.ContentType = "application/ms-excel";
        response.AppendHeader( "Content-Disposition", "attachment;filename=Export.xls");
         int count = dt.Columns.Count;
        StringBuilder builder = new StringBuilder();
        builder.Append( "<html><head>\n");
        builder.Append( "<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\n");
        builder.Append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n");
        builder.Append( "</head>\n");
        builder.Append( "<table border=1>");
         if (ExcelTitle != "")
        {
                 string str = "<font size=4><b>" + ExcelTitle + "</b></font>";
                 if (strUserMsg != "")
                {
                        str = str + "(" + strUserMsg + ")";
                }
                builder.Append( string.Concat( new object[] { "<tr><td colspan=", count, ">", str, "</td></tr>" }));
        }
        builder.Append( "<tr><td colspan=" + count + " valign=middle height=24>");
        builder.Append( "查询时间:" + DateTime.Now.ToString( "G") + "</td></tr>");
        builder.Append( "<tr>\n");
         for ( int i = 0; i < count; i++)
        {
                 if (dt.Columns[i].Caption.ToString().ToLower() != "id")
                {
                        builder.Append( "<td bgcolor=#CCFFCC><b>" + dt.Columns[i].Caption.ToString() + "</b></td>\n");
                }
        }
         foreach (DataRow row in dt.Rows)
        {
                builder.Append( "<tr>");
                 for ( int j = 0; j < count; j++)
                {
                         if (dt.Columns[j].Caption.ToString().ToLower() != "id")
                        {
                                builder.Append( "<td style='vnd.ms-excel.numberformat:@'>" + row[j].ToString() + "</td>");
                        }
                }
                builder.Append( "</tr>\n");
        }
        builder.Append( "</table>\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都可

你可能感兴趣的:(职场,导出Excel,休闲)