How can I read rows by group using Aspose? example
Asked
Active
Viewed 341 times
2 Answers
0
See the following sample code using Aspose.Cells for your reference:
e.g.
Sample code:
//Loading the file
Workbook book = new Workbook("e:\\test2\\Bk_readgrouped.xlsx");
//Get the first worksheet in the workbook
Worksheet sheet = book.Worksheets[0];
int maxRow = sheet.Cells.MaxDataRow;
int maxCol = sheet.Cells.MaxDataColumn;
int chk = 0;
bool pname = false;
Console.WriteLine("Retrieving each group's data");
for (int i = 0; i <= maxRow; i++)
{
int rowOutlineLevel = sheet.Cells.GetGroupedRowOutlineLevel(i);
if (rowOutlineLevel > 0)
{
pname = true;
if (pname== true & chk != rowOutlineLevel)
{
Console.WriteLine("\n");
Console.WriteLine("Group:" + rowOutlineLevel);
pname = false;
chk = rowOutlineLevel;
}
for (int j = 0; j <= maxCol; j++)
{
Console.Write(sheet.Cells[i, j].StringValue + "\t");
}
Console.WriteLine();
}
}
Hope, this helps a bit.
PS. I am working as Support developer/ Evangelist at Aspose.

Amjad Sahi
- 1,813
- 1
- 10
- 15
-
Thx, but this is not I need. I want to get "group", i.e. rows 1-3 belongs to group "1". Is it possible? – L Brg Jan 19 '21 at 05:59
-
I have enhanced the sample code (above), so you may check it now. Hope, this helps a bit. – Amjad Sahi Jan 20 '21 at 12:18
0
This works for me:
public virtual void ProcessExcelSheetTree (Worksheet Excelsheet, int current_level)
{
int current_row = 1;
int maxRow = Excelsheet.Cells.MaxDataRow;
int firstGroupIndex = 0;
while(Excelsheet.Cells[current_row, 0].StringValue != "")
{
var currentRow = Excelsheet.Cells.Rows[current_row];
if(currentRow.GroupLevel == current_level)
{
string targetName = Excelsheet.Cells[current_row, 0].StringValue;
Console.WriteLine(targetName);
firstGroupIndex = currentRow.Index;
for (int i = firstGroupIndex + 1; i <= maxRow; i++)
{
if(Excelsheet.Cells.Rows[i].GroupLevel > current_level)
{
string subTargetName = Excelsheet.Cells[i, 0].StringValue;
Console.WriteLine(" - " + subTargetName);
}
else
{
break;
}
}
}
current_row++;
}
}
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 14 '22 at 00:18