|
| 1 | +--- |
| 2 | +title: Handling Empty Cells with RadSpreadStreamProcessing During Excel Import |
| 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 in Excel Import |
| 6 | +slug: handle-empty-cells-radspreadstreamprocessing |
| 7 | +tags: radspreadstreamprocessing, document processing, excel, xlsx, import, datatable, empty cells |
| 8 | +res_type: kb |
| 9 | +ticketid: 1678225 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadSpreadStreamProcessing for Document Processing</td> |
| 19 | +</tr> |
| 20 | +</tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +When importing an Excel file into a DataTable using RadSpreadStreamProcessing, 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. |
| 26 | + |
| 27 | +This knowledge base article also answers the following questions: |
| 28 | +- How to import an Excel file into a DataTable with empty cells using RadSpreadStreamProcessing? |
| 29 | +- How to ensure data integrity when empty cells are present in an Excel import? |
| 30 | +- What approach can be used to include empty cells during Excel file import into a DataTable? |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +The following code snippet demonstrates how to implement this solution: |
| 37 | + |
| 38 | +```csharp |
| 39 | +DataTable dt = new DataTable(); |
| 40 | +string fileName = "Book1.xlsx"; |
| 41 | +using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Open)) |
| 42 | +{ |
| 43 | + using (IWorkbookImporter workBookImporter = SpreadImporter.CreateWorkbookImporter(SpreadDocumentFormat.Xlsx, fs)) |
| 44 | + { |
| 45 | + foreach (IWorksheetImporter worksheetImporter in workBookImporter.WorksheetImporters) |
| 46 | + { |
| 47 | + foreach (IRowImporter rowImporter in worksheetImporter.Rows) |
| 48 | + { |
| 49 | + if (rowImporter.RowIndex == 0) |
| 50 | + { |
| 51 | + foreach (ICellImporter cell in rowImporter.Cells) |
| 52 | + { |
| 53 | + dt.Columns.Add(cell.Value); |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + var newRow = dt.NewRow(); |
| 59 | + var cellIndex = 0; |
| 60 | + foreach (ICellImporter cell in rowImporter.Cells) |
| 61 | + { |
| 62 | + // Fill in DBNull.Value for skipped cells |
| 63 | + while (cellIndex < cell.ColumnIndex) |
| 64 | + { |
| 65 | + newRow[cellIndex] = DBNull.Value; |
| 66 | + cellIndex++; |
| 67 | + } |
| 68 | + newRow[cellIndex] = cell.Value; |
| 69 | + cellIndex++; |
| 70 | + } |
| 71 | + dt.Rows.Add(newRow); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +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. |
| 80 | + |
| 81 | +## See Also |
| 82 | + |
| 83 | +- [RadSpreadStreamProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/overview) |
| 84 | +- [Import and Export to Excel File Formats](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/import-and-export-to-excel-file-formats/xls/xlsformatprovider) |
| 85 | +- [Using DataTableFormatProvider](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/data-table/using-data-table-format-provider#export) |
0 commit comments