Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to read an .xls file using EPPlus

Tags:

c#

excel

epplus

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?

like image 756
user3151262 Avatar asked Oct 01 '14 23:10

user3151262


People also ask

Does EPPlus support XLS?

NET Framework from version 3.5 and . NET Core from version 2.0. EPPlus has no dependencies to any other library such as Microsoft Excel. The library is designed with the developer in mind.

Does Openpyxl work with XLS?

Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files.

How do I save an Excel file as XLSX in C#?

To convert XLS to XLSX in C# - simply load an XLS Excel workbook with IronXL, and then save with the ". xlsx" file extension. Note that images, charts, split panes, freezing panes will not always be copied between XLS and XLSX formats.


2 Answers

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

like image 157
Cory Nelson Avatar answered Sep 29 '22 20:09

Cory Nelson


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

like image 37
ajwaka Avatar answered Sep 29 '22 18:09

ajwaka