5

我的项目有一个新的要求是读取各种类型的 Excel 文件。我可以使用 Codeplex 的 ExcelDataReader dll 读取 .xls 和 .xlsx 文件。问题是当我尝试读取 .xlsb 文件时。ExcelDataReader 无法读取 .xlsb 文件。Microsoft.Office.Interop.Excel除了在基于服务器的应用程序中使用 dll之外,还有其他有效的读取 xlsb 文件的方法吗?

IExcelDataReader excelReader = fileName.EndsWith(".xlsx")
                                               ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                                               : ExcelReaderFactory.CreateBinaryReader(stream);
while (excelReader.Read())
{
     //myStuff read the file
}
4

3 回答 3

5

LinqToExcel支持 xlsb 以及 xls 和 xlsx。

该库的基本用法如下所示:

using (var excelQueryFactory = new ExcelQueryFactory(filePath))
{
     //access your worksheet LINQ way
     var worksheet = excelQueryFactory.Worksheet("worksheetName").Where(...);
}

更详细的教程

于 2015-02-08T19:18:52.823 回答
5

最快和最简单的解决方案是使用您已经制作的产品。

只需使用 Excel Interop 进行另存为以将 xlsb 转换为 xlsx,然后照常读取新文件。通常我不建议使用 Excel 互操作,因为它非常慢,但是对于仅转换文件来说它已经足够快了(假设正常情况下)。

我建议您尽可能使用 Office Open XML SDK 来读取生成的 xml 文件。

这是我之前做的一个:

public class XlConversion
{
    public static void MarshalReleaseComObject(object comObject)
    {
        if ((comObject != null) && (Marshal.IsComObject(comObject)))
        {
            Marshal.ReleaseComObject(comObject);
        }
    }

    public static void ConvertTsvToExcel(string inputFullPath, string outputExcelFullPath, bool deleteInput = false)
    {
        if (String.IsNullOrWhiteSpace(inputFullPath))
        {
            throw new ArgumentOutOfRangeException(nameof(inputFullPath));
        }

        if (String.IsNullOrWhiteSpace(outputExcelFullPath))
        {
            throw new ArgumentOutOfRangeException(nameof(outputExcelFullPath));
        }

        var inputFilename = new FileInfo(inputFullPath);
        var xlFilename = new FileInfo(outputExcelFullPath);

        const int maxSupportedXlFilenameLength = 218;

        if (xlFilename.FullName.Length > maxSupportedXlFilenameLength)
        {
            throw new ArgumentOutOfRangeException(nameof(outputExcelFullPath), outputExcelFullPath, ("The full path filename (" + xlFilename.FullName.Length + " characters) is longer than Microsoft Excel supports (" + maxSupportedXlFilenameLength + " characters)"));
        }

        var excelApp = new Application();
        Workbooks wbs = excelApp.Workbooks;
        Workbook wb = wbs.Open(inputFilename.FullName);

        wb.SaveAs(xlFilename.FullName, XlFileFormat.xlOpenXMLWorkbook);

        try
        {
            wb.Close();
            //excel.Quit();
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            MarshalReleaseComObject(wb);
            MarshalReleaseComObject(wbs);
            MarshalReleaseComObject(excelApp);
        }

        if (deleteInput)
        {
            File.Delete(inputFilename.FullName);
        }
    }
}
于 2015-02-08T19:20:56.997 回答
1

Aspose.Cells API 可用于方便地读取 XLSB 文件数据。除 XLSB 外,所有其他 Excel 格式如 XLS、XLSX、XLSM 等数据都可以用很少的代码轻松读取。

有关演示,请参阅以下C# 代码、代码内部使用的源 XLSB 文件和代码生成的控制台输出供您参考。

C#

// Print message on Console
Console.WriteLine("Reading XLSB file in C# using Aspose.Cells API.");
Console.WriteLine("----------------------------------------------");

// Directory path of input and output files.
string dirPath = "D:/Download/";

// Load your source XLSB file inside the Aspose.Cells Workbook object.
Workbook wb = new Workbook(dirPath + "Source.xlsb");

// Access first worksheet.
Worksheet ws = wb.Worksheets[0];

// Access cells enumarator
System.Collections.IEnumerator iEnum = ws.Cells.GetEnumerator();

// Print the cells data in while loop on console.
while(iEnum.MoveNext())
{
    Cell cell = (Cell)iEnum.Current;
    Console.WriteLine(cell.Value);
}

控制台输出

Reading XLSB file in C# using Aspose.Cells API.
----------------------------------------------
This is C3 data.
This is J4 data.
This is F6 data.
This is D9 data.
This is H10 data.
This is G15 data.
This is L17 data.
This is B20 data.

Aspose.Cells C# 代码中使用的源 XLSB 文件的快照

使用 Aspose.Cells API 读取的源 XLSB 文件

Aspose.Cells C# 代码生成的控制台输出快照 通过使用 Aspose.Cells API 读取 Excel XLSB 文件生成的控制台输出

于 2018-11-04T11:39:52.257 回答