Skip to content

Commit 3f9d914

Browse files
author
KB Bot
committed
Added new kb article grouping-data-using-radspreadprocessing
1 parent a581d14 commit 3f9d914

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
title: Grouping Data Example Using RadSpreadProcessing
3+
description: Learn how to group data in a worksheet using RadSpreadProcessing for Document Processing.
4+
type: how-to
5+
page_title: How to Group Data in RadSpreadProcessing
6+
meta_title: How to Group Data in RadSpreadProcessing
7+
slug: grouping-data-using-radspreadprocessing
8+
tags: document, processing, group, column, excel, spread
9+
res_type: kb
10+
ticketid: 1692310
11+
---
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| ---- | ---- | ---- |
17+
| 2025.2.520| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
This article shows how to generate a worksheet with grouped data having a flat collection of records as an input.
22+
23+
## Solution
24+
25+
To group data in a worksheet, follow these steps:
26+
27+
### Preparing Grouped Data
28+
29+
1. Perform grouping on your flat data before populating the worksheet.
30+
2. Use LINQ or other methods to group rows based on a specific column.
31+
32+
Use the following code snippet to group rows in a worksheet:
33+
34+
```csharp
35+
internal class Program
36+
{
37+
static void Main(string[] args)
38+
{
39+
string fileName = "sample.csv";
40+
Workbook workbook;
41+
42+
CsvFormatProvider csv_formatProvider = new CsvFormatProvider();
43+
44+
using (Stream input = new FileStream(fileName, FileMode.Open))
45+
{
46+
workbook = csv_formatProvider.Import(input, TimeSpan.FromSeconds(10));
47+
48+
}
49+
Worksheet worksheet = workbook.ActiveWorksheet;
50+
51+
// Convert worksheet data to DataTable
52+
DataTable dataTable = WorksheetToDataTable(worksheet);
53+
54+
// Apply grouping and outlining
55+
Workbook groupedWorkbook= ApplyGroupingToWorksheet(dataTable, new Workbook());
56+
57+
58+
59+
Telerik.Windows.Documents.Spreadsheet.FormatProviders.IWorkbookFormatProvider xlsx_formatProvider =
60+
new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();
61+
62+
string outputFilePath = "output.xlsx";
63+
File.Delete(outputFilePath);
64+
using (Stream output = new FileStream(outputFilePath, FileMode.Create))
65+
{
66+
xlsx_formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
67+
}
68+
69+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
70+
71+
outputFilePath = "groupedOutput.xlsx";
72+
File.Delete(outputFilePath);
73+
using (Stream output = new FileStream(outputFilePath, FileMode.Create))
74+
{
75+
xlsx_formatProvider.Export(groupedWorkbook, output, TimeSpan.FromSeconds(10));
76+
}
77+
78+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
79+
}
80+
static DataTable WorksheetToDataTable(Worksheet worksheet)
81+
{
82+
DataTable dataTable = new DataTable();
83+
84+
// Get used range
85+
CellRange usedRange = worksheet.UsedCellRange;
86+
87+
// Add columns to DataTable (assuming first row contains headers)
88+
for (int col = 0; col < usedRange.ColumnCount; col++)
89+
{
90+
string columnName = worksheet.Cells[0, col].GetValue().Value.RawValue.ToString();
91+
dataTable.Columns.Add(columnName ?? $"Column{col}");
92+
}
93+
94+
// Add data rows
95+
for (int row = 1; row < usedRange.RowCount; row++)
96+
{
97+
DataRow dataRow = dataTable.NewRow();
98+
for (int col = 0; col < usedRange.ColumnCount; col++)
99+
{
100+
dataRow[col] = worksheet.Cells[row, col].GetValue().Value.RawValue ?? string.Empty;
101+
}
102+
dataTable.Rows.Add(dataRow);
103+
}
104+
105+
return dataTable;
106+
}
107+
108+
static Workbook ApplyGroupingToWorksheet(DataTable dataTable, Workbook workbook)
109+
{
110+
// Group the data by the sixth column (index 5)
111+
var groupedResults = from p in dataTable.AsEnumerable()
112+
group p by p.Field<string>("categoryID") into g
113+
select new
114+
{
115+
categoryID = g.Key,
116+
productID = string.Join(";", from i in g select i.Field<string>("productID")),
117+
productName = string.Join(";", from i in g select i.Field<string>("productName")),
118+
quantityPerUnit = string.Join(";", from i in g select i.Field<string>("quantityPerUnit")),
119+
CustomunitPriceerLastname = string.Join(";", from i in g select i.Field<string>("unitPrice")),
120+
unitsInStock = string.Join(";", from i in g select i.Field<string>("unitsInStock")),
121+
unitsOnOrder = string.Join(";", from i in g select i.Field<string>("unitsOnOrder"))
122+
};
123+
124+
125+
workbook.Worksheets.Add();
126+
Worksheet worksheet = workbook.ActiveWorksheet;
127+
worksheet.Cells[0, 1].SetValue("productID");
128+
worksheet.Cells[0, 2].SetValue("productName");
129+
worksheet.Cells[0, 3].SetValue("quantityPerUnit");
130+
worksheet.Cells[0, 4].SetValue("unitPrice");
131+
// Header is row 0, data starts at row 1
132+
int currentRow = 1;
133+
134+
// Process each group
135+
foreach (var group in groupedResults)
136+
{
137+
worksheet.Cells[currentRow, 1].SetValue("CategoryID "+group.categoryID);
138+
currentRow++;
139+
140+
string[] productsIDs= group.productID.Split(';');
141+
string[] productNames = group.productName.Split(';');
142+
string[] quantityPerUnits = group.quantityPerUnit.Split(';');
143+
string[] unitPrices = group.CustomunitPriceerLastname.Split(';');
144+
string[] unitsInStocks = group.unitsInStock.Split(';');
145+
string[] unitsOnOrders = group.unitsOnOrder.Split(';');
146+
147+
int start = currentRow;
148+
int end= currentRow+ productsIDs.Length - 1;
149+
for (int i = 0; i < productsIDs.Length; i++)
150+
{
151+
worksheet.Cells[currentRow, 1].SetValue(productsIDs[i]);
152+
worksheet.Cells[currentRow, 2].SetValue(productNames[i]);
153+
worksheet.Cells[currentRow, 3].SetValue(quantityPerUnits[i]);
154+
worksheet.Cells[currentRow, 4].SetValue(unitPrices[i]);
155+
worksheet.Cells[currentRow, 5].SetValue(unitsInStocks[i]);
156+
worksheet.Cells[currentRow, 6].SetValue(unitsOnOrders[i]);
157+
currentRow++;
158+
}
159+
worksheet.Rows[start, end].Group();
160+
}
161+
return workbook;
162+
}
163+
}
164+
```
165+
166+
167+
168+
Modify the example to suit your specific data structure and requirements.
169+
170+
## See Also
171+
172+
- [RadSpreadStreamProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/overview)
173+
- [RadSpreadStreamProcessing API Reference](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.spreadsheet.model)
174+
- [Document Processing Libraries Overview](https://docs.telerik.com/devtools/document-processing/introduction)

0 commit comments

Comments
 (0)