C# 如何读取单个Excel单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18993735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read single Excel cell value
提问by DoodleKana
I have excel file with sheet1 that has a value I need to read on row 2 and column 10. Here is my code.
我有一个带有 sheet1 的 excel 文件,它有一个我需要在第 2 行和第 10 列上读取的值。这是我的代码。
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];
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?
获取 Excel.Range 单元格对象后,我不知道如何读取该单元格的内容。我尝试将其转换为数组并对其进行循环,然后尝试转换为字符串数组等。我确信它非常简单。有没有一种直接的方法可以只获取一个字符串的单元格值?
采纳答案by System Down
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;
回答by MadPointer
//THIS IS WORKING CODE
Microsoft.Office.Interop.Excel.Range Range_Number,r2;
Range_Number = wsheet.UsedRange.Find("smth");
string f_number="";
r2 = wsheet.Cells;
int n_c = Range_Number.Column;
int n_r = Range_Number.Row;
var number = ((Range)r2[n_r + 1, n_c]).Value;
f_number = (string)number;
回答by David Sopko
using Microsoft.Office.Interop.Excel;
string path = "C:\Projects\ExcelSingleValue\Test.xlsx ";
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);
Worksheet excelSheet = wb.ActiveSheet;
//Read the first cell
string test = excelSheet.Cells[1, 1].Value.ToString();
wb.Close();
This example used the 'Microsoft Excel 15.0 Object Library' but may be compatible with earlier versions of Interop and other libraries.
此示例使用了“Microsoft Excel 15.0 对象库”,但可能与早期版本的 Interop 和其他库兼容。
回答by Udaayipp
Please try this. Maybe this could help you. It works for me.
请试试这个。也许这可以帮助你。这个对我有用。
string sValue = (range.Cells[_row, _column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
//_row,_column your column & row number
//eg: string sValue = (sheet.Cells[i + 9, 12] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
//sValue has your value
回答by Shilpa
It is better to use .Value2() instead of .Value(). This is faster and gives the exact value in the cell. For certain type of data, truncation can be observed when .Value() is used.
最好使用 .Value2() 而不是 .Value()。这更快,并提供单元格中的确切值。对于某些类型的数据,使用 .Value() 时可以观察到截断。
回答by Vityata
The issue with reading single Excel Cell in .Net
comes from the fact, that the empty cell is evaluated to a Null
. Thus, one cannot use its .Value
or .Value2
properties, because an error shows up.
读取单个 Excel 单元格的问题.Net
来自这样一个事实,即空单元格被评估为Null
. 因此,不能使用它的.Value
或.Value2
属性,因为会出现错误。
To return an empty string, when the cell is Null
the Convert.ToString(Cell)
can be used in the following way:
要返回空字符串,当单元格为 时Null
,Convert.ToString(Cell)
可以通过以下方式使用:
Excel.Workbook wkb = Open(excel, filePath);
Excel.Worksheet wk = (Excel.Worksheet)excel.Worksheets.get_Item(1);
for (int i = 1; i < 5; i++)
{
string a = Convert.ToString(wk.Cells[i, 1].Value2);
Console.WriteLine(a);
}
回答by Newclique
Here's a solution that may work better in the case you are referencing objWorksheet.UsedRange.
这是一个在您引用 objWorksheet.UsedRange 的情况下可能更有效的解决方案。
Excel.Worksheet mySheet = ...(load a workbook, etc);
Excel.Range myRange = mySheet.UsedRange;
var values = (myRange.Value as Object[,]);
int rowNumber = 3, columnNumber = 5;
string cellValue = Convert.ToString(values[rowNumber, columnNumber]);