這是我在Office 2016安裝之前使用的代碼。Microsoft.Office.Interop.Excel初始化失敗(Office 2016)
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(finfo.FullName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
var cellValue =(excelWorksheet.Cells[a, b] as Excel.Range).Value;
的代碼能正常工作,但辦公室2016安裝後,我已經看到了,我不能使用 using Excel = Microsoft.Office.Interop.Excel;
,我發現的唯一的兼容庫
Microsoft.Office.Core (Microsoft Office 16.0 Object Library)
我便無法找到任何訪問Excel文件的例子。是否有任何其他方式來訪問Excel文件與Office 2016安裝(PIA)
這是我得到的錯誤,當我嘗試運行代碼
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).
試試這個:https://stackoverflow.com/questions/5070124/why-doesnt-the-office-pia-install-correctly-to-the-gac –
這是不是很明顯這是什麼與「初始化」。過去7年PIA [已過時](https://stackoverflow.com/questions/21013912/can-i-still-use-microsoft-office-interop-assemblies-with-office-2013),你會必須停止使用它們。如果它確實是「初始化」,程序將無法啓動,那麼請考慮您可能安裝了64位版本的Office。所以你的程序也需要以64位模式運行。 –
當我說初始化我的意思是Excel對象的實例來訪問Excel文件。我將Office更改爲2016年,我認爲PIA是我的代碼不起作用的主要問題(我猜)。我應該怎樣做才能訪問Excel文件? – kostas