c# - 如何使用 epplus 遍历 excel 表中的行?

标签 c# excel epplus

我是 epplus 的新手,我正在尝试从 Excel 表中读取一些值。

这是我目前所拥有的:

var fileInfo = new FileInfo(filename);
using(var excelPackage = new OfficeOpenXml.ExcelPackage(fileInfo))
{
    foreach (var sheet in excelPackage.Workbook.Worksheets)
    {
        foreach (ExcelTable table in sheet.Tables)
        {
             foreach(var row in table.Rows)  // <-- !!
             { ... }
        }
    }
}

但是,现在我很困惑,因为 ExcelTable 只有一个 Columns 属性,但没有我预期的 Rows 属性。我在库中的任何对象上都找不到 Rows 属性。

如何遍历表格,逐行读取?

最佳答案

在同一问题上寻求帮助时,我偶然发现了这个 link .它当然对我有用!绝对比使用 Interop 对象更好。 :)

虽然我稍微调整了一下:

var package = new ExcelPackage(new FileInfo("sample.xlsx"));

ExcelWorksheet workSheet = package.Workbook.Worksheets[0];
var start = workSheet.Dimension.Start;
var end = workSheet.Dimension.End;
for (int row = start.Row; row <= end.Row; row++)
{ // Row by row...
    for (int col = start.Column; col <= end.Column; col++)
    { // ... Cell by cell...
        object cellValue = workSheet.Cells[row, col].Text; // This got me the actual value I needed.
    }
}

关于c# - 如何使用 epplus 遍历 excel 表中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21742038/

相关文章:

c# - HLSL 和 XNA : How do I get the position and color of surrounding pixels of the current pixel shader iteration?

C# Keys.Apps 将始终打开 Windows 上下文菜单

c# - 使用 EPPLUS 对 excel 行进行分组

c# - 从 Google 日历中检索条目

c# - cleartool 在命令提示符和 C# 应用程序中的行为不同

excel - =IF(SUMPRODUCT(($A$2 :$A2=A2)*($B$2:$B2=B2))>1, 0,1)? 的详细解释

excel - 轻松查看 excel 公式子集的值

excel - 从两列中提取唯一的不同列表

c# - FormulaR1C1 增加行值 EPPlus

c# - 如何使用 Epplus 从 Excel 获取所有工作表名称?