在C#WPF应用程序中打开并读取Excel文件

我正在尝试打开位于本地计算机上的Excel文件,并从中取出一些值。 这些值是位于不同单元格中的string和数字的组合。 Excel文件将使用openFileDialog 。 我正在使用C#和开发一个桌面应用程序(WPF)。 这是我正在执行的代码:

  Excel.Application excelApp = new Excel.Application(); string workbookPath = "C:/Users/b_prashanth/Documents/26. Workload Modeling generic template.xlsx"; Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath); Excel.Sheets excelSheets = excelWorkbook.Worksheets; string currentSheet = "Sheet1"; Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet); Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1"); 

这样的事情呢? 你遇到了什么问题?

 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = ".xlsx"; bool? result = dlg.ShowDialog(); if (result != true) return; string workbookPath = dlg.FileName; Excel.Application excelApp = new Excel.Application(); Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath); Excel.Worksheet sheet = excelWorkbook.Sheets["Sheet1"] as Excel.Worksheet; if (sheet == null) return; Excel.Range range = sheet.get_Range("A1", Missing.Value) var yourValue = range.Text; 

我认为,OpenXML SDK应该可以帮到你。
http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK
它可以通过nuget – DocumentFormat.OpenXML获得, 在这里你可以find所有的类和algorithm,你需要什么

而不是Office interop,它需要在计算机上安装Office,则可以使用ADO驱动程序像访问数据库一样访问Excel工作表。

这已经logging在这里很多次,会很容易。