2013-05-02 82 views
1

我可以上傳文本文件並在文本框中讀取文本。使用vb.net從asp.net中的.doc或.docx文件中讀取文本

現在我想爲.doc或.docx文件做同樣的事情。

當我以類似的方式嘗試閱讀文本文件時,我收到了一些文本,這些文本在整個文檔中處於加密格式。從.txt文件讀取代碼如下:

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

任何人都可以提示我一些想法嗎?

+0

這是問題[http://stackoverflow.com/questions/14455268/reading-doc-and-docx-files-using-c-sharp-without-having-ms-office-installed-的重複上 - s]的(HTTP://小號tackoverflow.com/questions/14455268/reading-doc-and-docx-files-using-c-sharp-without-having-ms-office-installed-on-s)你也可以試試[http://docx.codeplex .com /](http://docx.codeplex.com/) – skhurams 2013-05-02 14:52:26

回答

1

你想要的是一個ifilter .doc(x)文件。 Ifilter被設計爲Windows用於其索引服務,但它們也經常被用於其他應用程序以便從包含文本的二進制文件中讀取文本。 IFilter經常免費發佈 - 我相信this包含doc/docx文件(和其他Office文件)的正確ifilters。

這就是說,我從來沒有在.net中使用ifilter接口,只有在非託管C++中,但它應該是可能的。一個快速的搜索結果使this成爲一個可能的開始(它有一些可以避免的事情和一些代碼的建議,我不能保證代碼的正常工作,你可能需要找到其他的東西,但是ifilter技術本身可以工作,我在項目中使用過。除了爲的IFilter附帶的讀卡器,它纔剛剛「作品」,撐不住了,我最後一次檢查。辦公室的IFilter做工精細的PDF,雖然。)

0

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

+0

歡迎來到SO。請正確格式化您的代碼,並考慮添加代碼以外的上下文 – 2016-05-16 16:09:48

相關問題