![](/img/trans.png)
[英]Display Text From a doc or docx file in a TextBox without using Microsoft.Office.Interop in asp.net
[英]Read text from a .doc or .docx file in asp.net using vb.net
我可以上传文本文件并在文本框中阅读其文本。
现在,我想对.doc或.docx文件执行相同的操作。
当我以类似的方式尝试阅读文本文件时,我得到了一些在整个文档中都是加密格式的文本。 从.txt文件读取的代码如下:
txtReadFiles.Text = My.Computer.FileSystem.ReadAllText(Path)
有人可以建议我一些想法吗?
您想要的是一个用于.doc(x)文件的ifilter 。 Windows旨在将Ifilter用于其索引服务,但是Ifilters也经常被其他应用程序使用,以从包含文本的二进制文件中读取文本。 IFilter经常免费发布-我相信它包含适用于doc / docx文件(和其他Office文件)的正确ifilter。
就是说,我从未在.net中使用过ifilter接口,仅在非托管c ++中使用过,但是应该可以的。 快速谷歌搜索打开了这个作为一个可能的地方开始(它的事情,以避免一些建议,以及一些代码。我不作任何保证代码工作,你可能需要找点别的事情,但IFilter的技术本身不工作,我之前曾在项目中使用过它。最后一次检查的是Reader附带的pdf的ifilter(仅能“工作”)。Officeifilter可以正常工作。)
Imports Microsoft.Office.Interop.Word 'above public class
If OpenFileDialogFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then TBfile.Text = OpenFileDialogFile.FileName 'alamat n nama file asli '----------- Dim ext As String ext = Path.GetExtension(OpenFileDialogFile.FileName) If ext = ".txt" Then 'tampilkan isi file TB1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialogFile.FileName) ElseIf ext = ".doc" Then Dim App As Application = New Application Dim doc As Document Try doc = App.Documents.Open(OpenFileDialogFile.FileName) Dim co As Integer = doc.Words.Count For i As Integer = 1 To co Dim tex As String = doc.Words(i).Text 'tampilkan isi file TB1.Text += tex Next doc.Close() Catch ex As Exception End Try ElseIf ext = ".docx" Then Dim App As Application = New Application Dim doc As Document Try doc = App.Documents.Open(OpenFileDialogFile.FileName) Dim co As Integer = doc.Words.Count For i As Integer = 1 To co Dim tex As String = doc.Words(i).Text 'tampilkan isi file TB1.Text += tex Next doc.Close() Catch ex As Exception End Try End If '---------- Else Call kosongkan() CBkunci1.Focus() End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.