使用 SpreadSheetGear 获取数据行数?

发布于 2024-11-17 16:35:32 字数 752 浏览 1 评论 0原文

我查了一些网上资源,也许我是瞎子,但我还无法找到答案。

我正在上传一个文件,将其转换为流,然后将其输入 SpreadSheetGear。现在,我需要循环遍历每一行并读取数据(这很好)。到目前为止,这是我的代码:

IWorkbook wb = Factory.GetWorkbookSet().Workbooks.OpenFromStream(file.InputStream);
IWorksheet ws = wb.ActiveWorksheet;
IRange cells = ws.Cells;

for (int i = 2; i <= cells.RowCount; i++)
{
    //Code for every row
    for (int x = 1; x <= cells.ColumnCount; x++)
    {
         //Code for every column
    }
}

问题是:cells.RowCount 等于1048576,这显然是 Excel 的行数上限。是否有对 SpreadSheetGear 的调用返回包含数据的行数?我无法使用固定金额,因为提供的电子表格可能有 500 到 2,000 行。

我知道 SpreadSheetGear 可能没有那么广泛使用,但我想无论如何我都会在这里碰碰运气。

干杯!

编辑:

在有人说使用 while 循环之前,行可能为空,因此检查空字符串有点混乱。

I've checked a few online resources, maybe I'm blind but I've as yet been unable to find an answer to this.

I'm uploading a file, converting it to a stream, feeding it into SpreadSheetGear. Now, I need to loop through every row and read the data (which is fine). Here is my code so far:

IWorkbook wb = Factory.GetWorkbookSet().Workbooks.OpenFromStream(file.InputStream);
IWorksheet ws = wb.ActiveWorksheet;
IRange cells = ws.Cells;

for (int i = 2; i <= cells.RowCount; i++)
{
    //Code for every row
    for (int x = 1; x <= cells.ColumnCount; x++)
    {
         //Code for every column
    }
}

Here is the problem: cells.RowCount is equal to 1048576 which is clearly the Excel upper limit on number of rows. Is there a call to SpreadSheetGear that returns the number of rows that have data? I cant use a fixed amount as the spreadsheet provided could have anything from 500 to 2,000 rows.

I understand SpreadSheetGear may not be that widely used but I thought I'd chance my arm here anyway.

Cheers!

Edit :

Before somebody says use a while loop, it's possible that a row can be empty so checking for null strings is a bit of a messy one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

数理化全能战士 2024-11-24 16:35:32

回答我自己的问题,总是这样。

无论如何,最终发现 IWorksheet.UsedRange 它只返回使用的单元格。

Answered my own question, always the way.

Anyway, eventually found IWorksheet.UsedRange which returns just the used cells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文