以编程方式获取总数.使用C#在Excel表中具有数据的行[英] Programmatically getting the total no. of rows having data in excel sheet using C#

本文是小编为大家收集整理的关于以编程方式获取总数.使用C#在Excel表中具有数据的行的处理方法,想解了以编程方式获取总数.使用C#在Excel表中具有数据的行的问题怎么解决?以编程方式获取总数.使用C#在Excel表中具有数据的行问题的解决办法?以编程方式获取总数.使用C#在Excel表中具有数据的行问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我的是C#控制台应用程序.我正在尝试使用c#在Epplus库的引用的情况下,以编程为excel表的数据来获取excel表的数据.我想阅读使用for Loop的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

这将返回带有数据的最后一行的数字.比您可以简单地循环直到这个数字.

显然,如果您在Yor工作表末尾有像评论的垃圾,这将无效,而不是更多的行.

本文地址:https://www.itbaoku.cn/post/2352616.html

问题描述

Mine is C# console application. I am trying to get the count of rows which are having data of an excel sheet programmatically, using C# with the reference of EPPlus Library. I want to read all the records of an excel spreadsheet using for loop. So I need to specify the upper limit of that loop.

code i used:-

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>"); } }

Now am statically mentioning that the code must retrieve 7 rows [for (int row = 2; row < 7; row++)]. Here the 7 must be calculated dynamically (during the run-time).

Kindly popup your points here to work it out. Thanks in advance.

推荐答案

You could do something like this:

var rowCount =  worksheet .Dimension.End.Row

This returns the number of the last row with data. Than you can simply loop until this number.

Obviously, this will not work if you have garbage like comments at the end of yor sheet, than there will more rows.

查看更多
相关标签/搜索