不能在VB.NET创建的单词表中加粗或更改文本大小

时间:2017-05-17 15:09:35

标签: vb.net

VB选择word doc中的表并向其添加行。它还应该能够加粗某些行并更改字体大小,但它不会

我用

设置数据
Public Sub TableSetCellValue(ByVal tableIndex As Integer, ByVal rowIndex As Integer, ByVal colIndex As Integer, ByVal value As String)

    wordDoc.Tables(tableIndex).Rows(rowIndex).Cells(colIndex).Range.Text = value

End Sub

我应该能够用

加粗
Public Sub TableRowFontWeight(ByVal tableIndex As Integer, ByVal rowIndex As Integer, ByVal bold As Boolean)

    wordDoc.Tables(tableIndex).Rows(rowIndex).Range.Bold = bold

End Sub

当我执行以下代码时

        writer.TableRowFontWeight(currTableIndex, currTableRowIndex, True)
        writer.TableSetCellValue(currTableIndex, currTableRowIndex, 0, column1)

它会写入文字,但不会加粗文字

1 个答案:

答案 0 :(得分:0)

这就是我使用openXML

的方法
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(FilePath, True)
            Dim wordCell = wordDoc.MainDocumentPart.Document.Body.Elements(Of Table).ElementAt(tableIndex).Elements(Of TableRow).ElementAt(rowIndex).Elements(Of TableCell).ElementAt(colIndex)
            Dim tempText = ""
            Dim bold As New Bold()
            'bold.Val = True
            Dim font As New FontSize()
            font.Val = "18"

            For Each para In wordCell.Elements(Of Paragraph)()
                If para.Elements(Of Run).Count > 0 Then
                    If para.Elements(Of Run).First.Elements(Of Text).Count > 0 Then
                        If weight = True Then
                            Dim runProperties As RunProperties = para.Elements(Of Run).First.AppendChild(New RunProperties(New Bold()))
                            runProperties.AppendChild(New RunProperties(font))
                            para.Elements(Of Run).First.AppendChild(New Text(value))
                        Else
                            Dim runProperties As RunProperties = para.Elements(Of Run).First.AppendChild(New RunProperties(font))
                            para.Elements(Of Run).First.AppendChild(New Text(value))
                        End If
                        For i = para.Elements(Of Run)().Count - 1 To 1 Step -1
                            para.Elements(Of Run).ElementAt(i).Remove()
                        Next
                    End If
                End If
            Next

        End Using