C#和Excel:Value2没有定义 [英] C# & Excel: Value2 has no definition

查看:290
本文介绍了C#和Excel:Value2没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I needed a way to read an Excel file through c#. I found an example https://coderwall.com/p/app3ya/read-excel-file-in-c

我创建了一个独立的程序来对其进行测试,并且该程序可以按预期工作.我将代码作为子程序添加到现有程序中,遇到错误.

I created a standalone program to test it and it worked as I had expected. I added the code as a subprogram into an existing program and encountered errors.

第一个错误,无法将类型'object'隐式转换为Microsoft.Office.Interop.Excel._Worksheet.存在显式转换."

The first error, "Cannot implicitly convert type 'object' to Microsoft.Office.Interop.Excel._Worksheet. An explicit conversion exists."

第二个错误,'对象'不包含'Value2'的定义".

The second error, "'Object' does not contain a definition for 'Value2'".

我通过向xlWorkbook.Sheets [1]中添加(Excel._Worksheet)来解决了第一个错误.

I fixed the first error with adding (Excel._Worksheet) to xlWorkbook.Sheets[1];

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

I can't figure out the second one on Value2 and need your help: "Error 98 'object' does not contain a definition for 'Value2' and no extension method 'Value2' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"

这是我的代码:

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);
    }

推荐答案

此问题的原因是目标框架!它使用的是.Net 3.5,因为主程序创建于2008年.今天添加了此子程序,并假定嵌入的互操作类型= true"(引用属性:Microsoft.Office.Interop.Excel).

The cause of this problem was the Target Framework! It was using .Net 3.5 because the main program was created in 2008. This subprogram was added today and it assumes that "Embed Interop Types = true" (Property of Reference: Microsoft.Office.Interop.Excel).

我转到了项目属性",并将目标框架"设置为".Net Framework 4客户端配置文件".生成了43个错误(不记得该错误的名称,但表明我可能需要添加对Microsoft.CSharp.dll的引用).我添加了Microsoft.CSharp参考,所有错误都消失了!

I went to the Project Properties and set the Target Framework to ".Net Framework 4 Client Profile". That generated 43 errors (can't remember the error name but indicated that maybe I needed to add a reference to Microsoft.CSharp.dll). I added the Microsoft.CSharp reference and all the errors disappeared!

这篇关于C#和Excel:Value2没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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