以下代碼對.xlsx正常工作,但對於.xls不起作用。我收到了此錯誤消息嘗試使用EPPlus讀取.xls文件時出錯
無法打開包。包是一個OLE複合文檔。如果這是一個加密包,請提供密碼
代碼
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());
}
如何正確地做到這一點?
感謝烏拉圭回合的答覆。但是,你能建議任何一個容易閱讀和處理excel的東西嗎?因爲我的項目完成了,所以我只想編輯一些epplus的代碼。 – user3151262 2014-10-01 23:26:57
@ user3151262,NPOI適用於.xls和.xlsx'https:// npoi.codeplex.com /' – user3473830 2014-10-01 23:39:24