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:
我正在尝试读取一个xlsx文件,现在我从这个Codeproject链接中得到了一个基本的概述,我得到了以下异常消息:与此异常相关的代码段如下所示:
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 * 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.
在搜索之后,我发现我需要以。net 4.5或更高版本为目标(我的目标是4.6.1,但我也尝试了4.5,仍然是相同的结果)。而且,就依赖关系而言,我的引用是这样的:在完成所有这些之后,我仍然会得到上面提到的异常,并且不知道为什么会发生这种情况。编辑1:我已经通过了这个*链接,我认为我需要最新的dll。我去了Nuget包管理器,那里“没有找到包”,可以进行更新。
1 个解决方案
#1
1
I have tried .NET 4.6.1
and 4.6.2.
Both work OK for me.
我尝试过。net 4.6.1和4.6.2。这两个对我来说都可以。
My Visual Studio
is Community Edition 2017
我的Visual Studio是2017年的社区版
The project references:
项目参考:
Note that System.IO.Compression.ZipFile
is not referenced. It was not possible to reference it in my environment.
注意,System.IO.Compression。ZipFile不是引用。不可能在我的环境中引用它。
Use Workbook.Worksheets()
or declare the calling class as sub-class of Excel.Workbook
.
使用Workbook.Worksheets()或将调用类声明为Excel.Workbook的子类。
Excel
is the namespace of the CodeProject article source which provides the necessary classes.
Excel是CodeProject文章源代码的命名空间,提供了必要的类。
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();
}
}
}
#1
1
I have tried .NET 4.6.1
and 4.6.2.
Both work OK for me.
我尝试过。net 4.6.1和4.6.2。这两个对我来说都可以。
My Visual Studio
is Community Edition 2017
我的Visual Studio是2017年的社区版
The project references:
项目参考:
Note that System.IO.Compression.ZipFile
is not referenced. It was not possible to reference it in my environment.
注意,System.IO.Compression。ZipFile不是引用。不可能在我的环境中引用它。
Use Workbook.Worksheets()
or declare the calling class as sub-class of Excel.Workbook
.
使用Workbook.Worksheets()或将调用类声明为Excel.Workbook的子类。
Excel
is the namespace of the CodeProject article source which provides the necessary classes.
Excel是CodeProject文章源代码的命名空间,提供了必要的类。
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();
}
}
}