Il seguente codice funziona bene per .xlsx, ma non funziona per .xls. Ho ricevuto questo messaggio di errore
Imansible aprire il pacchetto. Il pacchetto è un documento composto OLE. Se si tratta di un pacchetto crittografato, 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 posso fare questo correttamente?
EPPlus non funziona con il formato XLS. Solo XLSX. Dovrai trovare una nuova libreria.
Ho usato con successo la risposta di Tony https://stackoverflow.com/a/18904633/306515 e semplicemente convertirlo usando Microsoft.Office.Interop.Excel e ancora ansible utilizzare epplus