简体   繁体   English

EPPlus 打开 excel .xlsx 文件错误

[英]EPPlus opening excel .xlsx file error

public void DailyOvertime(string fname)
  {
      var existingFile = new FileInfo(fname);

      using (var package = new ExcelPackage(existingFile))
      {
          // Get the work book in the file
           ExcelWorkbook workBook = package.Workbook;
          if (workBook != null)
          {
              if (workBook.Worksheets.Count > 0)
              {
                  // Get the first worksheet
                  ExcelWorksheet currentWorksheet = workBook.Worksheets.First();

                  // read some data                    
                  var Monday = currentWorksheet.GetValue(11, 4);
                  var Tuesday = currentWorksheet.GetValue(13, 4);
                  var Wednesday = currentWorksheet.GetValue(15, 4);
                  var Thursday = currentWorksheet.GetValue(17, 4);
                  var Friday = currentWorksheet.GetValue(19, 4);
                  var Saturday = currentWorksheet.GetValue(21, 4);
                  var Sunday = currentWorksheet.GetValue(23, 4);

              }

          }
      }
  }

I am getting an error while running this code.运行此代码时出现错误。 Every time I get to the line ExcelWorkbook workBook = package.Workbook;每次我到达ExcelWorkbook workBook = package.Workbook; the program will break and I cant figure out why.程序会中断,我不知道为什么。 I'm trying to open an .xlsx file and everything seems to be working fine up to that point.我正在尝试打开一个 .xlsx 文件,到目前为止一切似乎都运行良好。

I did notice that this message was coming up in the Locals:我确实注意到本地人中出现了这条消息:

Workbook 'package.Workbook' threw an exception of type 'System.UriFormatException' OfficeOpenXml.ExcelWorkbook {System.UriFormatException}

Any help would be appreciated Thanks in advance!任何帮助将不胜感激提前致谢!

From the exception message, it seems highly likely that your Excel file has URIs in an incorrect format, and some versions of EPPlus throw exceptions in opening workbooks with incorrect URIs.从异常消息来看,您的 Excel 文件很有可能包含格式不正确的 URI,并且某些版本的 EPPlus 在打开具有不正确 URI 的工作簿时会引发异常。

Make sure you get the latest version of EPPlus from code plex, apparently the latest versions have patches that resolve this specific issue.确保从 code plex 获得最新版本的 EPPlus,显然最新版本具有解决此特定问题的补丁。

Check out this and this .看看这个这个 At the time of posting this answer, some comments in codeplex epplus branch say that this issue isn't fixed.在发布此答案时,codeplex epplus 分支中的一些评论表示此问题未解决。

Alternatively, you could check-out some other libraries that could help in reading xls files.或者,您可以查看一些有助于阅读 xls 文件的其他库。 Check if NPOI could suit your needs.检查NPOI是否可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM