c# - 如何从现有的 excel 文件中读取单元格值

标签 c# excel

我想读取 excel 文件中的每个单元格值,但即使在 NET 中尝试了不同的示例后我也无法获取单元格值。我没有得到以下代码的结果,任何人都可以回来吗?我正在使用 .net 框架 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

没有。我什至不是那个意思 :) 正如我在评论中提到的,您可以将整个范围存储在一个数组中。这并不意味着您需要遍历每个单元格以将其存储在数组中。您可以直接将范围的值分配给数组。请参阅此示例。

xlRng = xlWorkSheet.get_Range("A1", "A20");

Object arr = xlRng.Value;

foreach (object s in (Array)arr)
{
    MessageBox.Show(s.ToString());
}

关于c# - 如何从现有的 excel 文件中读取单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213255/

相关文章:

excel - 尝试在 Excel 中使用 VBA 从网页中提取一个值

c# - 如何使用 [Display(Name ="")] 作为 LoadFromCollection 的列标题

c# - System.Drawing.Image 和 System.Drawing.Bitmap 有什么区别?

c# - 在 C# 中使用#define 的真实示例?

excel - 从单元格字符串中删除 HTML 标签 : excel Formula

excel - 在 Excel 中将 5 列合并为一列

excel - 计算 Power Pivot 模型中总计的百分比

c# 发送数据到 php url

c# - 字典中的 “An item with the same key has already been added”

vba - 复制公式而不更改单元格引用