15

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?

Chris
  • 1,533
  • 2
  • 17
  • 33
user3151262
  • 151
  • 2
  • 2
  • 10
  • This also shows up as an index out of bounds error when you access `package.Workbook.Worksheets[1]` – Savage Oct 18 '19 at 11:29

2 Answers2

29

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

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • 1
    Thanks for ur reply. But can you suggest any one which is easy for reading and process the excel. Because my project completed so i want to edit some code of epplus only. – user3151262 Oct 01 '14 at 23:26
7

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

Community
  • 1
  • 1
ajwaka
  • 608
  • 1
  • 7
  • 12