Skip to content

Commit 162f612

Browse files
authored
Merge pull request #502 from telerik/new-kb-handle-empty-cells-radspreadstreamprocessing-11d5d326b25f4ba695cd36428c20b603
Added new kb article handle-empty-cells-radspreadstreamprocessing
2 parents 81024e5 + 548d525 commit 162f612

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Handling Empty Cells with RadSpreadStreamProcessing
3+
description: This article demonstrates how to manage empty cells when importing an Excel file into a DataTable using RadSpreadStreamProcessing.
4+
type: how-to
5+
page_title: How to Handle Empty Cells with RadSpreadStreamProcessing
6+
slug: handle-empty-cells-radspreadstreamprocessing
7+
tags: spread, stream, document, processing, excel, xlsx, import, datatable, empty, cells
8+
res_type: kb
9+
ticketid: 1678225
10+
---
11+
<style> img[alt$="><"] { border: 1px solid lightgrey; } </style>
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| ---- | ---- | ---- |
17+
| 2025.1.205| RadSpreadStreamProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
When importing an Excel file into a [DataTable](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=net-9.0) using [RadSpreadStreamProcessing]({%slug radspreadstreamprocessing-overview%}), empty cells are skipped, resulting in misalignment of the data with the columns. This article explains how to correctly handle empty cells during the import process, ensuring data is accurately represented in the DataTable.
22+
23+
![Empty Cells in SpreadStreamProcessing ><](images/spread-stream-processing-empty-cells.png)
24+
25+
## Solution
26+
27+
To handle empty cells correctly and maintain the alignment of data with the columns in the DataTable, manually verify when a skip in the cells occurs. Calculate how many cells this skip is and insert the same amount of DBNull.Value entries in the DataTable. This approach ensures that the structure of the DataTable accurately reflects the structure of the Excel file, including the empty cells.
28+
29+
The following code snippet demonstrates how to implement this solution:
30+
31+
```csharp
32+
DataTable dt = new DataTable();
33+
string fileName = "Book1.xlsx";
34+
using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Open))
35+
{
36+
using (IWorkbookImporter workBookImporter = SpreadImporter.CreateWorkbookImporter(SpreadDocumentFormat.Xlsx, fs))
37+
{
38+
foreach (IWorksheetImporter worksheetImporter in workBookImporter.WorksheetImporters)
39+
{
40+
foreach (IRowImporter rowImporter in worksheetImporter.Rows)
41+
{
42+
if (rowImporter.RowIndex == 0)
43+
{
44+
foreach (ICellImporter cell in rowImporter.Cells)
45+
{
46+
dt.Columns.Add(cell.Value);
47+
}
48+
}
49+
else
50+
{
51+
var newRow = dt.NewRow();
52+
var cellIndex = 0;
53+
foreach (ICellImporter cell in rowImporter.Cells)
54+
{
55+
// Fill in DBNull.Value for skipped cells
56+
while (cellIndex < cell.ColumnIndex)
57+
{
58+
newRow[cellIndex] = DBNull.Value;
59+
cellIndex++;
60+
}
61+
newRow[cellIndex] = cell.Value;
62+
cellIndex++;
63+
}
64+
dt.Rows.Add(newRow);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
```
71+
72+
This code iterates through each cell in the imported rows. If a cell is skipped due to being empty, it inserts `DBNull.Value` for each skipped position before inserting the next non-empty cell's value. This method ensures that each cell, including the empty ones, is accounted for in the DataTable, preserving the data's alignment with the Excel file's structure.
73+
74+
## See Also
75+
76+
- [RadSpreadStreamProcessing]({%slug radspreadstreamprocessing-overview%})
77+
- [Import Excel File Formats with RadSpreadStreamProcessing]({%slug radspreadstreamprocessing-import%})
5.92 KB
Loading

libraries/radspreadstreamprocessing/import.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ Through the importer objects, you can access the properties of the different ele
6767

6868
* [Getting Started]({%slug radspreadstreamprocessing-getting-started%})
6969
* [Workbook]({%slug radspreadstreamprocessing-model-workbook%})
70+
* [Handling Empty Cells with RadSpreadStreamProcessing]({%slug handle-empty-cells-radspreadstreamprocessing%})

0 commit comments

Comments
 (0)