尝试使用EPPlus读取.xls文件时出错

时间:2022-11-14 16:03:22

The following code is working fine for .xlsx, but it's not working for .xls. I got this error message

以下代码在.xlsx上运行良好,但在.xls上不工作。我得到了这个错误信息

Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password

不能打开包装。包是一个OLE复合文档。如果这是一个加密包,请提供密码

Code

代码

string filepath = txtBrowse.Text;

FileStream stream = System.IO.File.Open(filepath, FileMode.Open, FileAccess.ReadWrite);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

FileInfo newFile = new FileInfo(filepath);

using (ExcelPackage package = new ExcelPackage(newFile))
{
    string sheetName = System.DateTime.Now.ToShortDateString();

    foreach (OfficeOpenXml.ExcelWorksheet sheet in package.Workbook.Worksheets)
    {
        // Check the name of the current sheet
        if (sheet.Name == sheetName)
        {
            package.Workbook.Worksheets.Delete(sheetName);
            break; // Exit the loop now
        }
    }

    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(System.DateTime.Now.ToShortDateString());
}

How do I do this correctly?

我怎么做得正确?

2 个解决方案

#1


20  

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.

EPPlus不支持XLS格式。只有XLSX。你需要找一个新的图书馆。

#2


1  

I succesfully used Tony's answer https://*.com/a/18904633/306515 and simply convert it using Microsoft.Office.Interop.Excel and can still then use epplus

我成功地使用了Tony的答案https://*.com/a/18904633/306515,并使用Microsoft.Office.Interop进行转换。还可以使用epplus

#1


20  

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.

EPPlus不支持XLS格式。只有XLSX。你需要找一个新的图书馆。

#2


1  

I succesfully used Tony's answer https://*.com/a/18904633/306515 and simply convert it using Microsoft.Office.Interop.Excel and can still then use epplus

我成功地使用了Tony的答案https://*.com/a/18904633/306515,并使用Microsoft.Office.Interop进行转换。还可以使用epplus