vb.net .NET 在没有 Microsoft.Office.Interop 的情况下读取 word/excel 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15524512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:53:16  来源:igfitidea点击:

.NET read word/excel documents without Microsoft.Office.Interop

vb.netexcelms-word

提问by Manjar

I'm trying to open a doc file under VB.NET, I found a very simple way to do it using word:

我正在尝试在 VB.NET 下打开一个 doc 文件,我找到了一种使用 word 的非常简单的方法:

For example:

例如:

    Dim doc As Word.Document
    Dim wordApp As New Word.Application

    Dim allText As String
    Try
        doc = wordApp.Documents.Open("C:\marti.doc")
        allText = doc.Range.Text()
        doc.Close()
        RichTextBox1.Text = allText
    Catch
        'error            
    End Try

(More detailed info: http://support.microsoft.com/kb/316383)

(更详细的信息:http: //support.microsoft.com/kb/316383

This could work, but it need to open Microsoft Word window to handle it. I need to use it without Word installed. So I need a Library which can open doc/excel files.

这可以工作,但需要打开 Microsoft Word 窗口来处理它。我需要在没有安装 Word 的情况下使用它。所以我需要一个可以打开 doc/excel 文件的库。

Do you know a good Library that can do this?

你知道一个好的图书馆可以做到这一点吗?

I found this library: http://bytescout.com/download/trial/documentsdk.html

我找到了这个库:http: //bytescout.com/download/trial/documentsdk.html

Have you tried this?

你试过这个吗?

回答by Sam Makin

The recommended way of interacting with Office files is through the Office OpenXML library. You can get it here.

与 Office 文件交互的推荐方式是通过 Office OpenXML 库。你可以在这里得到它。

If you are using Microsoft Office 2003, you can load and save documents in Open XML if you have installed the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats. You can download the compatibility pack and find more info here.

如果您使用的是 Microsoft Office 2003,并且已安装 Microsoft Office Compatibility Pack for Word、Excel 和 PowerPoint 2007 文件格式,则可以加载和保存 Open XML 格式的文档。您可以下载兼容包并在此处找到更多信息。

This is an example method to gather the content of the document:

这是收集文档内容的示例方法:

Private Shared Function GetWordDocContent(strDoc As String) As String
    Dim stream As Stream = File.Open(strDoc, FileMode.Open)
    Dim wordprocessingDocument__1 As WordprocessingDocument = WordprocessingDocument.Open(stream, True)
    Dim body As Body = wordprocessingDocument__1.MainDocumentPart.Document.Body
    Dim content As String = body.InnerText
    wordprocessingDocument__1.Close()
    Return content    
End Function

Another example here.

另一个例子在这里

回答by Sam Makin

for handling docx file you can try NPOIor DocX

要处理 docx 文件,您可以尝试NPOIDocX

for the old word format (.doc) I've tried Syncfusion DocIOcomponents, but i prefer to use word automation with NetOffice, much better than the interop

对于旧的单词格式 (.doc),我尝试过Syncfusion DocIO组件,但我更喜欢在NetOffice 中使用单词自动化,这比互操作要好得多