Visual Studio VB.Net RichTextBox控件 - 列中的文本缺少换行符但表格工作正常

问题描述 投票:-2回答:1

在以下位置遇到RichTextBox VB.Net控件的问题:* Microsoft Visual Studio Professional 2012版本11.0.61219.00更新5 * Microsoft .NET Framework版本4.5.50938

我有RTF字符串存储在数据库中并在RichTextBox中使用它们,但是: *列中的文本缺少换行符(也就是硬回车/换行/回车)。 (例如:MS Word>页面布局>列>键入文本并按Enter键)。 *表中的文本看起来很好,到目前为止我遇到的所有其他格式。

如果我将RTF字符串从数据库保存到* .rtf文件并在Microsoft Word中打开它会正确显示列,但是如果我在写字板中打开它,列中缺少换行符,可能是因为您无法创建写字板中的列,仅在MS Word中。

想法/解决方案好吗?

即使我可以使用数据库中的RTF字符串创建word文档,这应该可行,但是我不知道如何做到这一点:

提取:

Private Sub RtfToWordDoc()

    ' Connect to database
    Dim sqlCon As New SqlConnection(strConnectionString)
    Dim sqlCmd As New SqlCommand
    Dim sqlReader As SqlDataReader
    Dim oWord As New Word.Application

    ' prepare the sql query
    sqlCmd.CommandText = "select ... from ... where ..."
    sqlCmd.CommandType = CommandType.Text
    sqlCmd.Connection = sqlCon

    ' obtain the query results
    sqlCon.Open()
    sqlReader = sqlCmd.ExecuteReader
    sqlReader.Read()

    If sqlReader.HasRows Then

        ' Populate the RichTextBox control on the Form with Query Results 
        Me.richTextBoxEx1.Rtf = CStr(sqlReader.GetSqlString(1))

        ' Copy the rich text box content
        richTextBoxEx1.SelectionStart = 0
        richTextBoxEx1.SelectionLength = richTextBoxEx1.TextLength
        richTextBoxEx1.Copy()

        ' Create a word document & paste the rich text box content into it
        oWord.Documents.Add()
        oWord.Visible = True
        Dim range = oWord.ActiveDocument.Range(Start:=0, End:=0)
        range.Paste()   ' keeps RTB formatting, ignores word formatting

    Else
        MsgBox("No results found.", MsgBoxStyle.OkOnly, "Error")
    End If

    sqlCon.Close()

End Sub
vb.net ms-word richtextbox rtf
1个回答
0
投票

没关系,想通了:)

Private Sub RtfToWordDoc()

    ' Connect to database
    Dim sqlCon As New SqlConnection(strConnectionString)
    Dim sqlCmd As New SqlCommand
    Dim sqlReader As SqlDataReader
    Dim sRTF As String
    Dim oWord As New Word.Application

    ' prepare the sql query
    sqlCmd.CommandText = "select ... from ... where ..."
    sqlCmd.CommandType = CommandType.Text
    sqlCmd.Connection = sqlCon

    ' obtain the query results
    sqlCon.Open()
    sqlReader = sqlCmd.ExecuteReader
    sqlReader.Read()

    If sqlReader.HasRows Then

        sRTF = CStr(sqlReader.GetSqlString(1))

        ' Populate the RichTextBox control on the Form with Query Results 
        ' Note: formatting within columns is incorrectly displayed
        Me.richTextBoxEx1.Rtf = sRTF

        ' Copy the contents of the Rich Text string to the clipboard
        Clipboard.SetText(sRTF, TextDataFormat.Rtf)

        ' Create a word document & paste the rich text box content into it
        oWord.Documents.Add()       ' creates a new word doc using default word template (normal.dot)
        oWord.Visible = True
        Dim range = oWord.ActiveDocument.Range(Start:=0, End:=0)
        range.Paste()   ' keeps rich text formatting, ignores word formatting

        ' Change the single spacing (from normal.dot word template) to no spacing
        range.ParagraphFormat.LineSpacing = 1
        range.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceAtLeast
        range.ParagraphFormat.SpaceBefore = 0
        range.ParagraphFormat.SpaceAfter = 0

    Else
        MsgBox("No results found.", MsgBoxStyle.OkOnly, "Error")
    End If

    sqlCon.Close()

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.