且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB.net excel合并单元格范围

更新时间:2023-12-03 11:57:10

感谢 J. Chomel 的评论.我可以自己回答这个问题.这个链接向我展示了我需要的所有信息.

Thanks to the comment by J. Chomel. I could answer this question by myself. This Link showed me all the information I needed.

谢谢!

    Public Sub GetRatings()

    Dim oExcelLoc As String = "J:\EXCEL - Lijsten\NOZZLE SIZES.xlsx"

    ' Excel load data
    Dim oExcelApp As New Application
    Dim oWorkBook As Workbook
    Dim oWorkSheet As Worksheet

    oWorkBook = oExcelApp.Workbooks.Open(oExcelLoc, False, True)
    oWorkSheet = oWorkBook.Worksheets(1)


    For oRow As Integer = 1 To 100
        If oWorkSheet.Range("A" & oRow).Value = GroupString Then

            Dim oRange As Range
            oRange = oWorkSheet.Range("A" & oRow)

            ' Check if merge cells
            If oRange.MergeCells Then

                Debug.Print("Group start row = " & oRow & " |Group end row = " & oRow + oRange.MergeArea.Rows.Count - 1)

            Else
                MsgBox("Error")
            End If
            Exit For
        End If
    Next


    oWorkBook.Close()

    'Release object references.
    releaseObject(oWorkSheet)
    releaseObject(oWorkBook)
    releaseObject(oExcelApp)

End Sub

Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub