C#:使用 ExcelPackage 获取行数/列数
我需要从 Excel 电子表格中读取和写入数据。有没有一种方法可以使用 ExcelPackage 找出某个工作表有多少行/列?我有以下代码:
FileInfo newFile = new FileInfo(@"C:\example.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
}
我需要迭代该工作表的每个单元格并将其吐入一个相当大的表中,但我不想打印出空白单元格或得到异常。是否有类似 worksheet.rowNum
或 colNum
的方法?
I need to read and write data from an Excel spreadsheet. Is there a method to finding out how many rows/columns a certain worksheet has using ExcelPackage? I have the following code:
FileInfo newFile = new FileInfo(@"C:\example.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
}
I need to iterate through each cell this worksheet has and spit it into a fairly big table, but I don't want to print out blank cells or get an exception. Is there a method resembling worksheet.rowNum
or colNum
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用 ExcelPackage(EPPlus.dll 版本 3.0.0.2)获取行数和列数,如下所示:
You can get the row and column count using ExcelPackage (EPPlus.dll version 3.0.0.2) as below:
这就是我所做的:
要获取工作簿上的值数组:
要获取范围,请执行以下操作:
This is what I do:
To get the array of values on the workbook:
to get the range do the following:
我将从UsedRange 属性开始,然后对UsedRange 最后一行中的每个单元格执行Cell.End(xlUp)。这应该为您提供每列的最终单元格,具有最大行索引的单元格是您真正使用的范围中的最后一个单元格。
UseRange 属性可能会出现错误,因为当单元格被清除但未删除时,UsedRange 属性不会更新。要使UsedRange属性再次有效,只需选择最后一个单元格之后的所有行和列(因此所有空白单元格),然后进行编辑->删除。
I would start with the UsedRange property and then for each Cell in the final Row of the UsedRange do Cell.End(xlUp). This should get you the final cell for each column, the cell which has the maximum row index is the last cell in your true used range.
The UsedRange property can appear wrong because when cells are cleared but not deleted, the UsedRange property is not updated. To make the UsedRange property valid again simply select all the rows and columns after your last cell (so all the blank cells) and go edit->delete.
我只是做了以下循环来解决问题。仅当您事先知道有多少列时,它才能正常工作。否则将需要另一个循环迭代。
I just did the following loop to solve the problem. It works fine only if you know how many columns there will be beforehand. Otherwise it would take another loop iteration.
我单独使用sheet.UsedRange,但注意到范围末尾的一些单元格是空白的,但仍然包含在范围内。
这是可行的,但是为了提高效率,您可能最好从范围中的最后一行开始并倒数以查看数据结束的位置(而不是从第一行开始的此片段!)
I was using sheet.UsedRange on its own but noticed that some cells at the end of the range were blank but still included in the range.
This works, however for efficiency you might be better staring from the last row in the range and counting back to see where your data ends (as opposed to this snippet which starts from the first row!)
我使用 ExcelPackage 库查找了一些网站。
另外,codeplex 上的页面有一个问题 - 如何获取行/列数?
好像没有支持啊抱歉,该文档也不可用。
您必须迭代行/列(记住电子表格可以容纳的最大行/列)并检查单元格是否包含任何值。
请参阅此链接 - http://web.archive.org/web/20110123164144/http://nayyeri.net/use-excelpackage-to-manipulate-open-xml-excel-files(原始链接已失效)
I have looked up a few websites using ExcelPackage library.
Also, the page on codeplex has a question on - how to get the number of rows/columns?
It seems there isn't a support of it. Sorry, the documentation is not available as well.
You will have to iterate on rows/columns (keeping in mind the max rows/columns the spreadsheet can hold) and check whether the cell contains any value.
See this link - http://web.archive.org/web/20110123164144/http://nayyeri.net/use-excelpackage-to-manipulate-open-xml-excel-files (original link dead)
获取行和列总数的最佳方法是使用以下方法:
The best way to get the total of rows and columns is with these methods:
Epplus 不支持usedrange,但您可以使用usedrange.cs 支持它
假设您已经下载了最新的EPPlus源代码,
对 Worksheet.cs 进行更改:使原始工作表部分。
然后创建名为UsedRange.cs的单独的cs文件,将以下代码粘贴到其中并编译。
Epplus does not have support for usedrange but you can have it using usedrange.cs
Assuming that you have downloaded latest EPPlus source code,
Make changes to Worksheet.cs : make the original one partial.
and then create separate cs file named UsedRange.cs paste the code below in it and compile.