无法将单元格中的数据分配给variables

我正在写代码,从一个Excel文件中获取数据,并将其放入另一个。 我这样做的想法是将数据从开始的工作簿写入一个variables,然后使用该variables将数据写入结束工作簿。 这是我的代码:

sApp = new Excel.Application sApp.Visible = false; sBook = sApp.Workbooks.Open(SfilePath.ToString()); sSheet = (Excel.Worksheet)sBook.Sheets[1]; 

这只是进入工作簿的介绍代码,下面是我遇到的问题:

 formNumber = (string)(sSheet.Cells["J2"] as Excel.Range).Value2.ToString(); 

当我运行我的程序,我得到一个错误,说

ArgumentException是未处理的。 在mscorlib.dll中发生未处理的“System.ArgumentException”typesexception。 附加信息:参数不正确。 (从HRESULTexception:0x80070057(E_INVALIDARG))

当我研究这个错误代码(HRESULT:0x80070057)时,我发现其他人也有同样的问题。 然而,所有的解决scheme都没有为我工作(即清理箱,清除临时文件)。

有没有任何人可以给我的见解? 如果您认为您可能需要了解更多关于代码的信息,请告诉我。

谢谢。

  Try this.. Excel.Range workSheetUsedRange = sSheet.UsedRange; string formNumber = string.Empty; if ((workSheetUsedRange.Cells[rowIndex, columnIndex] as Excel.Range).Value2 != null) formNumber = (workSheetUsedRange.Cells[rowIndex, columnIndex] as Excel.Range).Value2.ToString(); excelUtil.SaveWorkBook(file, ref workbook); You can use this code to get the value of any cell using rowIndex and columnIndex. It is a good practise to check if the value of the cell that you are trying to read is null. If the value is null, you get an exception. 

最后一行是保存工作簿。 看起来我用我的实用程序库粘贴了一个代码。 您可以使用代码进行保存。 随着你提到的错误代码,你收到的错误信息是什么? 你能否提供可以帮助理解问题/错误的其他细节?

我发现我的错误,我试图用他们的名字“J2”,“K2”来调用单元格。 但是你不能这样做,它必须靠点位置。 感谢所有提交答案的人,我很感激。