C# & Excel:Value2 没有定义

Bob*_*b T 4 c# excel visual-studio-2010

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

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

第一个错误,“无法将类型‘对象’隐式转换为 Microsoft.Office.Interop.Excel._Worksheet。存在显式转换。”

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

我通过将 (Excel._Worksheet) 添加到 xlWorkbook.Sheets[1] 修复了第一个错误;

我无法找出 Value2 上的第二个,需要您的帮助:“错误 98 'object' 不包含 'Value2' 的定义,并且找不到接受类型为 'object' 的第一个参数的扩展方法 'Value2' (您是否缺少 using 指令或程序集引用?)”

这是我的代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

Bir*_*DIN 5

我认为它与 Microsoft.Office.Interop.Excel 版本有关。我以前从未使用代码块以下方法来解决相同的错误。我认为这对他有用。

   object _xVal;

    _xVal= ((Excel.Range)xlWorksheet.Cells[i, j]).Value2;
Run Code Online (Sandbox Code Playgroud)

  • 你能补充解释吗? (2认同)

Bob*_*b T 4

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

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