Visual Studio VB.Net RichTextBox 控件 - 列中的文本缺少换行符但表格工作正常
Visual Studio VB.Net RichTextBox control - text in columns have missing line breaks but tables work fine
RichTextBox VB.Net 控件有问题:
* Microsoft Visual Studio Professional 2012 版本 11.0.61219.00 更新 5
* Microsoft .NET Framework 版本 4.5.50938
我将 RTF 字符串存储在数据库中并在 RichTextBox 中使用它们,但是:
* 列中的文本缺少换行符(又名 hard returns/new lines/carriage returns)。 (例如:MS Word > 页面布局 > 分栏 > 输入文本并按回车键)。
* 表格中的文本看起来不错,而且到目前为止我遇到的所有其他格式。
如果我将数据库中的 RTF 字符串保存到 *.rtf 文件中并在 Microsoft Word 中打开它,它会正确显示列,但是如果我在写字板中打开它,列中会缺少换行符,大概是您不能在写字板中创建列,只能在 MS Word 中创建。
Ideas/solutions好吗?
即使我可以使用数据库中的 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
没关系,想通了:)
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
RichTextBox VB.Net 控件有问题: * Microsoft Visual Studio Professional 2012 版本 11.0.61219.00 更新 5 * Microsoft .NET Framework 版本 4.5.50938
我将 RTF 字符串存储在数据库中并在 RichTextBox 中使用它们,但是:
* 列中的文本缺少换行符(又名 hard returns/new lines/carriage returns)。 (例如:MS Word > 页面布局 > 分栏 > 输入文本并按回车键)。
* 表格中的文本看起来不错,而且到目前为止我遇到的所有其他格式。
如果我将数据库中的 RTF 字符串保存到 *.rtf 文件中并在 Microsoft Word 中打开它,它会正确显示列,但是如果我在写字板中打开它,列中会缺少换行符,大概是您不能在写字板中创建列,只能在 MS Word 中创建。
Ideas/solutions好吗?
即使我可以使用数据库中的 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
没关系,想通了:)
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