我正在使用 epplus 读取一个 excel 文件,我想遍历每一行和每一列并查找列标题以检查集合并读取单元格值是否匹配。我是 epplus 的新手,找不到任何引用来做这样的事情。我在 this link 中研究了类似的问题,但我没有通过列标题循环的东西。
有没有可能这样做?任何帮助深表感谢。
谢谢!
最佳答案
如果要在 Excel 文件中查找与您指定的名称匹配的列,可以使用此函数:
public string FindColumnAddress(ExcelWorksheet sheet, string columnName)
{
int totalRows = sheet.Dimension.End.Row;
int totalCols = sheet.Dimension.End.Column;
var range = sheet.Cells[1, 1, 1, totalCols];
for (int i = 1; i <= totalCols; i++)
{
if (range[1, i].Address != "" && range[1, i].Value != null && range[1, i].Value.ToString() == columnName)
return range[1, i].Address;
}
return null;
}
它将返回列标题的地址。
您可以将其更改为返回 i 作为列的索引。
关于excel - 如何使用epplus循环读取excel中的列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25629085/