c# - 无法访问空单元格 EPPlus

标签 c# excel epplus

试图找出如何读取所有单元格,甚至是空单元格。

问题: 当一个单元格为空时,它会被忽略,并使用下一个单元格的值来代替……并抛出异常。

这个:

姓名............编号

汽车......................10

盒子......................26

......................19

苹果......................26

结束:

姓名............编号

汽车......................10

盒子......................26

19............苹果

26................................

var ws = excelPackage.Workbook.Worksheets.First();

for (var i = 3; i <= ws.Dimension.End.Row; i++) 
  {
  var cellValues = ws.Cells[i, 1, i, ws.Dimension.End.Column].ToList(); 

 var newThings = new Store
      {
        Name = cellValues[0].Text,
        Number = cellValues[1].Text,
      };

我试过类似的东西

Name = string.IsNullOrWhiteSpace(cellValues[0].Text) ? " " : cellValues[0].Text;

但为时已晚,因为损坏已经造成。 让这个东西工作会很棒,但我似乎不知道如何做。

如果您能提供任何帮助,我将不胜感激。 :)

最佳答案

您可以尝试单独读取每一行/列,而不是使用 EPPlus 范围。与此类似(这是未经测试的):

for (var j = 3; j <= excelSheet.Dimension.End.Row; ++j)
{
    var name = (excelSheet.Cells[j, 0].Value ?? "").ToString();
    var number = (excelSheet.Cells[j, 1].Value ?? "").ToString();
    var newThings = new Store
    {
        Name = name,
        Number = number
    };
}

关于c# - 无法访问空单元格 EPPlus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218134/

相关文章:

c# - SonarQube 未更新报告 - "Line 5 is out of range for file Program.cs. File has 4 lines"

c# - 在 Winform 中的 datagridview 底部添加新行

c# - 更改 SqlConnection 字符串并在运行时调用 SqlConnectionStringBuilder

excel - 如果满足三个条件,如何突出显示一行?

vba - Excel vba - 打开具有可变(日期)文件名的文件

excel - Epplus SetPosition图片问题

c# - 在 Epplus 中使用小计功能时,出现 "Excel found unreadable content in..."错误

c# - silverlight 项目文件的 ItemGroup 中的 WCFMetadata 标记

c# - 由于 NativeWindow,如何修复 Excel VSTO 项目中的 ThreadAbortException?

c# - 如何在 EPPlus 中过滤列(而不是行)?