System.InvalidOperationException: 未在本地計算機上注冊“Microsoft.ACE.OLEDB.12.0”提供程序。


   最近用MVC3 做了一個項目,發布時項目的中的數據導入功能(Excel格式,有固定的導入數據模板)居然不能用,查看報錯日志,發現是“System.InvalidOperationException: 未在本地計算機上注冊“Microsoft.ACE.OLEDB.12.0”提供程序............”。 在網上找一些資料,把問題解決了。如下圖所示。

 

 

     

    后台功能代碼:導入與導出實現代碼。

  1    #region -使用IO寫入Excel-
  2         /// <summary>
  3         ///  使用IO寫入Excel
  4         /// </summary>
  5         /// <param name="table"></param>
  6         /// <param name="file">保存路徑</param>
  7         public static bool dataTableToCsv(System.Data.DataTable table, string file)
  8         {
  9             string title = "";
 10             try
 11             {
 12                 FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
 13 
 14                 //FileStream fs1 = File.Open(file, FileMode.Open, FileAccess.Read);
 15 
 16                 StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);
 17 
 18                 for (int i = 0; i < table.Columns.Count; i++)
 19                 {
 20 
 21                     title += table.Columns[i].ColumnName + "\t"; //欄位:自動跳到下一單元格
 22 
 23                 }
 24 
 25                 title = title.Substring(0, title.Length - 1) + "\n";
 26 
 27                 sw.Write(title);
 28 
 29                 foreach (System.Data.DataRow row in table.Rows)
 30                 {
 31                     string line = "";
 32                     for (int i = 0; i < table.Columns.Count; i++)
 33                     {
 34                         line += row[i].ToString().Trim() + "\t"; //內容:自動跳到下一單元格
 35                     }
 36                     line = line.Substring(0, line.Length - 1) + "\n";
 37                     sw.Write(line);
 38                 }
 39                 sw.Close();
 40                 fs.Close();
 41                 return true;
 42 
 43             }
 44             catch (Exception)
 45             {
 46                 return false;
 47             }
 48         }
 49 
 50         #endregion
 51 
 52         #region- 讀取csv格式的Excel文件的方法-
 53         ///<summary>
 54         ///讀取csv格式的Excel文件的方法 
 55         ///</ummary>
 56         ///<param name="path">待讀取Excel的全路徑</param>
 57         ///<returns></returns>
 58         public static DataTable ReadExcelWithStream(string path)
 59         {
 60             DataTable dt = new DataTable();
 61             bool isDtHasColumn = false; //標記DataTable 是否已經生成了列
 62             StreamReader reader = new StreamReader(path, System.Text.Encoding.Default); //數據流
 63             while (!reader.EndOfStream)
 64             {
 65                 string meaage = reader.ReadLine();
 66                 string[] splitResult = meaage.Split(new char[] { ',' }, StringSplitOptions.None); //讀取一行 以逗號分隔 存入數組
 67                 DataRow row = dt.NewRow();
 68                 for (int i = 0; i < splitResult.Length; i++)
 69                 {
 70                     if (!isDtHasColumn) //如果還沒有生成列
 71                     {
 72                         dt.Columns.Add("column" + i, typeof(string));
 73                     }
 74                     row[i] = splitResult[i];
 75                 }
 76                 dt.Rows.Add(row); //添加行
 77                 isDtHasColumn = true; //讀取第一行后 就標記已經存在列 再讀取以后的行時,就不再生成列
 78             }
 79             return dt;
 80         }
 81         #endregion
 82 
 83         #region -讀取xls\xlsx格式的Excel文件的方法-
 84         ///<summary>
 85         ///讀取xls\xlsx格式的Excel文件的方法 
 86         ///</ummary>
 87         ///<param name="path">待讀取Excel的全路徑</param>
 88         ///<returns></returns>
 89         public static DataTable ReadExcelToTable(string path)
 90         {
 91             DataSet ds = new DataSet();
 92             //如果HDR=YES,DataTable默認的列名為Excel 第一行數據...
 93             //如果HDR=NO,DataTable默認的列名為F1,F2,F3...
 94             string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"; // Office 07及以上版本 不能出現多余的空格 而且分號注意
 95             using (OleDbConnection conn = new OleDbConnection(strConn))
 96             {
 97                 conn.Open();
 98                 DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
 99                 string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一個sheet的名字
100                 string strExcel = string.Format("select * from [{0}]", firstSheetName);
101                 OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
102                 adapter.Fill(ds);
103                 conn.Close();
104             }
105             return ds.Tables[0];
106         }
107         #endregion

   發布時,服務器的PC 一定安裝 Office 07 以上版本。不是導入數據會失敗!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM