简体   繁体   English

ASPOSE:如何正确获取工作表中的总行数

[英]ASPOSE: How to correctly get Total Rows in Worksheet

Currently using Aspose for .NET in a C# WebService.目前在 C# WebService 中使用 Aspose for .NET。 I am currently having issues getting the correct Total Rows using worksheet.Cells.Rows.Count It is showing that I have around 230 rows but inside the file, I only have 14 rows.我目前在使用 worksheet.Cells.Rows.Count 获取正确的总行数时遇到问题它显示我有大约 230 行但在文件内部,我只有 14 行。 I am guessing because I reuse my Excel file and deleted some rows by highlighting the rows then pressing the delete key.我猜是因为我重用了我的 Excel 文件并通过突出显示行然后按删除键删除了一些行。

When I recreate a brand new Excel file, then copy-paste the 14 values in the new file.当我重新创建一个全新的 Excel 文件时,然后将 14 个值复制粘贴到新文件中。 The worksheet.Cells.Rows.Count works correctly. worksheet.Cells.Rows.Count 工作正常。

Would this be a bug in the library or am I deleting the values in Excel file wrongly or I am using a wrong count property?这是库中的错误还是我错误地删除了 Excel 文件中的值或者我使用了错误的计数属性?

Am I missing something?我错过了什么吗?

TIA TIA

Well, RowCollection/ColumnCollection.Count attribute would give you total number of rows/columns which are initialized, so it won't be always reliable to get total number of data rows/cols.好吧,RowCollection/ColumnCollection.Count 属性将为您提供已初始化的行/列的总数,因此获取数据行/列的总数并不总是可靠的。 I think you should use Worksheet.Cells.MaxDataRow and Worksheet.Cells.MaxDataColumn properties to get the farthest (last) row/column indices (0 - based), see the sample code segment for your reference: eg Sample code:我认为您应该使用 Worksheet.Cells.MaxDataRow 和 Worksheet.Cells.MaxDataColumn 属性来获取最远(最后)行/列索引(基于 0),请参阅示例代码段以供参考:例如示例代码:

    Workbook workbook = new Workbook("Book1.xlsx");
     //Get the farthest (last) row's index (zero-based) which contains data.
     int lastRowIndex = workbook.Worksheets[0].Cells().MaxDataRow;

     //Get the farthest (last) column's index (zero-based) which contains data.
     int lastColIndex = workbook.Worksheets[0].Cells.MaxDataColumn;
......

Hope, this helps a bit.希望这个对你有帮助。

PS. PS。 I am working as Support developer/ Evangelist at Aspose.我在 Aspose 担任支持开发人员/传播者。

I used this but get wrong column count:我用过这个但得到错误的列数:

var dataTable = worksheet.Cells.ExportDataTable(0, 0,worksheet.Cells.Rows.Count, worksheet.Cells.Columns.Count, options);

Instead of worksheet.Cells.Rows.Count and worksheet.Cells.Rows.Count use these: worksheet.Cells.MaxRow + 1 and worksheet.Cells.MaxColumn + 1而不是 worksheet.Cells.Rows.Count 和 worksheet.Cells.Rows.Count 使用这些: worksheet.Cells.MaxRow + 1worksheet.Cells.MaxColumn + 1

This works correctly:这可以正常工作:

var dataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM