我的项目有一个新要求,即阅读各种类型的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
}
答案 0 :(得分:4)
LinqToExcel支持xlsb以及xls和xlsx。
此库的基本用法如下:
using (var excelQueryFactory = new ExcelQueryFactory(filePath))
{
//access your worksheet LINQ way
var worksheet = excelQueryFactory.Worksheet("worksheetName").Where(...);
}
答案 1 :(得分:2)
最快捷,最简单的解决方案是使用您已经制作的产品。
只需使用Excel Interop进行保存 - 将xlsb转换为xlsx,然后照常读取新文件。通常我不建议使用Excel Interop,因为它非常慢,但只是转换文件它足够快(假设正常条件)。
我建议您尽可能使用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);
}
}
}
答案 2 :(得分:0)
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文件快照