当前位置: 首页 > 知识库问答 >
问题:

VB. net,将RTF TextBox内容导出到Excel Cell

冯新知
2023-03-14

我有一个RichTextFormat文本框,它包含多行、粗体、下划线和斜体文本。我需要将文本和字体样式都粘贴到Excel单元格中,因此“SAMPLE”仍将是“SAMPLE”,而不是“SAMPLE”或“SAMPLE\b0\par”。

我目前尝试

ExcelApp.Range("C2").Value = RTFTB.Rtf

这让我

{\rtf1\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fnil\fcharset0 Calibri;}}\viewkind4\uc1\pard\b\f0\fs23示例加粗\b0\par\par\i示例倾斜\i0\par\par\ul示例下划线\par\par\ulnone示例正常\par}

(RTF代码

而且

ExcelApp.Range("C2").Value = RTFTB.Text

这让我

示例加粗

斜体示例

示例下划线

样品正常

(无fontstyle)

共有2个答案

姬朗
2023-03-14

我最终没有做RTF超文本标记语言Excel解决方案因为我没有所需的技能这样做

相反,我选择了这个:

>

  • 复制整个RichTextBox

    粘贴到Word中

    复制Word文档

    将其粘贴到Excel中

    它并不完美,因为它变成了图像而不是文本,但至少它是有效的。

    这是我的代码

    
    'Phase 1 : Copy the RichTextBox
    
            TbRTF.SelectAll()
            TbRTF.Copy()
            Clipboard.SetData(DataFormats.Rtf, TbRTF.Rtf)
    
    'Prepare phase 2 : Declare variables
    
            Dim aDoc As Word.Document = WordApp.Documents.Open("a blank word file.docx")
            Dim TheRange As Word.Range = aDoc.ActiveWindow.Selection.Range
    
    'Phase 2 : Paste into Word
    
            WordApp.Visible = False
    
            TheRange.WholeStory()
            TheRange.Paste()
    
    'Phase 3 : Copy from Word
    
            TheRange.WholeStory()
            WordApp.ActiveDocument.Select()
            WordApp.Selection.Copy()
    
    'Phase 3.5 : Close Word
    
            aDoc.Close()
            WordApp.Quit()
    
    'Phase 4 : Paste into Excel
    
            ExcelApp.Range("D" & r).PasteSpecial()
    
    'Phase 4.5 : Close Excel
    
            Classeur.SaveAs("FinishedExcelFile.xlsx")
            Classeur.Close()
            ExcelApp.Quit()
    

  • 沙星波
    2023-03-14

    使用剪贴板传输信息并不是我想提出的解决方案。然而,我知道,如果不将RTF转换为一系列UI自动化命令来模拟将文本添加到Excel然后格式化,就无法实现您的目标。

    以下方法可用于将RTF作为Html放置在剪贴板上。它在可编辑模式下使用Web浏览器控件将剪贴板的RTF格式转换为Html。然后将Html放置在剪贴板上。

    总的顺序是:

    >

  • 将RTF作为RTF放在剪贴板上
  • 创建WebBrowser控件并将其DocumentText设置为可编辑Html。

    在WebBrowser中。DocumentCompleted事件处理程序,

    将RTF粘贴到WebBrowser。

    ...

    Public Shared Sub RtfToHtmlClipboard(rtf As String)
        Clipboard.SetData(DataFormats.Rtf, rtf)
        Dim browser As New WebBrowser
        AddHandler browser.DocumentCompleted, Sub(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
                                                Dim wb As WebBrowser = CType(sender, WebBrowser)
                                                wb.Document.ExecCommand("Paste", False, Nothing)
                                                wb.Document.ExecCommand("SelectAll", False, Nothing)
                                                wb.Document.ExecCommand("Copy", False, Nothing)
                                                wb.Dispose()
                                              End Sub
    
        browser.DocumentText = "<html><body contenteditable=""true""></body></html>"
    End Sub
    

    编辑:

    我使用上面的方法将RTF作为HTML放在剪贴板上,供用户手动粘贴。因此,我没有考虑使用对时间敏感的用例(如Excel automation)的影响。

    对于自动化情况,最好不要每次都创建控件。此外,通过自动化粘贴到Excel时似乎还有其他机制在起作用(可能与Office剪贴板有关?),这再次证实了我不喜欢使用剪贴板。

    然而,我发现以下方法可以将格式化的RTF复制到Excel。我的Office版本很古老(2007),所以希望它在新版本上仍然有效。创建新的WinForm应用程序并替换Form1。用VB的以下内容来测试它。在运行代码时,单击“EXECUTE”按钮创建一个Excel实例,并将一些格式匹配的RTF粘贴到其中。

    Imports Excel = Microsoft.Office.Interop.Excel
    
    Public Class Form1
        Private WithEvents rtb As RichTextBox
        Private WithEvents btnExecute As Button
        Private editableBrowser As WebBrowser
    
        Public Sub New()
            InitializeComponent()
            Setup()
        End Sub
    
        Private Sub Setup()
            editableBrowser = New WebBrowser With {
                .DocumentText = "<html><body contenteditable=""true""></body></html>"
            }
    
            btnExecute = New System.Windows.Forms.Button()
            rtb = New System.Windows.Forms.RichTextBox()
            SuspendLayout()
    
            btnExecute.Location = New System.Drawing.Point(580, 20)
            btnExecute.Size = New System.Drawing.Size(135, 50)
            btnExecute.TabIndex = 0
            btnExecute.Text = "Execute"
            btnExecute.UseVisualStyleBackColor = True
            AddHandler btnExecute.Click, AddressOf btnExecute_Click
    
            rtb.Location = New System.Drawing.Point(20, 20)
            rtb.Size = New System.Drawing.Size(450, 350)
            rtb.TabIndex = 1
    
            ClientSize = New System.Drawing.Size(800, 450)
            Controls.Add(Me.rtb)
            Controls.Add(Me.btnExecute)
            ResumeLayout()
        End Sub
    
        Private Sub btnExecute_Click(sender As Object, e As EventArgs)
            ExcelWork()
            COMCleanUp()
        End Sub
    
        Private Function GetRTF1() As String
            rtb.Clear()
            ' add some text and format it.
            Dim formattedText As String = "some sample rtf"
            rtb.Text = "This is " & formattedText & " to format and copy."
            rtb.CreateControl()
            rtb.SelectionStart = rtb.Find(formattedText)
            rtb.SelectionLength = formattedText.Length
            rtb.SelectionColor = Color.Red
            rtb.SelectionFont = New Font(Font.FontFamily, Font.Size + 2.0F, FontStyle.Bold)
            Return rtb.Rtf
        End Function
    
        Sub ExcelWork()
            Dim app As New Excel.Application
            Dim wb As Excel.Workbook = app.Workbooks.Add()
            Dim rng As Excel.Range = DirectCast(wb.Worksheets(1), Excel.Worksheet).Range("A1")
    
            PlaceRtfFAsHtmlOnClipboard(GetRTF1)
            ' for some reason rng.PasteSpecial just pastes as 
            ' unformatted text.  manual pasting results in formatted 
            ' text.
            ' The Worsheet.Paste method as well as the Worsheet.PasteSpecial
            ' methods will paste the Clipboard HTML format
            rng.Worksheet.PasteSpecial(Format:="HTML")
            'rng.Worksheet.Paste(rng)
    
            wb.Saved = True
            app.Visible = True  ' hand control over to user
            app.UserControl = True
        End Sub
    
        Private Sub PlaceRtfFAsHtmlOnClipboard(rtf As String)
    
            ' Clear the browser
            editableBrowser.Document.ExecCommand("SelectAll", False, Nothing)
            editableBrowser.Document.ExecCommand("Cut", False, Nothing)
    
            '  put rtf on clipboard
            Clipboard.SetData(DataFormats.Rtf, rtf)
    
            ' and paste to the editable broswer
            editableBrowser.Document.ExecCommand("Paste", False, Nothing)
            editableBrowser.Document.ExecCommand("SelectAll", False, Nothing)
    
            ' copy the html to the Clipboard
            editableBrowser.Document.ExecCommand("Copy", False, Nothing)
        End Sub
    
        Private Sub COMCleanUp()
            Do
                GC.Collect()
                GC.WaitForPendingFinalizers()
            Loop While System.Runtime.InteropServices.Marshal.AreComObjectsAvailableForCleanup
        End Sub
    
    End Class 
    

  •  类似资料:
    • 我必须将多个PDF合并为一个PDF。 我正在使用iText。sharp库和collect转换了代码并尝试使用它(从这里)实际的代码是C#,我将其转换为VB.NET。 我现在收到以下错误: 已添加具有相同密钥的项目。 我做了一些调试,并将问题归结为以下几行: 为什么会出现这种错误?

    • 我需要将一些HTML导出到Word中,发现了以下链接:https://www.codexworld.com/export-html-to-word-doc-docx-using-javascript 代码如下。它生成一个。doc文件(旧的Word格式),但我更喜欢。docx。文章说只要稍加修改就可以完成,但没有说明如何完成。有人知道怎么做这个改变吗?

    • 在 Dreamweaver 中以 xml 形式导出和导入可编辑内容。了解将基于模板的内容从一个站点导出到另一个站点。 可以将基于模板的文档看作是其中含有由名称-值对表示的数据的模板。每一对由可编辑区域的名称及该区域的内容组成。 您可以将名称/值对导出到 XML 文件中,以便可在 Dreamweaver 之外(例如,在 XML 编辑器或文本编辑器中,或在数据库应用程序中)处理数据。反之,如果您的 X

    • 本文向大家介绍C#导出文本内容到word文档的方法,包括了C#导出文本内容到word文档的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#导出文本内容到word文档的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的C#程序设计有所帮助。

    • 问题内容: 我正在编写一个非常简单的示例项目,以使自己熟悉Jasper Reports。我想将已配置的报告导出为PDF ,但是没有工厂方法: 如何在中获取PDF ? 问题答案: 好的,这就是它的工作原理。实际上返回一个对象,因此: 请享用。