1.未在本地計算機上註冊「microsoft.ACE.oledb.12.0」提供程序。windows
2.可能出現解析Excel時致使服務「Office Software Protection Platform」中止,不能進行excel的繼續解析致使程序假死;ui
a) 服務「Office Software Protection Platform」設置權限以下this
b) 設置服務的恢復情況spa
3.轉換excel3d
private void ConvertExcel(string savePath) { //將xml文件轉換爲標準的Excel格式 Object Nothing = Missing.Value;//因爲yongCOM組件不少值須要用Missing.Value代替 Excel.Application ExclApp = new Excel.ApplicationClass();// 初始化 Excel.Workbook ExclDoc = null; try { ExclDoc = ExclApp.Workbooks.Open(savePath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);//打開Excl工做薄 Object format = Excel.XlFileFormat.xlWorkbookNormal;//獲取Excl 2007文件格式 //Excel.XlFileFormat.xlWorkbookDefault; ExclApp.DisplayAlerts = false; ExclDoc.SaveAs(savePath, format, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);//保存爲Excl 2007格式 //ExclDoc.SaveAs(savePath, format, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);//保存爲Excl 2007格式 } catch (Exception ex) { LogEntry.TraceError("ConvertExcel", ex); } finally { if (ExclDoc != null) { ExclDoc.Close(Nothing, Nothing, Nothing); } ExclApp.Quit(); this.KillExcel(); } }
4.OLEDB讀取excel數據excel
private DataTable GetExcelData(string filePath) { DataTable result = null; //string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1'", filePath);//Excel 8.0;HDR=YES;IMEX=1;//Excel 97-2003 //string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES';", filePath);//Excel 2007-2013 string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=1';", filePath);//Excel 2007-2013 //this.KillExcel(); using (OleDbConnection oleDbConn = new OleDbConnection(strConn)) { try { oleDbConn.Open(); DataTable schemaTable = oleDbConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); string tableName = ""; if (schemaTable != null && schemaTable.Rows.Count > 0) { tableName = schemaTable.Rows[0][2].ToString().Trim();//獲取到第一個Sheet1名稱做爲表名 string strExcel = ""; OleDbDataAdapter oleDbAdp = null; DataSet ds = new DataSet(); strExcel = "select * from [" + tableName + "]"; oleDbAdp = new OleDbDataAdapter(strExcel, strConn); oleDbAdp.Fill(ds, tableName); if (ds != null && ds.Tables.Count > 0) { result = ds.Tables[tableName]; } } else { LogEntry.TraceError("Excel文件讀取錯誤,內容爲空,文件路徑:" + filePath, null); } } catch (Exception ex) { LogEntry.TraceError("讀取Excel異常", ex); this.ConvertExcel(filePath);//出現不能讀取時,轉換下文件,下次能夠讀取 } finally { oleDbConn.Close(); } } return result; }
3.當程序的宿主爲windows服務時code
讀取一個excel後可能致使不能讀取下一個excel文件,問題緣由暫時懷疑權限阻止致使,暫時沒解決;orm