Foxtable(狐表)用户栏目专家坐堂 → 麻烦有点蓝等各位老师 帮忙看看 怎么实现读取word文件内容并显示在富文本框内内容?且对应的文本格式不变……


  共有4252人关注过本帖树形打印复制链接

主题:麻烦有点蓝等各位老师 帮忙看看 怎么实现读取word文件内容并显示在富文本框内内容?且对应的文本格式不变……

帅哥哟,离线,有人找我吗?
cnsjroom
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1571 积分:11238 威望:0 精华:0 注册:2021/1/17 17:06:00
麻烦有点蓝等各位老师 帮忙看看 怎么实现读取word文件内容并显示在富文本框内内容?且对应的文本格式不变……  发帖心情 Post By:2022/5/16 13:03:00 [显示全部帖子]

当前项目有表A(html内容  纯文本内容  两个列名)

 

按图依次点击的按钮

在点击word文件导入后,文本直接写入了表A中的纯文本内容列中,但是没有显示在富文本编辑框中……

点击保存后,获取得到的Word文件会对应的保存到当前行

 

怎么实现读取word文件内容并显示在富文本框内内容?且对应的文本格式不变……


此主题相关图片如下:11.png
按此在新窗口浏览图片

 下载信息  [文件大小:770.1 KB  下载次数:2]
点击浏览该文件:kindeditor富文本案例v2.zip

 

代码如下:

初始化按钮代码:

Dim HKCU= Registry.CurrentUser
Dim hkSoftWare = HKCU.OpenSubKey("SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",True)
hkSoftWare.SetValue(System.Diagnostics.Process.GetCurrentProcess.ProcessName &".exe", "11001", Microsoft.Win32.RegistryValueKind.DWord)
HKCU.Close()
hkSoftWare.Close()

Dim wb = e.Form.Controls("WebBrowser1").BaseControl
wb.Url = new System.Uri(ProjectPath & "KindEditor\e.html", System.UriKind.Absolute)
Dim ke As new KindEditor '必须创建一个新实例
wb.ObjectForScripting =ke

e.Form.Controls("GroupBox2").Enabled =True

 

读取并绑定当前行:

Dim wb = e.Form.Controls("WebBrowser1").BaseControl
Dim ke As KindEditor = wb.ObjectForScripting

Dim r As Row = Tables("表A").Current
If r IsNot Nothing Then
    '绑定某个字段,当富文本框变化时,自动变化
    ke.FullHtmlBindingField = "表A.完整html内容" 
    ke.InnerHtmlBindingField = "表A.纯文本内容" 
    wb.refresh '刷新
End If

 

Word文件导入代码如下:

Dim app As New MSWord.Application
'Dim wb2 As WinForm.WebBrowser = e.Form.Controls("WebBrowser1")
'Dim t1 As WinForm.TextBox = e.Form.Controls("TextBox1")
Dim r As Row = Tables("表A").Current
try
    Dim dlg As New OpenFileDialog
    dlg.Filter = "|*.*"
    If dlg.ShowDialog =DialogResult.OK Then
        Dim fl As String = dlg.FileName
        'wb2.Address=fl
        Dim doc = app.Documents.Open(dlg.fileName)
        Dim count = Doc.Characters.Count
        Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
        't1.text=rng.Text.replace(chr(13), vbcrlf)
        If r IsNot Nothing Then
            r("纯文本内容")=rng.Text.replace(chr(13), vbcrlf)
        End If
    End If
    app.Quit
catch ex As exception
    msgbox(ex.message)
    app.Quit
End try

 

保存按钮代码:

Dim r As Row = Tables("表A").Current
If r IsNot Nothing Then
    r.Save
    MessageBox.Show("当前行保存成功")
End If

 

 

在点击word文件导入后,文本直接写入了表A中的纯文本内容列中,但是没有显示在富文本编辑框中……

点击保存后,获取得到的Word文件会对应的保存到当前行

 

 

当前项目全局代码内容为:

'富文本框 WebBrowser里的KindEditor方法属性
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class KindEditor
    Public isReadOnly As Boolean =False '是否只读 

    Private _FullHtmlBindingField As String 
    Private _InnerHtmlBindingField As String 
    Private _FullHtml As String
    Private _InnerHtml As String

    '完整html内容
    Property FullHtml() As String
        Get '获取属性的值
            Return _FullHtml
        End Get
        Set(ByVal value As String) '设定属性的值
            _FullHtml = value

            '同步更新绑定的列
            If _FullHtmlBindingField <> "" Then
                Dim vs() As String = _FullHtmlBindingField.split(".")
                If vs.Length =2 Then
                    Tables(vs(0)).Current(vs(1)) = value
                End If
            End If
        End Set
    End Property

    '纯文本内容
    Property InnerHtml() As String
        Get '获取属性的值
            Return _InnerHtml
        End Get
        Set(ByVal value As String) '设定属性的值
            '1用正则表达式去掉所有空格和换行符
            Static rgx As New RegularExpressions.Regex("\s")
            value = rgx.Replace(value,"")

            '2去掉base64的图片
            Dim si As Integer = value.IndexOf("<imgsrc=""data:image/png;base64,")
            If si>-1 Then
                Dim ei As Integer = value.IndexOf("/>",si)
                Do While (ei>si AndAlso si >-1)
                    value = value.Remove(si,ei-si+2)   
                    si = value.IndexOf("<imgsrc=""data:image/png;base64,")
                    If si >-1 Then
                        ei = value.IndexOf("/>",si)
                    End If
                Loop
            End If

            _InnerHtml =value

            '同步更新绑定的列
            If _InnerHtmlBindingField <> "" Then
                Dim vs() As String = _InnerHtmlBindingField.split(".")
                If vs.Length =2 Then
                    Tables(vs(0)).Current(vs(1)) = value
                End If
            End If
        End Set
    End Property

    '把html内容绑定到某个列
    Property FullHtmlBindingField() As String
        Get '获取属性的值
            Return _FullHtmlBindingField
        End Get
        Set(ByVal value As String) '设定属性的值
            '把当前列内容写入到FullHtml
            If value <> "" Then
                Dim vs() As String = value.split(".")
                If vs.Length =2 Then
                    _FullHtml = Tables(vs(0)).Current(vs(1))
                End If
            End If

            _FullHtmlBindingField= value
        End Set
    End Property

    '把纯文本内容绑定到某个列
    Property InnerHtmlBindingField() As String
        Get '获取属性的值
            Return _InnerHtmlBindingField
        End Get
        Set(ByVal value As String) '设定属性的值
            '把当前列内容写入到InnerHtml
            If value <> "" Then
                Dim vs() As String = value.split(".")
                If vs.Length =2 Then
                    _InnerHtml = Tables(vs(0)).Current(vs(1))
                End If
            End If

            _InnerHtmlBindingField =value
        End Set
    End Property
End Class

 

 

 

 

 

 

 

如果是先点击word文件导入按钮,将word文件读取并保存到指定行后,怎么生成HTML内容呢?
红色部分代码只能生成带格式的文本内容,不是HTML格式的内容……
如果能够解决这个问题,那对应的读取也就和富文本中点击word图标ctrl+v复制后保存的效果一样了 ,那自然在打开就实现了预期效果!


完整的html内容该如何根据当前已经读取到的Word文件内容来生成呢?


Dim app As New MSWord.Application
'Dim wb2 As WinForm.WebBrowser = e.Form.Controls("WebBrowser1")
'Dim t1 As WinForm.TextBox = e.Form.Controls("TextBox1")
Dim r As Row = Tables("表A").Current
try
    Dim dlg As New OpenFileDialog
    dlg.Filter = "|*.*"
    If dlg.ShowDialog =DialogResult.OK Then
        Dim fl As String = dlg.FileName
        'wb2.Address=fl
        Dim doc = app.Documents.Open(dlg.fileName)
        Dim count = Doc.Characters.Count
        Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
        't1.text=rng.Text.replace(chr(13), vbcrlf)
        If r IsNot Nothing Then
            r("纯文本内容")=rng.Text.replace(chr(13), vbcrlf)
            r("完整html内容")=rng.Text.replace(chr(13), vbcrlf)
        End If
    End If
    app.Quit
catch ex As exception
    msgbox(ex.message)
    app.Quit
End try


 回到顶部