如何阅读从现有的Excel文件中的单元格值 [英] How to read cell values from existing excel file
问题描述
我想读在Excel文件中的每一个单元格的值,但是我不能够甚至在NET尝试不同的例子后得到的单元格值。我没有得到的结果用下面的代码,任何人可以取回这一点。我使用的.NET Framework 2.0
字符串文件路径=F:/BulkTest.xlsx
Microsoft.Office.Interop.Excel.Application ExcelApp =新Microsoft.Office.Interop.Excel.Application();
ExcelApp.Visible = TRUE;
Microsoft.Office.Interop.Excel.Workbook WB = ExcelApp.Workbooks.Open(文件路径,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value ,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet SH =(Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets [工作表Sheet1];
范围excelRange = sh.UsedRange;
的for(int i = 2; I< = excelRange.Count + 1;我++)
{
字符串值= sh.Cells [I,2]的ToString() ;
}
到现在为止我试图直接将单元格值的变量,现在我会尽量把单元格的值使用范围的数组。谢谢!!!! - Teja公司瓦玛13分钟前
块引用>
没有。我甚至不意味着:)我在,你可以存储在数组中的整个范围注释中。这并不意味着你需要循环,虽然每个单元将其存储在数组中。可以直接指定的范围内的值的数组。见这个例子
xlRng = xlWorkSheet.get_Range(A1,A20);
对象ARR = xlRng.Value;
的foreach(对象S(阵列)ARR)
{
MessageBox.Show(s.ToString());
}
I want to read the each cell value in excel file, But i am not able to get the cell values even after trying different examples in NET. I am not getting result with the following code, can any one get back on this. I am using .net framework 2.0
string filePath = "F:/BulkTest.xlsx"; Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ExcelApp.Visible = true; Microsoft.Office.Interop.Excel.Workbook wb = ExcelApp.Workbooks.Open(filePath, Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value); Microsoft.Office.Interop.Excel.Worksheet sh = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets["Sheet1"]; Range excelRange = sh.UsedRange; for (int i=2; i<= excelRange.Count + 1 ; i++) { string values = sh.Cells[i,2].ToString(); }
解决方案Till now i am trying to take cell values directly to variables, now i will try to take cell values to an array using Range. Thanks!!!! – Teja Varma 13 mins ago
No. I didn't even mean that :) As I mentioned in the comment that you can store the entire range in an array. That doesn't mean that you need to loop though each cell to store it in an array. You can directly assign the values of the range to the array. See this example.
xlRng = xlWorkSheet.get_Range("A1", "A20"); Object arr = xlRng.Value; foreach (object s in (Array)arr) { MessageBox.Show(s.ToString()); }
这篇关于如何阅读从现有的Excel文件中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!