防止Reader(OpenXMLReader SAX)每1026个字符打破字符串块

时间:2016-09-20 13:50:12

标签: vb.net excel openxml stringbuilder xlsx

我正在使用从StackOverflow中获取的一些优秀代码并将其转换为VB NET:

  • 从excel .xlsx文件中的所有speadsheets中提取所有文本行
  • 将每一行追加到StringBuilder。

代码运行得很好,但我的stringbuilder条目每1026个字符分割一次。 即文字读取 ..这是Sta
ckOverflow .. 而不是左'..这是StackOverflow ..'并且在自然的分行中分裂。

Imports System.Text
Imports System.Linq
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Public Class clsParseXLS

    Public Shared Sub parseXLSX(strFileName As String, sbTxtFromFile As StringBuilder)

        Dim intFirst As Integer = 1

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

                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)

                                    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))

                                        If Not ssi Is Nothing Then
                                            If intFirst = 1 Then
                                                sbTxtFromFile.Append(ssi.Text.Text)
                                                intFirst = 2
                                            Else
                                                sbTxtFromFile.Append(Environment.NewLine & reader.GetText())
                                                intFirst = 2
                                            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)
                                                intFirst = 2
                                            End If
                                        End If
                                    End If

                                    'Console.Out.Write("{0}: {1} ", c.CellReference, cellValue)
                                End If
                            Loop While reader.ReadNextSibling()
                            'Console.Out.WriteLine()
                        End If
                    End While
                Next
            End Using

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

End Class

有谁知道我可以做些什么来纠正这种情况并拆分行?

非常感谢任何人提供的任何帮助。

非常感谢@Hans发布我正在使用的原始代码。 Using OpenXmlReader

1 个答案:

答案 0 :(得分:0)

必须在StringBuilder重置其缓冲区时,是否必须使用String Builder?