我如何使用epplus遍历Excel表中的行? [英] How do i iterate through rows in an excel table using epplus?
问题描述
我是 epplus 的新手,我正试图从excel表中读取一些值。
I'm new to epplus, and i'm trying to read some values from an excel table.
这是我到目前为止的内容:
This is what i have so far:
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
属性。
However, now i'm stumped, as the ExcelTable
only has a Columns
property, but not a Rows
property as i had expected. I cannot find a Rows
property on any object in the library.
如何遍历表,在逐行阅读?
推荐答案
在寻求有关同一问题的帮助时,我偶然发现了这个< a href = http://codealoc.wordpress.com/2012/04/19/reading-an-excel-xlsx-file-from-c/>链接。当然对我有用!绝对比使用Interop对象好。 :)
While searching for help on the same problem, I stumbled across this link. It certainly worked for me! Definitely better than using Interop objects. :)
我对此做了些微调整:
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.
}
}
这篇关于我如何使用epplus遍历Excel表中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!