Return 值不为空的单元格使用 linq - Epplus
Return cell with value not null using linq - Epplus
我正在使用下面的 Linq 代码 return 我行中最后一个填充的单元格。但是,我不知道出于什么原因(可能是某种格式,或类似的原因)具有空值的单元格被 returned。例子
我的行是从B16开始到AG16,论文中的代码应该是returnAG16,但是这是returning AI16,而且这个单元格是空白的,没有值,公式,没什么。
我想忽略具有空白值的单元格,所以 return AG16。
我正在根据下面的代码进行操作,returns AI16 尽管它没有价值
var lastRowCellValue = worksheet.Cells.Last(c => c.End.Row == 16)
- 16是他要检查的行行
我已经尝试使用下面的代码,但它 return 是 NULL 而不是 AG16,这是我最后一个值单元格
var lastRowCellValue = worksheet.Cells.Last(c => c.End.Row == 16).Where(c => c.Value != null);
我做错了什么?
假设问题类似于 this, with empty cells or formatting causing the problem, you could use a function derived from this answer:
int LastColumnByRow(ExcelWorksheet sheet, int rownum) {
var col = sheet.Dimension.End.Column;
while (col >= 1) {
if (!string.IsNullOrEmpty(sheet.Cells[rownum, col].Text)) {
break;
}
col--;
}
return col;
}
这样调用:
var lastRowCellValue = worksheet.Cells[16, LastColumnByRow(ws, 16)];
我正在使用下面的 Linq 代码 return 我行中最后一个填充的单元格。但是,我不知道出于什么原因(可能是某种格式,或类似的原因)具有空值的单元格被 returned。例子
我的行是从B16开始到AG16,论文中的代码应该是returnAG16,但是这是returning AI16,而且这个单元格是空白的,没有值,公式,没什么。
我想忽略具有空白值的单元格,所以 return AG16。
我正在根据下面的代码进行操作,returns AI16 尽管它没有价值
var lastRowCellValue = worksheet.Cells.Last(c => c.End.Row == 16)
- 16是他要检查的行行
我已经尝试使用下面的代码,但它 return 是 NULL 而不是 AG16,这是我最后一个值单元格
var lastRowCellValue = worksheet.Cells.Last(c => c.End.Row == 16).Where(c => c.Value != null);
我做错了什么?
假设问题类似于 this, with empty cells or formatting causing the problem, you could use a function derived from this answer:
int LastColumnByRow(ExcelWorksheet sheet, int rownum) {
var col = sheet.Dimension.End.Column;
while (col >= 1) {
if (!string.IsNullOrEmpty(sheet.Cells[rownum, col].Text)) {
break;
}
col--;
}
return col;
}
这样调用:
var lastRowCellValue = worksheet.Cells[16, LastColumnByRow(ws, 16)];