【问题标题】:Can't read XLS files when i download them and try to open them in openfiledialog C#当我下载 XLS 文件并尝试在 openfiledialog C# 中打开它们时无法读取它们
【发布时间】:2017-09-03 19:11:58
【问题描述】:

所以当我下载 this file this comes up:

现在我认为一切正常,应该可以正常工作。 因此,如果我尝试在我的 Visual Studio 2015 中使用 openfiledialog I get this error.打开此文件@

现在,当我将 excel 保存为 97-2003 xls 文件时,它适用于 createbinaryreader。如果我将它保存为普通的 excel 格式的 xlsx 文件,它适用于 createopenxmlreader

我该如何解决这个问题,这样我就不需要转到 excel 文件并将其专门保存为 excel 工作图(xls 或 xlsx)?

PS:我将这个 excel 文件放入 datagridview 中。

【问题讨论】:

    标签: c# excel visual-studio-2015 exceldatareader


    【解决方案1】:

    这些注释太啰嗦了,无法评论,所以我把它们作为答案在这里:

    注意 1:像这样以适当的方式使用 ExcelReaderFactory:

    IExcelDataReader reader;
    if (file.Extension.Equals(".xls"))
        reader = ExcelReaderFactory.CreateBinaryReader(stream);
    else if (file.Extension.Equals(".xlsx"))
        reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    else
        throw new Exception("Invalid Excel File");
    

    注意2:使用AsDataSet()方法,不带参数。

    result = reader.AsDataSet();
    

    注意 3:您可以使用更新版本的ExcelDataReader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多