简体   繁体   中英

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.

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 :

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

can anyone suggest me some idea?

What you want is an ifilter for .doc(x) files. 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. IFilters are frequently released for free - I believe this contains the correct ifilters for doc/docx files (and other Office files).

That said, I've never used the ifilter interface in .net, only in unmanaged c++, but it should be possible. 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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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