Как оптимизировать вставку комментариев, чтобы преуспеть с помощью openxml - PullRequest
0 голосов
/ 19 октября 2018

прилагается мой код для вставки комментариев в Excel.Требуется около 3 минут, чтобы пройти около 500 строк.

Словарь, имеющий ссылку на ячейку и комментарий, который будет добавлен в ячейку, создается и циклически проходит для достижения функциональности на данный момент.

Есть ли способ оптимизировать мой код?Например, используя параллель .foeach?Если да, пожалуйста, укажите способ, которым я могу это сделать.

Public Sub InsertComments(ByVal worksheetPart As WorksheetPart, ByVal commentsToAddDict As Dictionary(Of String, String))
        If commentsToAddDict.Any() Then
            Dim commentsVmlXml As String = String.Empty

            For Each commentToAdd In commentsToAddDict
                Dim colLetters As String() = Regex.Split(commentToAdd.Key, "([A-Z]+)")
                commentsVmlXml += GetCommentVMLShapeXML(colLetters(1), colLetters(2))
            Next

            Dim vmlDrawingPart As VmlDrawingPart = worksheetPart.AddNewPart(Of VmlDrawingPart)()

            Using writer As XmlTextWriter = New XmlTextWriter(vmlDrawingPart.GetStream(FileMode.Create), Encoding.UTF8)
                writer.WriteRaw("<xml xmlns:v=""urn:schemas-microsoft-com:vml""" & vbCrLf & " xmlns:o=""urn:schemas-microsoft-com:office:office""" & vbCrLf & " xmlns:x=""urn:schemas-microsoft-com:office:excel"">" & vbCrLf & " <o:shapelayout v:ext=""edit"">" & vbCrLf & "  <o:idmap v:ext=""edit"" data=""1""/>" & vbCrLf & "</o:shapelayout><v:shapetype id=""_x0000_t202"" coordsize=""21600,21600"" o:spt=""202""" & vbCrLf & "  path=""m,l,21600r21600,l21600,xe"">" & vbCrLf & "  <v:stroke joinstyle=""miter""/>" & vbCrLf & "  <v:path gradientshapeok=""t"" o:connecttype=""rect""/>" & vbCrLf & " </v:shapetype>" & commentsVmlXml & "</xml>")
            End Using

            For Each commentToAdd In commentsToAddDict
                Dim worksheetCommentsPart As WorksheetCommentsPart = If(worksheetPart.WorksheetCommentsPart, worksheetPart.AddNewPart(Of WorksheetCommentsPart)())

                If worksheetPart.Worksheet.Descendants(Of LegacyDrawing)().SingleOrDefault() Is Nothing Then
                    Dim vmlPartId As String = worksheetPart.GetIdOfPart(vmlDrawingPart)
                    Dim legacyDrawing As LegacyDrawing = New LegacyDrawing() With {
                        .Id = vmlPartId
                    }
                    worksheetPart.Worksheet.Append(legacyDrawing)
                End If

                Dim comments As Comments
                Dim appendComments As Boolean = False

                If worksheetPart.WorksheetCommentsPart.Comments IsNot Nothing Then
                    comments = worksheetPart.WorksheetCommentsPart.Comments
                Else
                    comments = New Comments()
                    appendComments = True
                End If

                If worksheetPart.WorksheetCommentsPart.Comments Is Nothing Then
                    Dim authors As Authors = New Authors()
                    Dim author As Author = New Author()
                    author.Text = "Author Name"
                    authors.Append(author)
                    comments.Append(authors)
                End If

                Dim commentList As CommentList
                Dim appendCommentList As Boolean = False

                If worksheetPart.WorksheetCommentsPart.Comments IsNot Nothing AndAlso worksheetPart.WorksheetCommentsPart.Comments.Descendants(Of CommentList)().SingleOrDefault() IsNot Nothing Then
                    commentList = worksheetPart.WorksheetCommentsPart.Comments.Descendants(Of CommentList)().Single()
                Else
                    commentList = New CommentList()
                    appendCommentList = True
                End If

                Dim comment As Comment = New Comment() With {
                    .Reference = commentToAdd.Key,
                    .AuthorId = CType(0UI, UInt32Value)
                }
                Dim commentTextElement As CommentText = New CommentText()
                Dim run As Run = New Run()
                Dim runProperties As RunProperties = New RunProperties()
                Dim bold As Bold = New Bold()
                Dim fontSize As FontSize = New FontSize() With {
                    .Val = 8.0R
                }
                Dim color As Color = New Color() With {
                    .Indexed = CType(81UI, UInt32Value)
                }
                Dim runFont As RunFont = New RunFont() With {
                    .Val = "Tahoma"
                }
                Dim runPropertyCharSet As RunPropertyCharSet = New RunPropertyCharSet() With {
                    .Val = 1
                }
                runProperties.Append(bold)
                runProperties.Append(fontSize)
                runProperties.Append(color)
                runProperties.Append(runFont)
                runProperties.Append(runPropertyCharSet)
                Dim text As Text = New Text()
                text.Text = commentToAdd.Value
                run.Append(runProperties)
                run.Append(text)
                commentTextElement.Append(run)
                comment.Append(commentTextElement)
                commentList.Append(comment)

                If appendCommentList Then
                    comments.Append(commentList)
                End If

                If appendComments Then
                    worksheetCommentsPart.Comments = comments
                End If
            Next
        End If
    End Sub
...