vb.net Word表格格式

时间:2016-10-10 18:35:22

标签: vb.net ms-word

我一直在试图弄清楚如何强制单词表格下线直到单元格的结尾。如果线条长和/或短,我似乎遇到了问题。我不是专家,但我假设所有角色的大小都不一样......

enter image description here

这是代码生成的内容

enter image description here

以下是我用于创建上述代码的代码。我认为我应该能够检查细胞长度?任何帮助,将不胜感激。

Public Shared Sub CreateWordDocument()         尝试             Dim oWord As Word.Application             Dim oDoc As Word.Document

        'Start Word and open the document template.
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add

        Dim Row As Integer, Column As Integer
        Dim myTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 10, 2)

        myTable.Range.ParagraphFormat.SpaceAfter = 1

        Dim mystring As String = "This is my Test name That Runs over to the next line"
        Dim address1 As String = "123 1st fake street"
        Dim address2 As String = "Fake town place"

        Dim mystring2 As String = "This is good line"
        Dim address3 As String = "321 3rd fake street"
        Dim address4 As String = "Fake town place"
        Dim line As String = "_"

        For Row = 1 To 10

            If Row <> 5 Then
                myTable.Rows.Item(Row).Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle
                myTable.Rows.Item(Row).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
                myTable.Rows.Item(Row).Range.Font.Bold = False
                myTable.Rows.Item(Row).Range.Font.Size = 11
                myTable.Rows.Item(Row).Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle
            End If
            For Column = 1 To 2

                If Column = 1 And Row = 1 Then
                    myTable.Cell(Row, Column).Range.Text = GetString(mystring)
                ElseIf Column = 1 And Row = 2 Then
                    myTable.Cell(Row, Column).Range.Text = GetString(address1)
                ElseIf Column = 1 And Row = 3 Then
                    myTable.Cell(Row, Column).Range.Text = GetString(address2)
                ElseIf Column = 2 And Row = 1 Then
                    myTable.Cell(Row, Column).Range.Text = GetString(mystring2)
                ElseIf Column = 2 And Row = 2 Then
                    myTable.Cell(Row, Column).Range.Text = GetString(address3)
                ElseIf Column = 2 And Row = 3 Then
                    myTable.Cell(Row, Column).Range.Text = GetString(address4)
                Else
                    myTable.Cell(Row, Column).Range.Text = GetString(line)
                End If
            Next
        Next

        Dim strCellText As String
        Dim uResp As String

        Dim itable As Table

        For Each itable In oDoc.Tables
            uResp = ""
            For Row = 1 To itable.Rows.Count
                For Col = 1 To itable.Columns.Count
                    strCellText = itable.Cell(Row, Col).Range.Text
                    If strCellText.Length >= 33 Then
                        Console.Write("this will be on a different line")
                    ElseIf strCellText.Length <= 31 Then
                        Console.Write("this will be on a different line")
                    End If
                Next
            Next
        Next

    Catch ex As Exception

    End Try


End Sub

Public Shared Function GetString(ByVal strGetLine As String) As String

    If strGetLine.Length <> 30 Then
        Do Until strGetLine.Length >= 30
            strGetLine += "_"
            Dim count As String = strGetLine.Length
        Loop
    End If

    Return strGetLine

End Function

1 个答案:

答案 0 :(得分:1)

您的问题分为两部分。一个是字体。因为您使用“_”将每一行填充到预定宽度,所以必须使用等宽字体,否则这些行将不均匀地结束。使用等宽字体,每个字符将占用相同的宽度,这将为您提供统一的线条。其次,GetString函数接受少于30个字符的任何行并填充它,但它不处理超过30个字符的任何行,这就是行自行换行的原因。为了解决这两个问题,我将字体设置为等宽字体(在本例中为Courier New)并修改了GetString函数的逻辑。现在,如果该行超过30个字符,该函数将找到一个空格,它可以将字符串尽可能接近30-char限制并在那里添加一个中断,然后用下划线填充两行。以下是包含更改的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Added these two Dim's so I could run your example
    Dim oWord As Object
    Dim oDoc As Document

    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add

    Dim Row As Integer, Column As Integer
    Dim myTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 10, 2)

    myTable.Range.ParagraphFormat.SpaceAfter = 1

    Dim mystring As String = "This is my Test name That Runs over to the next line"
    Dim address1 As String = "123 1st fake street"
    Dim address2 As String = "Fake town place"

    Dim mystring2 As String = "This is good line"
    Dim address3 As String = "321 3rd fake street"
    Dim address4 As String = "Fake town place"
    Dim line As String = "_"

    For Row = 1 To 10
        'Removed this If, because all lines need font set to ensure same width, even if line has no text
        'If Row <> 5 Then
        myTable.Rows.Item(Row).Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle
        myTable.Rows.Item(Row).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
        myTable.Rows.Item(Row).Range.Font.Bold = False
        myTable.Rows.Item(Row).Range.Font.Size = 11
        myTable.Rows.Item(Row).Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle
        myTable.Rows.Item(Row).Range.Font.Name = "Courier New" 'Set font to a monospaced font
        'End If

        For Column = 1 To 2
            If Column = 1 And Row = 1 Then
                myTable.Cell(Row, Column).Range.Text = GetString(mystring)
            ElseIf Column = 1 And Row = 2 Then
                myTable.Cell(Row, Column).Range.Text = GetString(address1)
            ElseIf Column = 1 And Row = 3 Then
                myTable.Cell(Row, Column).Range.Text = GetString(address2)
            ElseIf Column = 2 And Row = 1 Then
                myTable.Cell(Row, Column).Range.Text = GetString(mystring2)
            ElseIf Column = 2 And Row = 2 Then
                myTable.Cell(Row, Column).Range.Text = GetString(address3)
            ElseIf Column = 2 And Row = 3 Then
                myTable.Cell(Row, Column).Range.Text = GetString(address4)
            Else
                myTable.Cell(Row, Column).Range.Text = GetString(line)
            End If
        Next
    Next

    Dim strCellText As String
    Dim uResp As String
    Dim itable As Table
    For Each itable In oDoc.Tables
        uResp = ""
        For Row = 1 To itable.Rows.Count
            For Col = 1 To itable.Columns.Count
                strCellText = itable.Cell(Row, Col).Range.Text
                If strCellText.Length >= 33 Then
                    Console.Write("this will be on a different line")
                ElseIf strCellText.Length <= 31 Then
                    Console.Write("this will be on a different line")
                End If
            Next
        Next
    Next
End Sub

Public Shared Function GetString(ByVal strGetLine As String) As String
    'If strGetLine.Length <> 30 Then
    '    Do Until strGetLine.Length >= 30
    '        strGetLine += "_"
    '        Dim count As String = strGetLine.Length
    '    Loop
    'End If
    'New Function Logic:

    'If the line is just a blank line, then just send back 30 underscores
    If strGetLine.Trim.Equals("_") Then Return strGetLine.PadRight(30, "_")

    Dim ret As String = Nothing
    If strGetLine.Length > 30 Then
        Dim lineBreak As Integer = 0
        If strGetLine.Length >= 30 Then
            Dim i As Integer = 0
            Do While i <= 30
                i = strGetLine.IndexOf(" ", i + 1)
                If i <= 30 Then lineBreak = i
            Loop
        End If
        ret = strGetLine.Substring(0, lineBreak).Trim.PadRight(30, "_") & vbCrLf
        ret &= strGetLine.Substring(lineBreak, strGetLine.Length - lineBreak).Trim.PadRight(30, "_")
    Else
        ret = strGetLine.PadRight(30, "_")
    End If
    Return ret
End Function

哪个输出:

Line Test

现在我确定你会注意到,右栏中似乎有一个空白行(其余的空白行来自10行循环)。这只是因为同一行的另一列有两行。我不知道这是否是您想要的,但如果您希望两列的外观具有相同的行数,则必须跟踪是否在第1列中拆分行,并添加额外的空白行到第二列......但这应该让你朝着正确的方向前进