@@ -79,171 +79,164 @@ To customize the cell format of the exported file before it reaches the client:
79
79
</GridExport>
80
80
81
81
<GridColumns>
82
- <GridColumn Field="@nameof(SampleData.ProductId )" Title="ID" Width="100px " />
83
- <GridColumn Field="@nameof(SampleData.ProductName )" Title="Product Name" Width="300px " />
84
- <GridColumn Field="@nameof(SampleData.UnitsInStock )" Title="In stock" Width="100px " />
85
- <GridColumn Field="@nameof(SampleData.Price )" Title="Unit Price" Width="200px " />
86
- <GridColumn Field="@nameof(SampleData.Discontinued )" Title="Discontinued" Width="100px " />
87
- <GridColumn Field="@nameof(SampleData.FirstReleaseDate )" Title="Release Date" Width="300px " />
82
+ <GridColumn Field="@nameof(Product.Id )" Title="ID" Width="60px " />
83
+ <GridColumn Field="@nameof(Product.Name )" Title="Product Name" Width="180px " />
84
+ <GridColumn Field="@nameof(Product.Price )" Width="160px " />
85
+ <GridColumn Field="@nameof(Product.Quantity )" Width="120px " />
86
+ <GridColumn Field="@nameof(Product.ReleaseDate )" Title="Release Date" DisplayFormat="{0:d}" Width="240px " />
87
+ <GridColumn Field="@nameof(Product.Discontinued )" Width="160px " />
88
88
</GridColumns>
89
89
</TelerikGrid>
90
90
91
91
@code {
92
- private List<SampleData > GridData { get; set; }
92
+ private List<Product > GridData { get; set; } = new();
93
93
94
94
private bool ExportAllPages { get; set; }
95
95
96
- private async Task OnExcelAfterExport(GridAfterExcelExportEventArgs args)
96
+ private void OnExcelAfterExport(GridAfterExcelExportEventArgs args)
97
97
{
98
- var bytes = args.Stream.ToArray();
98
+ byte[] bytes = args.Stream.ToArray();
99
99
100
- var excelStream = new MemoryStream(bytes);
100
+ using MemoryStream importStream = new MemoryStream(bytes);
101
101
102
- var export = new MemoryStream();
102
+ using MemoryStream exportStream = new MemoryStream();
103
103
104
- //define the desired styling - https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/cell-styles
105
- SpreadCellFormat markedCell = new SpreadCellFormat()
106
- {
107
- Fill = SpreadPatternFill.CreateSolidFill(new SpreadColor(230, 238, 223)),
108
- IsBold = true
109
- };
104
+ // Define the desired styling.
105
+ // https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/cell-styles
106
+ SpreadCellFormat customCellFormat = new SpreadCellFormat()
107
+ {
108
+ Fill = SpreadPatternFill.CreateSolidFill(new SpreadColor(230, 238, 223)),
109
+ IsBold = true
110
+ };
111
+
112
+ // Import the stream to modify it and then export it.
113
+ // https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/model/workbook
114
+ using IWorkbookImporter workBookImporter = SpreadImporter.CreateWorkbookImporter(SpreadDocumentFormat.Xlsx, importStream);
110
115
111
- //import the stream to modify it and then export it -
112
- //see https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/model/workbook
113
- using (excelStream)
116
+ using IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, exportStream);
117
+
118
+ // Loop through the sheets to copy their content.
119
+ foreach (IWorksheetImporter worksheetImporter in workBookImporter.WorksheetImporters)
114
120
{
115
- using (IWorkbookImporter workBookImporter = SpreadImporter.CreateWorkbookImporter(SpreadDocumentFormat.Xlsx, excelStream))
121
+ using IWorksheetExporter worksheetExporter = workbookExporter.CreateWorksheetExporter(worksheetImporter.Name);
122
+
123
+ // Set fixed width to all columns.
124
+ // If you need to autofit the content, use the CellContentSizeHelper:
125
+ //https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/text-measuring
126
+ for (int i = 0; i < 6; i++)
127
+ {
128
+ IColumnExporter column = worksheetExporter.CreateColumnExporter();
129
+
130
+ column.SetWidthInPixels(160);
131
+
132
+ column.Dispose();
133
+ }
134
+
135
+ int lastRow = 0;
116
136
117
- using (IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, export))
137
+ // Loop through the rows to copy their content.
138
+ foreach (IRowImporter rowImporter in worksheetImporter.Rows)
118
139
{
119
- //loop through the sheets to copy their content
120
- foreach (IWorksheetImporter worksheetImporter in workBookImporter.WorksheetImporters)
140
+ int rowDifference = rowImporter.RowIndex - lastRow;
141
+
142
+ // Ensure proper row order in case there are any empty rows.
143
+ if (rowDifference > 1)
121
144
{
122
- using (var worksheetExporter = workbookExporter.CreateWorksheetExporter(worksheetImporter.Name))
145
+ worksheetExporter.SkipRows(rowDifference - 1);
146
+ }
147
+
148
+ int lastColumn = 0;
149
+
150
+ using IRowExporter rowExporter = worksheetExporter.CreateRowExporter();
151
+
152
+ // Loop through the cells to copy their content.
153
+ foreach (ICellImporter cellImporter in rowImporter.Cells)
154
+ {
155
+ string value = cellImporter.Value;
156
+
157
+ if (string.IsNullOrEmpty(value))
123
158
{
124
- //this sets fixed width to all columns.
125
- //If you need to autofit the content, use the CellContentSizeHelper -
126
- //https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/text-measuring
127
- for (int i = 0; i < 5; i++)
128
- {
129
- var column = worksheetExporter.CreateColumnExporter();
130
-
131
- column.SetWidthInPixels(100);
132
-
133
- column.Dispose();
134
- }
135
-
136
- int lastRow = 0;
137
-
138
- //loop through the rows to copy their content
139
- foreach (IRowImporter rowImporter in worksheetImporter.Rows)
140
- {
141
- int rowDifference = rowImporter.RowIndex - lastRow;
142
-
143
- //this part ensures the proper cells order in case there are any empty rows
144
- if (rowDifference > 1)
145
- {
146
- worksheetExporter.SkipRows(rowDifference - 1);
147
- }
148
-
149
- int lastColumn = 0;
150
-
151
- using (var rowExporter = worksheetExporter.CreateRowExporter())
152
- {
153
- //loop through the cells to copy their content
154
- foreach (ICellImporter cellImporter in rowImporter.Cells)
155
- {
156
- string value = cellImporter.Value;
157
-
158
- if (string.IsNullOrEmpty(value))
159
- {
160
- continue;
161
- }
162
-
163
- var valueType = cellImporter.ValueType;
164
-
165
-
166
- var importedFormat = new SpreadCellFormat();
167
-
168
- if (cellImporter.Format != null)
169
- {
170
- importedFormat = cellImporter.Format;
171
- }
172
-
173
-
174
- int cellDifference = cellImporter.ColumnIndex - lastColumn;
175
-
176
- //this part ensures the proper cells order in case there are any empty cells
177
- if (cellDifference > 1)
178
- {
179
- rowExporter.SkipCells(cellDifference - 1);
180
- }
181
-
182
- //check the cell value to apply proper formatting
183
- using (var cellExporter = rowExporter.CreateCellExporter())
184
- {
185
- switch (valueType)
186
- {
187
- case CellValueType.Boolean:
188
- var boolValue = bool.Parse(value);
189
- cellExporter.SetValue(boolValue);
190
- break;
191
- case CellValueType.Number:
192
- var dateNumberValue = double.Parse(value);
193
- cellExporter.SetFormat(importedFormat);
194
- cellExporter.SetValue(dateNumberValue);
195
- break;
196
- case CellValueType.Error:
197
- case CellValueType.Text:
198
- case CellValueType.SharedString:
199
- case CellValueType.RichText:
200
- cellExporter.SetValue(value);
201
- break;
202
- }
203
-
204
- //apply the defined style to the desired cells
205
- if (cellImporter.RowIndex == 0)
206
- {
207
- cellExporter.SetFormat(markedCell);
208
- }
209
- }
210
-
211
- lastColumn = cellImporter.ColumnIndex;
212
- }
213
- }
214
-
215
- lastRow = rowImporter.RowIndex;
216
- }
159
+ continue;
217
160
}
161
+
162
+ CellValueType valueType = cellImporter.ValueType;
163
+
164
+ SpreadCellFormat importedFormat = new SpreadCellFormat();
165
+
166
+ if (cellImporter.Format != null)
167
+ {
168
+ importedFormat = cellImporter.Format;
169
+ }
170
+
171
+ int cellDifference = cellImporter.ColumnIndex - lastColumn;
172
+
173
+ // Ensure proper cell order in case there are any empty cells.
174
+ if (cellDifference > 1)
175
+ {
176
+ rowExporter.SkipCells(cellDifference - 1);
177
+ }
178
+
179
+ // Check the cell value to apply proper formatting.
180
+ using ICellExporter cellExporter = rowExporter.CreateCellExporter();
181
+
182
+ switch (valueType)
183
+ {
184
+ case CellValueType.Boolean:
185
+ bool boolValue = bool.Parse(value);
186
+ cellExporter.SetValue(boolValue);
187
+ break;
188
+ case CellValueType.Number:
189
+ double dateNumberValue = double.Parse(value);
190
+ cellExporter.SetFormat(importedFormat);
191
+ cellExporter.SetValue(dateNumberValue);
192
+ break;
193
+ case CellValueType.Error:
194
+ case CellValueType.Text:
195
+ case CellValueType.SharedString:
196
+ case CellValueType.RichText:
197
+ cellExporter.SetValue(value);
198
+ break;
199
+ }
200
+
201
+ // Apply the defined style to the desired cells.
202
+ if (cellImporter.RowIndex == 0)
203
+ {
204
+ cellExporter.SetFormat(customCellFormat);
205
+ }
206
+
207
+ lastColumn = cellImporter.ColumnIndex;
218
208
}
209
+
210
+ lastRow = rowImporter.RowIndex;
219
211
}
220
212
}
221
213
222
- //pass the modified stream to the event arguments
223
- args.Stream = export ;
214
+ // Pass the modified stream to the GridAfterExcelExportEventArgs event argument.
215
+ args.Stream = exportStream ;
224
216
}
225
217
226
218
protected override void OnInitialized()
227
219
{
228
- GridData = Enumerable.Range(1, 100).Select(x => new SampleData
229
- {
230
- ProductId = x,
231
- ProductName = "Product " + x,
232
- UnitsInStock = x * 2,
233
- Price = 3.14159m * x,
234
- Discontinued = x % 4 == 0,
235
- FirstReleaseDate = DateTime.Now.AddDays(-x)
236
- }).ToList();
220
+ Random rnd = Random.Shared;
221
+ GridData = Enumerable.Range(1, 100).Select(x => new Product
222
+ {
223
+ Id = x,
224
+ Name = $"Product {x}",
225
+ Price = rnd.Next(1, 100) * 1.23m,
226
+ Quantity = rnd.Next(0, 100),
227
+ ReleaseDate = DateTime.Now.AddDays(-rnd.Next(1, 1000)),
228
+ Discontinued = x % 4 == 0
229
+ }).ToList();
237
230
}
238
231
239
- public class SampleData
232
+ public class Product
240
233
{
241
- public int ProductId { get; set; }
242
- public string ProductName { get; set; }
243
- public int UnitsInStock { get; set; }
234
+ public int Id { get; set; }
235
+ public string Name { get; set; } = string.Empty;
244
236
public decimal Price { get; set; }
237
+ public int Quantity { get; set; }
238
+ public DateTime ReleaseDate { get; set; }
245
239
public bool Discontinued { get; set; }
246
- public DateTime FirstReleaseDate { get; set; }
247
240
}
248
241
}
249
242
````
0 commit comments