C#&Excel:Value2没有定义

我需要一种通过c#读取Excel文件的方法。 我find一个例子https://coderwall.com/p/app3ya/read-excel-file-in-c

我创build了一个独立的程序来testing它,它按我的预期工作。 我将代码作为子程序添加到现有程序中并遇到错误。

第一个错误,“不能隐式转换types的对象”Microsoft.Office.Interop.Excel._Worksheet。存在明确的转换。

第二个错误“Object”不包含“Value2”的定义。

我解决了第一个错误,将(Excel._Worksheet)添加到xlWorkbook.Sheets [1];

我找不到Value2的第二个,需要你的帮助:“错误98'对象'不包含'Value2'的定义,并且不能find接受types'object'的第一个参数的扩展方法'Value2' (你是否缺less使用指令或程序集引用?)“

这是我的代码:

using System; using System.Collections.Generic; using System.Collections;ArrayList, ListBox, ComboBox, etc. using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using XView.Properties; using System.Reflection; using ZedGraph; using System.IO; using System.Management; using System.Threading; using System.Diagnostics; using System.Runtime.InteropServices; using Excel = Microsoft.Office.Interop.Excel; private void readExcelFile(ArrayList al) { string rowItems = string.Empty; //Create COM Objects. Create a COM object for everything that is referenced Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"W:\acad\x-pak\XPak setting_tool_charts.xlsx"); Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; //iterate over the rows and columns and print to the console as it appears in the file - Excel is not zero based!! //---start at i = 4 since in Rick's Excel file, that's where the data begins! for (int i = 4; i <= rowCount; i++) { rowItems = string.Empty; for (int j = 1; j <= colCount; j++) { //new line if (j == 1) Console.Write("\r\n"); //write the value to the console if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) { //Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t"); if (j == 1) //new line rowItems = xlRange.Cells[i, j].Value2.ToString(); else rowItems += "^" + xlRange.Cells[i, j].Value2.ToString(); } } al.Add(rowItems); } //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); //rule of thumb for releasing com objects: // never use two dots, all COM objects must be referenced and released individually // ex: [somthing].[something].[something] is bad //release com objects to fully kill excel process from running in the background Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet); //close and release xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook); //quit and release xlApp.Quit(); Marshal.ReleaseComObject(xlApp); } 

我认为这与Microsoft.Office.Interop.Excel版本有关。 我以前从来没有用过代码块来解决同样的错误。 我认为这会对他有用。

  object _xVal; _xVal= ((Excel.Range)xlWorksheet.Cells[i, j]).Value2; 

这个问题的原因是目标框架! 它使用的是.NET 3.5,因为主程序是在2008年创build的。这个子程序今天被添加,它假设“Embed Interop Types = true”(引用属性:Microsoft.Office.Interop.Excel)。

我去了项目属性,并将目标框架设置为“.Net Framework 4客户端configuration文件”。 这生成了43个错误(不记得错误名称,但表示可能需要添加对Microsoft.CSharp.dll的引用)。 我添加了Microsoft.CSharp引用,所有的错误消失了!