Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenXmlReader没有从excel.xlsx工作表VB Net中提取所有单元格内容_Excel_Vb.net_Xml Parsing_Openxml Sdk - Fatal编程技术网

OpenXmlReader没有从excel.xlsx工作表VB Net中提取所有单元格内容

OpenXmlReader没有从excel.xlsx工作表VB Net中提取所有单元格内容,excel,vb.net,xml-parsing,openxml-sdk,Excel,Vb.net,Xml Parsing,Openxml Sdk,OpenXmlReader没有从excel.xlsx工作表中提取所有单元格内容 我有一些源于SO并转换为VB Net的代码: 我的代码在工作表的前14行循环,没有任何问题,但随后将找不到任何向下的单元格 reader.ElementType似乎查找超过14行的行(如果reader.ElementType是GetType(行),则查找不到更多的单元格(GetType(单元格)) 最后提取的单元格是L14,但excel文件的单元格内容最多为L29 源代码: Public Shared Functio

OpenXmlReader没有从excel.xlsx工作表中提取所有单元格内容

我有一些源于SO并转换为VB Net的代码:

我的代码在工作表的前14行循环,没有任何问题,但随后将找不到任何向下的单元格

reader.ElementType似乎查找超过14行的行(如果reader.ElementType是GetType(行),则查找不到更多的单元格(GetType(单元格))

最后提取的单元格是L14,但excel文件的单元格内容最多为L29

源代码:

Public Shared Function fncParseXLSXorXLSM(strFileName As String, sbTxtFromFile As StringBuilder) As StringBuilder

    sbTxtFromFile.Length = 0
    Dim intFirst As Integer = 1

    Try

        Using spreadsheetDocument__1 As SpreadsheetDocument = SpreadsheetDocument.Open(strFileName, False)
            Dim workbookPart As WorkbookPart = spreadsheetDocument__1.WorkbookPart

            Dim sharedStringItemsArray As SharedStringItem() = workbookPart.SharedStringTablePart.SharedStringTable.Elements(Of SharedStringItem)().ToArray()

            Dim sheets As DocumentFormat.OpenXml.Spreadsheet.Sheets = spreadsheetDocument__1.WorkbookPart.Workbook.Sheets

            ' For each sheet, display the sheet information.
            For Each sheet As DocumentFormat.OpenXml.OpenXmlElement In sheets
                For Each attr As DocumentFormat.OpenXml.OpenXmlAttribute In sheet.GetAttributes()
                    Debug.Print("{0}: {1}", attr.LocalName, attr.Value)
                    If attr.LocalName = "state" And attr.Value = "hidden" Then
                        Debug.Print("{0}: {1}", attr.LocalName, attr.Value)
                    End If
                Next
            Next

            For Each worksheetPart As WorksheetPart In workbookPart.WorksheetParts

                Dim reader As OpenXmlReader = OpenXmlReader.Create(worksheetPart)

                While reader.Read()

                    If reader.ElementType Is GetType(Row) Then
                        reader.ReadFirstChild()
                        Do
                            If reader.ElementType Is GetType(Cell) Then
                                Dim c As Cell = DirectCast(reader.LoadCurrentElement(), Cell)
                                Debug.Print(c.CellReference.ToString)

                                '********************************************************************
                                ' Placed here to identify the last row openXmlReader seems to get to
                                '********************************************************************
                                If c.CellReference.ToString = "L14" Then
                                    Stop
                                End If
                                '********************************************************************
                                ' Placed here to identify the last row openXmlReader seems to get to
                                '********************************************************************

                                If c.DataType IsNot Nothing AndAlso c.DataType.Value.ToString = "SharedString" Then
                                    'Dim ssi As SharedStringItem = workbookPart.SharedStringTablePart.SharedStringTable.Elements(Of SharedStringItem)().ElementAt(Integer.Parse(c.CellValue.InnerText))
                                    Dim ssi As SharedStringItem = sharedStringItemsArray(Integer.Parse(c.CellValue.InnerText))

                                    If Not ssi.Text Is Nothing Then
                                        If Not ssi.Text.Text Is Nothing Then
                                            If intFirst = 1 Then
                                                sbTxtFromFile.Append(ssi.Text.Text)
                                                intFirst = 2
                                            Else
                                                sbTxtFromFile.Append(Environment.NewLine & ssi.Text.Text)
                                            End If
                                        End If
                                    Else
                                        If Not ssi.InnerText Is Nothing Then

                                            If ssi.InnerText Like "*Total of 25 CP TR Units*" Then
                                                Stop
                                            End If

                                            If intFirst = 1 Then
                                                sbTxtFromFile.Append(ssi.InnerText)
                                                intFirst = 2
                                            Else
                                                sbTxtFromFile.Append(Environment.NewLine & ssi.InnerText)
                                            End If
                                        End If
                                    End If
                                Else
                                    If Not c.CellValue Is Nothing Then
                                        If intFirst = 1 Then
                                            sbTxtFromFile.Append(c.CellValue.InnerText)
                                            intFirst = 2
                                        Else
                                            sbTxtFromFile.Append(Environment.NewLine & c.CellValue.InnerText)
                                        End If
                                    End If
                                End If
                            End If
                        Loop While reader.ReadNextSibling()
                    End If
                    If sbTxtFromFile.Length > 0 Then
                        sbTxtFromFile.Append(Environment.NewLine)
                    End If
                End While
            Next
        End Using

        Return sbTxtFromFile

    Catch ex As Exception
        If ex.Message Like "The process cannot access the file '*" Then 'File in use
            sbTxtFromFile.Append("|11readonly11|")
        End If
        'MsgBox(ex.ToString)
    End Try

End Function
以下是指向.xlsx文件的链接:


虽然不是我想要的答案(我相信我仍然可以使用我的原始代码返回所有单元格),但我已通过删除外部的“IF”语句解决了问题,该语句正在查找行,读取第一个子项并遍历所有的同级项。 代码现在只查找作为单元格的元素并返回值

'Previous Code

While reader.Read()
    If reader.ElementType Is GetType(Row) Then
        reader.ReadFirstChild()
            Do
                If reader.ElementType Is GetType(Cell) Then
                    'Code Here
                End If
           Loop While reader.ReadNextSibling()
    End If
End While


'New Code   
While reader.Read()
    'If reader.ElementType Is GetType(Row) Then
    'reader.ReadFirstChild()
    'Do
    If reader.ElementType Is GetType(Cell) Then
        Do
            'Code Here
        Loop While reader.ReadNextSibling()
    End If
    'Loop While reader.ReadNextSibling()
    'End If
End While

虽然不是我想要的答案(我相信我仍然能够使用我的原始代码返回所有单元格),但我已经通过删除外部的“IF”语句解决了我的问题,该语句正在查找行,读取第一个孩子并循环遍历所有兄弟姐妹。 代码现在只查找作为单元格的元素并返回值

'Previous Code

While reader.Read()
    If reader.ElementType Is GetType(Row) Then
        reader.ReadFirstChild()
            Do
                If reader.ElementType Is GetType(Cell) Then
                    'Code Here
                End If
           Loop While reader.ReadNextSibling()
    End If
End While


'New Code   
While reader.Read()
    'If reader.ElementType Is GetType(Row) Then
    'reader.ReadFirstChild()
    'Do
    If reader.ElementType Is GetType(Cell) Then
        Do
            'Code Here
        Loop While reader.ReadNextSibling()
    End If
    'Loop While reader.ReadNextSibling()
    'End If
End While