C#导出EXCEL(DataTable导出EXCEL)

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.IO; using System.Web; using Microsoft.Office.Interop.Excel; using System.Reflection;

/*  * 开发人员:Hisen  * 时间:2008年11月24日  * 功能:将数据导出Excel  *  */ namespace XT.LiTree.Logic {     public class ExcelExport     {         private ExcelExport()         { }         private static ExcelExport _instance = null;

public static ExcelExport Instance         {             get             {                 if (_instance == null) _instance = new ExcelExport();                 return _instance;             }         }

/// <summary>         /// DataTable直接导出Excel,此方法会把DataTable的数据用Excel打开,再自己手动去保存到确切的位置         /// </summary>         /// <param name="dt">要导出Excel的DataTable</param>         /// <returns></returns>         public bool DoExport(DataTable dt)         {             Application app = new ApplicationClass();             if (app == null)             {                 throw new Exception("Excel无法启动");             }             app.Visible = true;             Workbooks wbs = app.Workbooks;             Workbook wb = wbs.Add(Missing.Value);             Worksheet ws = (Worksheet)wb.Worksheets[1];

int cnt = dt.Rows.Count;             int columncnt = dt.Columns.Count;

// *****************获取数据********************             object[,] objData = new Object[cnt + 1, columncnt];  // 创建缓存数据             // 获取列标题             for (int i = 0; i < columncnt; i++)             {                 objData[0, i] = dt.Columns[i].ColumnName;             }             // 获取具体数据             for (int i = 0; i < cnt; i++)             {                 System.Data.DataRow dr = dt.Rows[i];                 for (int j = 0; j < columncnt; j++)                 {                     objData[i + 1, j] = dr[j];                 }             }

//********************* 写入Excel******************             Range r = ws.get_Range(app.Cells[1, 1], app.Cells[cnt + 1, columncnt]);             r.NumberFormat = "@";             //r = r.get_Resize(cnt+1, columncnt);             r.Value2 = objData;             r.EntireColumn.AutoFit();

app = null;             return true;         }

/// <summary>         /// DataTable通过流导出Excel         /// </summary>         /// <param name="ds">数据源DataSet</param>         /// <param name="columns">DataTable中列对应的列名(可以是中文),若为null则取DataTable中的字段名</param>         /// <param name="fileName">保存文件名(例如:a.xls)</param>         /// <returns></returns>         public bool StreamExport(DataTable dt, string[] columns, string fileName, System.Web.UI.Page pages)         {             if (dt.Rows.Count > 65535) //总行数大于Excel的行数             {                 throw new Exception("预导出的数据总行数大于excel的行数");             }             if (string.IsNullOrEmpty(fileName)) return false;                         StringBuilder content = new StringBuilder();             StringBuilder strtitle = new StringBuilder();             content.Append("<html xmlns:o=‘urn:schemas-microsoft-com:office:office‘ xmlns:x=‘urn:schemas-microsoft-com:office:excel‘ xmlns=‘http://www.w3.org/TR/REC-html40‘>");             content.Append("<head><title></title><meta http-equiv=‘Content-Type‘ content=\"text/html; charset=gb2312\">");             //注意:[if gte mso 9]到[endif]之间的代码,用于显示Excel的网格线,若不想显示Excel的网格线,可以去掉此代码             content.Append("<!--[if gte mso 9]>");             content.Append("<xml>");             content.Append(" <x:ExcelWorkbook>");             content.Append("  <x:ExcelWorksheets>");             content.Append("   <x:ExcelWorksheet>");             content.Append("    <x:Name>Sheet1</x:Name>");             content.Append("    <x:WorksheetOptions>");             content.Append("      <x:Print>");             content.Append("       <x:ValidPrinterInfo />");             content.Append("      </x:Print>");             content.Append("    </x:WorksheetOptions>");             content.Append("   </x:ExcelWorksheet>");             content.Append("  </x:ExcelWorksheets>");             content.Append("</x:ExcelWorkbook>");             content.Append("</xml>");             content.Append("<![endif]-->");             content.Append("</head><body><table style=‘border-collapse:collapse;table-layout:fixed;‘><tr>");

if (columns != null)             {                 for (int i = 0; i < columns.Length; i++)                 {                     if (columns[i] != null && columns[i] != "")                     {                         content.Append("<td><b>" + columns[i] + "</b></td>");                     }                     else                     {                         content.Append("<td><b>" + dt.Columns[i].ColumnName + "</b></td>");                     }                 }             }             else             {                 for (int j = 0; j < dt.Columns.Count; j++)                 {                     content.Append("<td><b>" + dt.Columns[j].ColumnName + "</b></td>");                 }             }             content.Append("</tr>\n");

for (int j = 0; j < dt.Rows.Count; j++)             {                 content.Append("<tr>");                 for (int k = 0; k < dt.Columns.Count; k++)                 {                     object obj = dt.Rows[j][k];                     Type type = obj.GetType();                     if (type.Name == "Int32" || type.Name == "Single" || type.Name == "Double" || type.Name == "Decimal")                     {                         double d = obj == DBNull.Value ? 0.0d : Convert.ToDouble(obj);                         if (type.Name == "Int32" || (d - Math.Truncate(d) == 0))                             content.AppendFormat("<td style=‘vnd.ms-excel.numberformat:#,##0‘>{0}</td>", obj);                         else                             content.AppendFormat("<td style=‘vnd.ms-excel.numberformat:#,##0.00‘>{0}</td>", obj);                     }                     else                         content.AppendFormat("<td style=‘vnd.ms-excel.numberformat:@‘>{0}</td>", obj);                 }                 content.Append("</tr>\n");             }             content.Append("</table></body></html>");             content.Replace("&nbsp;", "");             pages.Response.Clear();             pages.Response.Buffer = true;             pages.Response.ContentType = "application/ms-excel";  //"application/ms-excel";             pages.Response.Charset = "UTF-8";             pages.Response.ContentEncoding = System.Text.Encoding.UTF7;             fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);             pages.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);             pages.Response.Write(content.ToString());             //pages.Response.End();  //注意,若使用此代码结束响应可能会出现“由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。”的异常。             HttpContext.Current.ApplicationInstance.CompleteRequest(); //用此行代码代替上一行代码,则不会出现上面所说的异常。             return true;         }

/// <summary>         /// 直接由GridView导出Excel         /// </summary>         /// <param name="ctl">控件(一般是GridView)</param>         /// <param name="FileName">导出的文件名</param>         /// <param name="removeIndexs">要移除的列的索引数组(因为有时我们并不希望把GridView中的所有列全部导出)</param>         /// <param name="pages"></param>         public void ControlToExcel(System.Web.UI.WebControls.GridView ctl, string FileName, string[] removeIndexs, System.Web.UI.Page pages)         {             if (removeIndexs != null)             {                 foreach (string index in removeIndexs)                 {                     ctl.Columns[int.Parse(index)].Visible = false;                 }             }             pages.Response.Charset = "UTF-8";             pages.Response.ContentEncoding = System.Text.Encoding.UTF7;             pages.Response.ContentType = "application/ms-excel";             FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);             pages.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);             ctl.Page.EnableViewState = false;             System.IO.StringWriter tw = new System.IO.StringWriter();             System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);             ctl.RenderControl(hw);             pages.Response.Write(tw.ToString());             HttpContext.Current.ApplicationInstance.CompleteRequest();         }     } }

时间: 2024-11-05 10:46:47

C#导出EXCEL(DataTable导出EXCEL)的相关文章

DataTable导出到Excel

static DataTable GetTable() { // // Here we create a DataTable with four columns. // DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patien

C# Datatable导出Excel方法

C# 导出Excel方法  先引用下System.IO;System.data; 具体函数如下: 1 public static bool ExportCSV(DataTable dt, string fileNmae) 2 { 3 bool Msg = false; 4 string con = ""; 5 foreach (DataColumn dc in dt.Columns) 6 { 7 con += dc.ColumnName + ","; 8 } 9 c

将DataTable导出为Excel文件的方法

 需求:前台点击某个按钮,在后台从数据库中获取某DataTable数据到处成Excel文件. 1.Asp按钮控件 两个按钮,分别调用两种导出Excel文件的后台方法. <%--第一种方法--%> <form id="form1" runat="server"> <div> <asp:Button runat="server" OnClick="Btn1_Click" ID="

DataTable ---导出Excel

//mscorlib.dll, v4.0.0.0&&Microsoft.Office.Interop.Excel.dll, v14.0.0.0 /// <summary> /// 导出Excel /// </summary> /// <param name="dt_data">要导出的数据表</param> public void ExcelPort(DataTable dt_data) { try { //表格控件对象(

C# 将内存中的datatable数据导出为Excel(方法二,创建Excel对象导出)【转载】

上次写了一个用文件流方式将Datatable导出Excel的方法,这个方法有局限性,比如没法对Excel进行一些增加列颜色等简单的操作,现在,给大家介绍另外一种方法,用微软的Excel类.既然要用到类,那必须是你的机子要装上Excel才行呢. public  void DataTabletoExcel(System.Data.DataTable[] tmpDataTable,string date1,string date2) { string saveFileName = ""; S

把DataTable导出为Excel文件

今天项目里遇到了需要把DataTable导出为Excel的问题将解决方案记录在园子备忘 需要一些东西第三方的类库Aspose.Cells此处随意找个下载链接:http://www.cr173.com/soft/66096.html本人不保证该链接的合法性和有效性 说重点,上Demo public bool ExportAsExcel(string URI, DataSet ds) { Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();

【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进阶,除了上一篇提到的处理乱码问题,本例还添加了处理多行表头.合并单元格的功能及处理中文文件名乱码问题,应该可以满足日常开发的需要了. 废话不多说了,直接上代码: [C#] 可以写单独类 1 using System; 2 using System.Collections.Generic; 3 usi

asp.net DataTable导出 excel的方法记录(第三方)

官网:http://npoi.codeplex.com/ 简单应用,主要是可以实现我们想要的简单效果,呵呵 需要引入dll,可以在官网下载,也可在下面下载 C#代码   protected void getExcel(DataTable dt) { NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.Sheet sheet = book.CreateSh

DataTable导出为Excel

/// <summary> /// 导出Excel Datatable版本 /// </summary> /// <param name="dt">导出的Datatable</param> /// <param name="ExcelName">导出EXCEL的名称 不需要要带有扩展名_xls</param> public static void NewExportExcelScore(Data