我怎样才能读取Excel中的单元格与C#?

我想在C#中做一个excel工具。 该工具必须打开文件夹中的每个excel文档,并在单元格E1中查找值。 如果单元格保存我search的值,它将被删除,文档将被保存。 然后,应用程序将转到下一个excel文件。

我可以打开文档,但是我不能在单元格中查看值。

在这里我的代码:

using Microsoft.Office.Interop.Excel; //Preparing the required items Microsoft.Office.Interop.Excel.Application excel = null; Workbook wb = null; //Start Excel excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = false; try { //Open file wb = excel.Workbooks.Open( @"C:\Users\....", ExcelKonstanten.UpdateLinks.DontUpdate, ExcelKonstanten.ReadOnly, ExcelKonstanten.Format.Nothing, "", //Password "", //WriteResPasswort ExcelKonstanten.IgnoreReadOnlyRecommended, XlPlatform.xlWindows, "", //Separator ExcelKonstanten.Editable, ExcelKonstanten.DontNotifiy, ExcelKonstanten.Converter.Default, ExcelKonstanten.DontAddToMru, ExcelKonstanten.Local, ExcelKonstanten.CorruptLoad.NormalLoad); //Read sheets Sheets sheets = wb.Worksheets; //Select a sheet… Worksheet ws = (Worksheet)sheets.get_Item("Tabelle1"); //…or a cell Range range = (Range)ws.get_Range("E", "1"); //Read out the value string zellwert = range.Value2.ToString(); // <--- This is where I get the error! string zellwert = range.Value2; Console.WriteLine(zellwert); } catch (Exception e) { Console.WriteLine(e.Message); } finally { wb.Close(false, null, null); excel.Quit(); } Console.WriteLine("Prüfung abgeschlossen..."); 

我在这个页面上尝试同样的事情:

C#-Projekt: Zugriff auf Excel-Dateien

我应该再看看你的代码。 当你要求一个范围时,你把从哪个shell中读出。

例如:

 Range range = ( Range ) ws. get_Range ( "E1" , "E1" ) ;