Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从Excel单元格中读取文本?_C#_Excel - Fatal编程技术网

C# 无法从Excel单元格中读取文本?

C# 无法从Excel单元格中读取文本?,c#,excel,C#,Excel,我试图获取输入excel工作表中特定单元格的字符串值。我的代码如下: Excel.Application excel_app = new Excel.Application(); Excel.Workbook dashboard = excel_app.Workbooks.Open(Filename:@"H:\charge_code_project\test_dashboard.xlsb", ReadOnly: true); Excel.Worksheet worksheet

我试图获取输入excel工作表中特定单元格的字符串值。我的代码如下:

Excel.Application excel_app = new Excel.Application();
Excel.Workbook dashboard = excel_app.Workbooks.Open(Filename:@"H:\charge_code_project\test_dashboard.xlsb", ReadOnly: true);
Excel.Worksheet worksheet = (Excel.Worksheet)dashboard.Worksheets[package_type];
Excel.Range range = worksheet.UsedRange;
int rowCount = range.Rows.Count;
string cell_text = worksheet.Cells[10, 10].Text;
在尝试生成时,我在最后一行收到以下错误:

Error   CS1061  'object' does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
我在以前的项目中使用过这种从excel文件中读取文本的方法,这种方法很有效,所以我知道存在.text。我看不出我以前如何使用它和现在如何使用它之间有什么区别。我相信我已经包括了所有必要的汇编参考和使用语句:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Windows.Forms.VisualStyles;
using System.IO;
我添加了以下程序集引用:

Microsoft Excel 16.0 Object Library
Microsoft Office 15.0 Object Library 
Microsoft Office 16.0 Object Library
Microsoft Visual Basic for Applications Extensibility 5.3
OLE Automation


有人能帮我看看我做错了什么吗?

由于对象没有Text属性,因此您得到了异常

您应该使用该属性获取单元格/区域的值

示例代码:

string cell_text = ((Range)worksheet.Cells[10, 10]).Value.ToString();

当我尝试使用.Value时,也会出现相同的错误。事实上,当我键入时出现的建议只显示4个属性,.ToString,.GetType,。GetHashCode和.Equals。我知道有比这些更多的属性,但它们似乎不知怎么地丢失了。单元格[10,10]返回一个动态类型。这实际上是一个范围对象。只需强制转换。除非目标版本为5,否则不要在.NETCore中尝试Excel互操作。一周前发布的。