2014-10-01 7 views
8

Il seguente codice funziona correttamente per .xlsx, ma non funziona per .xls. Ho ricevuto questo messaggio di erroreErrore durante il tentativo di leggere un file .xls utilizzando EPPlus

Impossibile aprire il pacchetto. Il pacchetto è un documento composto OLE. Se questo è un pacchetto cifrato, si prega di fornire la password

Codice

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()); 
} 

Come faccio a fare questo in modo corretto?

risposta

19

EPPlus non funziona con il formato XLS. Solo XLSX. Dovrai trovare una nuova libreria.

+1

Grazie per la risposta ur. Ma puoi suggerire qualcuna che sia facile da leggere ed elaborare l'Excel. Perché il mio progetto è stato completato, quindi voglio modificare solo il codice di epplus. – user3151262

+4

@ user3151262, NPOI funziona sia per .xls che per .xlsx 'https: // npoi.codeplex.com /' – user3473830

Problemi correlati