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

时间:2018-07-27 08:36:39

标签: c#

所以我是C的新用户。但是在第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://docs.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 

    }
}

}

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

1 个答案:

答案 0 :(得分:1)

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

我认为您想在代码中使用单词Excel,因此您必须为命名空间添加别名:

using Excel = Microsoft.Office.Interop.Excel;

然后它应该工作

Excel.Application excelApp = new Excel.Application();

,并确保已将程序集Microsoft.Office.Interop.Excel添加到项目中。 How to add this assembly