简体   繁体   English

无法在 C# 中使用 Gembox 加载 .xls 文件

[英]Unable to load .xls file using Gembox in C#

I wanted to load .xls file(Type: 97-2003 spreadsheet) using C#.我想使用 C# 加载.xls文件(类型: 97-2003电子表格)。 I am using Gembox library.我正在使用 Gembox 库。

When I used below command, I encountered " file contains corrupted data. " as error.当我使用下面的命令时,我遇到“文件包含损坏的数据。 ”作为错误。

ExcelFile ef = ExcelFile.Load(filepath, XlsxLoadOptions.XlsxDefault);

When I removed XlsxLoadOptions parameter than I am getting " Reading error: file is not a valid OLE2 Compound File. "当我删除 XlsxLoadOptions 参数时,我收到“读取错误:文件不是有效的 OLE2 复合文件。

I am new to C# and unable to debug the root-cause of the issue.我是 C# 新手,无法调试问题的根本原因。 Please help!请帮忙!

UPDATE (2020-03-28)更新 (2020-03-28)

In the newer versions of GemBox.Spreadsheet, the ExcelFile.Load(String) will check the file's signature in case of a ".xls" file.在较新版本的 GemBox.Spreadsheet 中, ExcelFile.Load(String)将检查文件的签名,以防出现“.xls”文件。
In other words, there is no more need for that GetLoadOptions method from below.换句话说,不再需要下面的GetLoadOptions方法。

Also, there is a new overload method, ExcelFile.Load(Stream) .此外,还有一个新的重载方法ExcelFile.Load(Stream)
This one will always check the file's signature in the provided stream.这将始终检查提供的流中的文件签名。

ORIGINAL原版

This question was answered in the comments.这个问题在评论中得到了回答。 Unfortunately, it is not well visible there, so here is the answer and also some additional details about it.不幸的是,它在那里不太明显,所以这里是答案以及有关它的一些其他详细信息。

GemBox.Spreadsheet provides few Load overload methods . GemBox.Spreadsheet 提供了很少的Load 重载方法 When using the following:使用以下内容时:

ExcelFile ef = ExcelFile.Load("C://temp//book.xls");

It will result in following:它将导致以下结果:

ExcelFile ef = ExcelFile.Load("C://temp//book.xls", LoadOptions.XlsDefault);

Which is the same as the following:这与以下内容相同:

ExcelFile ef = ExcelFile.Load("C://temp//book.xls", new XlsLoadOptions());

The load options specify how the input file will be read and when using the ExcelFile.Load(String) method, the options will be based on the file's extension.加载选项指定如何读取输入文件,并且在使用ExcelFile.Load(String)方法时,选项将基于文件的扩展名。

In this case, the file has ".xls" extension, however, it was not of a binary XLS format (BIFF8), but rather it was of an HTML format.在这种情况下,文件具有“.xls”扩展名,但是,它不是二进制 XLS 格式 (BIFF8),而是 HTML 格式。 This is a trick that is somewhat commonly used, you can have HTML, CSV, even XLSX files with a ".xls" extension and MS Excel will be able to open it.这是一个有点常用的技巧,您可以拥有 HTML、CSV,甚至带有“.xls”扩展名的 XLSX 文件,并且 MS Excel 将能够打开它。 It will detect the right file's format and it will prompt the user with something like the following message:它将检测正确的文件格式,并会提示用户类似以下消息:

File format and extension of 'book.xls' don't match. “book.xls”的文件格式和扩展名不匹配。 The file could be corrupted or unsafe.该文件可能已损坏或不安全。 Unless you trust its source, don't open it.除非你相信它的来源,否则不要打开它。 Do you want to open it anyway?还是要打开它?

Note that this trick only works with ".xls" extension, it does not work with for example ".xlsx".请注意,此技巧仅适用于“.xls”扩展名,不适用于例如“.xlsx”。 Nevertheless, we could use something like the following to detect the right file format:不过,我们可以使用类似以下内容来检测正确的文件格式:

private static LoadOptions GetLoadOptions(string path)
{
    string extension = Path.GetExtension(path).ToUpperInvariant();
    switch (extension)
    {
        case ".XLSX":
        case ".XLSM":
        case ".XLTX":
        case ".XLTM":
            return LoadOptions.XlsxDefault;
        case ".XLS":
        case ".XLT":
            return GetLoadOptions(path, null);
        case ".ODS":
        case ".OTS":
            return LoadOptions.OdsDefault;
        case ".TAB":
        case ".TSV":
            return new CsvLoadOptions(CsvType.TabDelimited);
        case ".CSV":
            return LoadOptions.CsvDefault;
        default:
            return null;
    }
}

private static LoadOptions GetLoadOptions(string xlsPath, LoadOptions defaultOptions)
{
    byte[] signature = new byte[8];
    using (var stream = File.OpenRead(xlsPath))
        stream.Read(signature, 0, 8);

    byte[] xlsSignature = new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 };
    if (signature.SequenceEqual(xlsSignature))
        return LoadOptions.XlsDefault;

    byte[] xlsxSignature = new byte[] { 0x50, 0x4B, 0x03, 0x04 };
    if (signature.Take(4).SequenceEqual(xlsxSignature))
        return LoadOptions.XlsxDefault;

    string firstLine = File.ReadLines(xlsPath)
        .First(line => !string.IsNullOrWhiteSpace(line)).TrimStart().ToUpperInvariant();
    if (firstLine.StartsWith("<!DOCTYPE") ||
        firstLine.StartsWith("<HTML") ||
        firstLine.StartsWith("<BODY"))
        return LoadOptions.HtmlDefault;

    return defaultOptions;
}

Also here is a small demonstration example on how to use it:这里还有一个关于如何使用它的小演示示例:

string filepath = "C://temp//book.xls";
LoadOptions options = GetLoadOptions(filepath);

if (options == null)
    throw new FileFormatException();

ExcelFile ef = ExcelFile.Load(filepath, options);
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM