尝试使用EPPlus读取.xls文件时出错 [英] Error when trying to read an .xls file using EPPlus

查看:2644
本文介绍了尝试使用EPPlus读取.xls文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码适用于.xlsx,但它并不适用于.xls。我收到这个错误信息

The following code is working fine for .xlsx, but it's not working for .xls. I got this error message


无法打开包。软件包是一个OLE复合文档。如果这是加密包,请提供密码

Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password

代码

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

如何正确执行?

推荐答案

EPPlus不适用于XLS格式。只有XLSX。你需要找到一个新的图书馆。

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

这篇关于尝试使用EPPlus读取.xls文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆