Error when trying to read an .xls file using EPPlus

六月ゝ 毕业季﹏ 提交于 2020-01-11 08:16:48

问题


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

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

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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/26152215/error-when-trying-to-read-an-xls-file-using-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!