[英]Programmatically getting the total no. of rows having data in excel sheet using C#
我的是C#控制台应用程序。 我正在尝试使用C#和EPPlus库的引用以编程方式获取具有excel工作表数据的行数。 我想使用for循环读取excel电子表格的所有记录。 因此,我需要指定该循环的上限。
我使用的代码:-
FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
// get the second worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[2];
// output the data in column 2
StringBuilder sb = new StringBuilder();
for (int row = 2; row < 7; row++)
{
sb.Append("<tr>");
for (int col = 2; col < 11; col++)
{
if (col == 7)
sb.Append("<td valign='top' border='1'><a href='https://na7.salesforce.com/'" + worksheet.Cells[row, col].Value + ">" + "https://na7.salesforce.com/" + worksheet.Cells[row, col].Value + "</td>");
else
sb.Append("<td valign='top' border='1'>" + worksheet.Cells[row, col].Value + "</td>");
}
sb.Append("</tr>");
}
}
现在静态地提到代码必须检索7行[for(int row = 2; row <7; row ++)]。 在此,必须动态计算7(在运行期间)。
请在这里弹出您的观点以解决。 提前致谢。
您可以执行以下操作:
var rowCount = worksheet .Dimension.End.Row
这将返回带有数据的最后一行的编号。 比起简单地循环直到这个数字。
显然,如果您在工作表末尾有类似注释的垃圾内容,那么它将不起作用,因为它将有更多的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.