找不到 Excel 类型或命名空间名称

dip*_*pl0 0 c#

所以我是一个新的 C Sharp 用户。然而,在第 4、18、19 和 20 行,我收到以下错误:

找不到 Excel 类型或命名空间名称

我已经导入了对excel COM库的引用,但没有效果。

using System;
using System.IO;
using System.Diagnostics;
using Excel;
namespace CallPython
{

class Program
{

    private static bool ExcelCommentsChecker()
    {
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook workBook = excelApp.Workbooks.Open(@"C:\Tools\comments.xlsx");
        foreach (var worksheet in workBook.Worksheets()
            if (worksheet.Comments != null)
            {
                Console.WriteLine("comments");
            }
            else
            {
                Console.WriteLine("The variable is set to false.");
            }
        //foreach (var row in worksheet.Rows)
        //   foreach (var cell in row.Cells)
        // https://learn.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-add-and-delete-worksheet-comments
        // https://www.c-sharpcorner.com/blogs/how-to-add-move-hide-remove-comments-to-an-excel-worksheet-cell-using-epplus-net-application-c-sharp-part-eight
        //https://www.codeproject.com/Tips/801032/Csharp-How-To-Read-xlsx-Excel-File-With-Lines-of
        // if (cell != null) // Do something with the cells
        return false;
    }


    static void Main(string[] args)
    {
        // full path of python interpreter 
        string python = @"C:\Tools\python3\python.exe";

        // python app to call 
        string myPythonApp = @"C:\Tools\wordchecksinglefile.py";
        //https://www.codeproject.com/Tips/801032/Csharp-How-To-Read-xlsx-Excel-File-With-Lines-of
        // Create new process start info 
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

        // make sure we can read the output from stdout 
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;

        // start python app with 3 arguments  
        // 1st arguments is pointer to itself,  
        // 2nd and 3rd are actual arguments we want to send 
        myProcessStartInfo.Arguments = myPythonApp;

        Process myProcess = new Process();
        // assign start information to the process 
        myProcess.StartInfo = myProcessStartInfo;

        // start the process 
        myProcess.Start();

        // Read the standard output of the app we called.  
        // in order to avoid deadlock we will read output first 
        // and then wait for process terminate: 
        StreamReader myStreamReader = myProcess.StandardOutput;
        string myString = myStreamReader.ReadLine();

        /*if you need to read multiple lines, you might use: 
            string myString = myStreamReader.ReadToEnd() */

        // wait exit signal from the app we called and then close it. 
        myProcess.WaitForExit();
        myProcess.Close();
        ///
        Console.WriteLine("Value received from script: " + myString);

        System.Threading.Thread.Sleep(3000);

        // write the output we got from python app 

    }
}
Run Code Online (Sandbox Code Playgroud)

}

我知道这是一个导入错误,但我在 C Sharp 方面还不够先进,无法知道这个错误发生在哪里。

Ant*_*e V 5

您正在使用Office Interop Objects,但使用的命名空间不正确。

Excel我想你想在代码中使用这个词,所以你必须给命名空间起别名:

using Excel = Microsoft.Office.Interop.Excel;
Run Code Online (Sandbox Code Playgroud)

那么它应该可以工作

Excel.Application excelApp = new Excel.Application();
Run Code Online (Sandbox Code Playgroud)

并确保您已将程序集添加Microsoft.Office.Interop.Excel到您的项目中。如何添加这个程序集