Search code examples
asp.netvb.netepplus

Merged Cells not being skipped in Excel worksheet


I am trying to upload data from an Excel worksheet which has some merged cells.

This is the code in question.

            Using excel = New ExcelPackage(ulExcelData.PostedFile.InputStream)
                Dim _worksheet = excel.Workbook.Worksheets.First()
                Dim _hasHeader = False
                Dim _mergedAddress = _worksheet.MergedCells(13, 7)
                Dim mergedadress = _worksheet.MergedCells(1, 2)

                For Each col In _worksheet.Cells(13, 7, _worksheet.Dimension.End.Row, 10)

                    _dataTable.Columns.Add(If(_hasHeader, Nothing, col.Value))
                    String.Format("{0}", col.Start.Column)
                Next


                For _rowNumber = 1 To _worksheet.Dimension.End.Row
                    Dim _worksheetRow = _worksheet.Cells(_rowNumber, 13, _rowNumber, 10)
                    Dim _row As DataRow = _dataTable.Rows.Add()

                    For Each cell In _worksheetRow
                        If cell.Value Is Nothing Then
                            _row.Delete()
                            GoTo executeInsert
                        End If
                    Next cell
                Next

I tried this: Get Merged Cell Area with EPPLus but no luck.

The columns I want to read are from "F" to "J" but "G", "H" and "I" are merged, so here I want the value that's in "G" and I want to ignore "H" and "I".

Any ideas?

I'll be happy to add any clarifications.

UPDATE

All saves as per normal apart from column J. I hit "H" and I save null values to my database as "H" and "I" are empty cells.

UPDATED CODE

                        For _rowNumber = 13 To _worksheet.Dimension.End.Row
                            Dim _worksheetRow = _worksheet.Cells(_rowNumber, 6, _rowNumber, 9) 'Cols F, G, H, I'
                            Dim _row As DataRow = _dataTable.Rows.Add()

                            For Each cell In _worksheetRow
                                If cell.Value Is Nothing Then
                                    Continue For
                                Else
                                    _row(cell.Start.Column - 6) = cell.Value.ToString.Trim()
                                End If
                            Next cell
                        Next

I'm getting values from "G" but I am unable to "skip" "H" and "I" to go straight to "J" and get the value. So basically from columns 6 to 10 I want to skip 8 and 9 and go straight to 10.


Solution

  • Finally, there was no need to try and "skip anything". I deleted the extra two columns that were merged along with "G" since they didn't contain any values and all worked fine. I simply added this line into my code.

    _worksheet.DeleteColumn(8, 2)
    

    Final Code Portion

    Using excel = New ExcelPackage(ulExcelData.PostedFile.InputStream)
                            Dim _worksheet = excel.Workbook.Worksheets.First()
                            Dim _hasHeader As Boolean = False
    
                            _worksheet.DeleteColumn(8, 2)
    
                            For Each col In _worksheet.Cells("F13:J13")
                                If col.Value IsNot Nothing Then
                                    _dataTable.Columns.Add()
                                    String.Format("{0}", col.Start.Column)
                                Else
                                    Continue For
                                End If
                            Next