using Microsoft.Office.Interop.Excel;
針對office 2003需添加引用Microsoft Excel 11.0 Object Library
如添加之後還不能用再using microsoft.office.core和excel
引用的dll為
Interop.Microsoft.Office.Core.dll
Interop.Excel.dll
Excel匯出類:
using System;using System.IO;using System.Data;using Microsoft.Office.Interop.Excel; /// <summary> /// 匯出Excal檔案 /// </summary> public class ImportExcel{ private string title; private string rpt_name; private string outFilePath; private System.Data.DataTable dt; private System.Data.DataTable outTable; public ImportExcel() { } /// <summary> /// 設定Excel標題 /// </summary> public string rptName { get { return title; } set { title = value; } } /// <summary> /// 需要匯入EXCEL的表 /// </summary> public System.Data.DataTable RptData { get { return dt; } set { dt = value; } } /// <summary> /// 設定輸出檔案的存放位置 /// </summary> public string OutFilePath { get { return outFilePath; } set { outFilePath = value; } } /// <summary> /// 建構函式,使用該類需傳入Excel標題,DataTable,檔案的存放路徑 /// </summary> /// <param name="rptName">要設定的Excel標題</param> /// <param name="dtable">要填充Excel的表</param> /// <param name="FilePath">要存放的路徑</param> public ImportExcel(string rptName, System.Data.DataTable dtable, string file_path) { rpt_name = rptName; outTable = dtable; outFilePath = file_path; try { this.InsertExcel(); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 該函數向Excel表中插入記錄,要插入的資料為一個記憶體表.其中記憶體表中的列名為表中的列名 /// 該函數所使用的動態串連庫Excel.dll,Office.dll,VBIDE.dll. /// </summary> public void InsertExcel() { int col_count = 0; int row_count = 0; int k = 0; int l = 0; //檢查檔案是否存在 if (File.Exists(outFilePath)) { throw new Exception("檔案已存在,建立失敗!"); } Microsoft.Office.Interop.Excel.ApplicationClass rptApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); rptApp.Application.Workbooks.Add(true); rptApp.Visible = false; object missing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Workbook rptBook = rptApp.Workbooks[1]; Microsoft.Office.Interop.Excel.Worksheet rptSheet = (Microsoft.Office.Interop.Excel.Worksheet)rptBook.ActiveSheet; //設定報表的標題 rptSheet.Cells[1, 1] = rpt_name; //插入欄位標題 foreach (DataColumn col in outTable.Columns) { col_count++; rptSheet.Cells[2, col_count] = col.ColumnName.ToString(); } //設定欄位標題格式 rptSheet.get_Range(rptSheet.Cells[2, 1],rptSheet.Cells[2, col_count]).Font.Bold = true; // 是否加粗:是 //設定儲存格的格式 foreach (DataRow dr in outTable.Rows) { row_count++; col_count = 0; foreach (DataColumn col in outTable.Columns) { col_count++; if (col.DataType == System.Type.GetType("System.String")) { rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count + 2, col_count]).NumberFormatLocal = "@"; } else if (col.DataType == System.Type.GetType("System.DateTime")) { rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count + 2, col_count]).NumberFormatLocal = "yyyy-m-d"; } } } //向報表插入記錄 for (int i = 0; i < outTable.Rows.Count; i++) { for (int j = 0; j < outTable.Columns.Count; j++) { k = i + 3; l = j + 1; rptSheet.Cells[k, l] = outTable.Rows[i][j].ToString(); rptSheet.get_Range(rptSheet.Cells[1, l], rptSheet.Cells[1, l]).Borders.LineStyle = 7; rptSheet.get_Range(rptSheet.Cells[2, l], rptSheet.Cells[2, l]).Borders.LineStyle = 1; rptSheet.get_Range(rptSheet.Cells[k, l], rptSheet.Cells[k, l]).Borders.LineStyle = 1; } } //設定標題的格式 rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, 1]).Font.Bold = true; rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, 1]).Font.Size = 22; //設定報表的標題為跨列置中合併儲存格 rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, col_count]).Select(); rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, col_count]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenterAcrossSelection; //設定報表表格為最適應寬度 rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count, col_count]).Select(); rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count, col_count]).Columns.AutoFit(); //另存檔案到指定路徑下 rptBook.SaveAs(outFilePath, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); //關閉檔案 rptBook.Close(false, outFilePath, true); rptApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(rptSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(rptBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(rptApp); //強制釋放無用資源 GC.Collect(); }}
錯誤:檢索 COM 類別工廠中 CLSID 為 {00024500-0000-0000-C000-000000000046} 的組件時失敗,原因是出現以下錯誤: 80070005。
解決方案一:
控制台-》管理工具-》元件服務-》電腦-》我的電腦-》DCom配置-》找到Microsoft Excel 應用程式
之後
單擊屬性開啟此應用程式的屬性對話方塊。
2. 單擊標識選項卡,然後選擇互動式使用者。
3.單擊"安全"選項卡,分別在"啟動和啟用許可權"和"存取權限"組中選中"自訂",然後
自訂->編輯->添加ASP.NET賬戶和IUSER_電腦名稱: 需要本地啟用,和本地訪問兩個許可權
4."標識"選項卡 選擇互動式使用者