如何解决在 C# 中找不到的方法? [英] How to resolve the Method not found in C#?

查看:127
本文介绍了如何解决在 C# 中找不到的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取 xlsx 文件,我从
与此异常相关的代码段如下:

public static sst SharedStrings;///<总结>///反序列化 Excel 工作簿中的所有工作表///</总结>///<param name="ExcelFileName">Excel xlsx 文件的完整路径和文件名</param>///<returns></returns>公共静态 IEnumerable工作表(字符串 ExcelFileName){工作表 ws;使用 (ZipArchive zipArchive = ZipFile.Open(ExcelFileName, ZipArchiveMode.Read)){SharedStrings = DeserializedZipEntry(GetZipArchiveEntry(zipArchive, @"xl/sharedStrings.xml"));foreach (var worksheetEntry in (WorkSheetFileNames(zipArchive)).OrderBy(x => x.FullName)){ws = DeserializedZipEntry(worksheetEntry);ws.NumberOfColumns = worksheet.MaxColumnIndex + 1;ws.ExpandRows();收益率 ws;}}}


经过一番搜索,我发现我需要针对 .NET 4.5 或更高版本,(我的目标是 4.6.1,但我也尝试过 4.5,结果仍然相同).另外,就依赖项而言,我的引用如下所示:

完成所有这些之后,我仍然遇到上面提到的异常,并且不知道为什么会发生这种情况.
编辑 1:我已经浏览了

注意 System.IO.Compression.ZipFile 被引用.无法在我的环境中引用它.

使用Workbook.Worksheets()或将调用类声明为Excel.Workbook的子类.

ExcelCodeProject 文章 提供必要类的源代码.

使用Excel;使用系统;命名空间 akExcelAsZipDemo{课程计划:练习册{// 从//https://www.codeproject.com/tips/801032/csharp-how-to-read-xlsx-excel-file-with-lines-ofthe//// 启发://https://github.com/ahmadalli/ExcelReaderstatic void Main(string[] args){const string fileName = @"E:\AK\export.xlsx";var worksheets = Worksheets(fileName);foreach(工作表中的工作表 ws){Console.WriteLine($"cols={ws.NumberOfColumns} rows={ws.Rows.Length}");}Console.WriteLine();}}}

I'm trying to read an xlsx file, and I got a basic overview from This Codeproject Link
Now, I'm getting the following exception message:

The code segment related to this exception is given below:

public static sst SharedStrings;

    /// <summary>
    /// All worksheets in the Excel workbook deserialized
    /// </summary>
    /// <param name="ExcelFileName">Full path and filename of the Excel xlsx-file</param>
    /// <returns></returns>
    public static IEnumerable<worksheet> Worksheets(string ExcelFileName)
    {
        worksheet ws;
        using (ZipArchive zipArchive = ZipFile.Open(ExcelFileName, ZipArchiveMode.Read))
        {
            SharedStrings = DeserializedZipEntry<sst>(GetZipArchiveEntry(zipArchive, @"xl/sharedStrings.xml"));
            foreach (var worksheetEntry in (WorkSheetFileNames(zipArchive)).OrderBy(x => x.FullName))
            {
                ws = DeserializedZipEntry<worksheet>(worksheetEntry);
                ws.NumberOfColumns = worksheet.MaxColumnIndex + 1;
                ws.ExpandRows();
                yield return ws;
            }
        }
    }


After some searching, I figured out that I needed to target .NET 4.5 or above version, (I'm targetting 4.6.1, but I also have tried 4.5, still same results). Also, as far as the dependencies are concerned, my references look like this:

After doing all this, I'm still getting the exception mentioned above, and have no idea why this is happening.
EDIT 1: I've been through This StackOverflow Link, where I figured that I need the latest DLLs. I went to the Nuget Package Manager, and there were "No Packages Found" which were available for the updates.

解决方案

I have tried .NET 4.6.1 and 4.6.2. Both work OK for me.

My Visual Studio is Community Edition 2017

The project references:

Note that System.IO.Compression.ZipFile is not referenced. It was not possible to reference it in my environment.

Use Workbook.Worksheets() or declare the calling class as sub-class of Excel.Workbook.

Excel is the namespace of the CodeProject article source which provides the necessary classes.

using Excel;
using System;

namespace akExcelAsZipDemo
{
    class Program : Workbook
    {
        //  from
        //  https://www.codeproject.com/tips/801032/csharp-how-to-read-xlsx-excel-file-with-lines-ofthe
        //
        //  inspired:
        //  https://github.com/ahmadalli/ExcelReader

        static void Main(string[] args)
        {
            const string fileName = @"E:\AK\export.xlsx";

            var worksheets = Worksheets(fileName);

            foreach (worksheet ws in worksheets)
            {
                Console.WriteLine($"cols={ws.NumberOfColumns} rows={ws.Rows.Length}");
            }

            Console.WriteLine();
        }
    }
}

这篇关于如何解决在 C# 中找不到的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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