如何读取单个Excel单元格值 [英] How to read single Excel cell value

查看:177
本文介绍了如何读取单个Excel单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Excel与具有价值,我需要第2行和列10
读到这里是我的工作表Sheet1的代码文件。

I have excel file with sheet1 that has a value I need to read on row 2 and column 10. Here is my code.

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
var cell = (Excel.Range)excelWorksheet.Cells[10, 2];



获取单元格对象,它是Excel.Range后,我不知道如何读的是内容细胞。我试图将其转换成数组,遍历它,我尝试转换为字符串数组等等。我相信这是很简单的。有没有一种直接的方式来获得只有一个单元格的值是字符串?

After getting cell object which is Excel.Range, I do not know how to read the content of that cell. I tried converting it into array and looping over it and I tried converting to string array etc. I am sure it is very simple. Is there a direct way to get just one cell value that is string?

推荐答案

您需要将其转换为一个字符串(不字符串数组),因为它是一个单一的值。

You need to cast it to a string (not an array of string) since it's a single value.

var cellValue = (string)(excelWorksheet.Cells[10, 2] as Excel.Range).Value;

这篇关于如何读取单个Excel单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆