简体   繁体   English

使用vb.net从asp.net中的.doc或.docx文件读取文本

[英]Read text from a .doc or .docx file in asp.net using vb.net

I can upload a text file and read its text in a textbox. 我可以上传文本文件并在文本框中阅读其文本。

Now I want to do the same for .doc or .docx files. 现在,我想对.doc或.docx文件执行相同的操作。

When I tried it in the similar way I read the text files I got some text which is in encrypted format in the whole document. 当我以类似的方式尝试阅读文本文件时,我得到了一些在整个文档中都是加密格式的文本。 The code for reading from a .txt file is as follows : 从.txt文件读取的代码如下:

txtReadFiles.Text = My.Computer.FileSystem.ReadAllText(Path)

can anyone suggest me some idea? 有人可以建议我一些想法吗?

What you want is an ifilter for .doc(x) files. 您想要的是一个用于.doc(x)文件的ifilter Ifilters were designed to be used by Windows for its indexing service, but they are frequently pressed into use also for other applications to read text from binary files that contain text. Windows旨在将Ifilter用于其索引服务,但是Ifilters也经常被其他应用程序使用,以从包含文本的二进制文件中读取文本。 IFilters are frequently released for free - I believe this contains the correct ifilters for doc/docx files (and other Office files). IFilter经常免费发布-我相信包含适用于doc / docx文件(和其他Office文件)的正确ifilter。

That said, I've never used the ifilter interface in .net, only in unmanaged c++, but it should be possible. 就是说,我从未在.net中使用过ifilter接口,仅在非托管c ++中使用过,但是应该可以的。 A quick googling turned up this as a likely place to start (it has some recommendations of things to avoid, and some code. I make no guarantee that the code works, you might have to find something else. But the ifilter technology itself does work, I've used it in projects before. Other than the ifilter for pdfs that ships with Reader, which only just "works", barely, last I checked. The Office ifilters work fine, though.) 快速谷歌搜索打开了这个作为一个可能的地方开始(它的事情,以避免一些建议,以及一些代码。我不作任何保证代码工作,你可能需要找点别的事情,但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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM