如何在.NET中阅读Microsoft Word文档?

发布于 2024-08-19 20:45:47 字数 36 浏览 3 评论 0原文

如何使用 C# 将 doc、docx 文件读入 .NET。

How to read doc, docx file into .NET with C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

椒妓 2024-08-26 20:45:47

我看到您使用了 asp.net 标记。您不应使用自动化 API (COM Interop) 从 ASP.NET 或任何其他服务器应用程序运行 Microsoft Office 产品。 Office 产品设计为从桌面运行 - 具有用户界面。它们在服务器场景中无法正常工作,此外还存在许可问题。

使用 Aspose.Words for .NET< /a> 或其他类似技术。它们设计用于服务器环境。

I see you used the asp.net tag. You should not use the automation API (COM Interop) to run Microsoft Office products from ASP.NET or any other server application. The Office products are made to be run from the desktop - with a user interface. They don't work properly in a server scenario, and additionally, there are licensing issues.

Use Aspose.Words for .NET or some other such technology instead. They are designed to be used in a server environment.

紫﹏色ふ单纯 2024-08-26 20:45:47

Aspose.Words for .NET 是一个商业库,可以让你做到这一点。来自网站:

使用 Aspose.Words for .NET,开发人员可以轻松打开和保存 DOC、OOXML、RTF、WordprocessingML、HTML、MHTML、TXT 和 OpenDocument 文档。

Aspose.Words for .NET is a commercial library that allows you to do exactly this. From the website:

Using Aspose.Words for .NET, developers can easily open and save DOC, OOXML, RTF, WordprocessingML, HTML, MHTML, TXT and OpenDocument documents.

江湖彼岸 2024-08-26 20:45:47

通常,COM 互操作用于与 Office 文档交互。

这是 MSDN 上关于创建 Excel 文件的示例,它应该可以给您一个想法。

http://msdn.microsoft.com/en-us /library/ms173186(VS.80).aspx

此外,Visual Studio 2010 和 .net 4.0 将包含更多动态语言功能,这些功能有助于进行 Office com 互操作,请在此处阅读更多信息

http://blogs.msdn.com/samng /archive/2009/06/16/com-interop-in-c-4-0.aspx

这是一个视频

http://msdn.microsoft.com/en-us/vcsharp/ee460939.aspx

Generally a COM interop is used to interface with office documents.

Here's an example on MSDN on creating an excel file, it should give you an idea.

http://msdn.microsoft.com/en-us/library/ms173186(VS.80).aspx

Also, Visual Studio 2010 along with .net 4.0 will include more dynamic language features which lend themselves to doing office com interop, read more here

http://blogs.msdn.com/samng/archive/2009/06/16/com-interop-in-c-4-0.aspx

And here's a video

http://msdn.microsoft.com/en-us/vcsharp/ee460939.aspx

清醇 2024-08-26 20:45:47

您可以简单地使用 RichTextBox 控件通过 RichTextBox.Load 方法读取 .rtf 和 .doc 文件

you can simply use the RichTextBox control to read .rtf and .doc files using RichTextBox.Load method

是你 2024-08-26 20:45:47

Microsoft 提供了一组免费的互操作程序集,用于与 .NET 中的各种 Office 文件格式进行交互,下载位置根据您使用的 Office 版本而有所不同,但在 Google 中搜索“Microsoft Office Primary Interop Assemblies”将生成以下链接: MSDN 中的各种版本,例如 这个适用于 Office 2007。

至于如何使用这些互操作打开 Word 文档(doc 或 docx),以下代码片段显示了如何打开 Word 文档:

_Application WordApp = new Microsoft.Office.Interop.Word.Application();

  object WordFile = "C:\\SomeDoc.doc";
  object RdOnly = false;
  object Visible = true;
  object Missing = System.Reflection.Missing.Value;
  Document Doc = WordApp.Documents.Open(ref WordFile, ref Missing, ref RdOnly, ref Missing, ref Missing, 
                                        ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, 
                                        ref Missing, ref Visible, ref Missing, ref Missing, ref Missing, 
                                        ref Missing);

从那里您可以使用 Doc 访问文档的各个部分。

Microsoft provide a free set of interop assemblies for interacting with the various Office file formats in .NET, the download locations differ depending on which version of Office you are using but a Google search for "Microsoft Office Primary Interop Assemblies" will yield the links for various versions from MSDN such as this one for Office 2007.

As for how to open a Word document (doc or docx) using these interops the following snippet shows how to open a Word document:

_Application WordApp = new Microsoft.Office.Interop.Word.Application();

  object WordFile = "C:\\SomeDoc.doc";
  object RdOnly = false;
  object Visible = true;
  object Missing = System.Reflection.Missing.Value;
  Document Doc = WordApp.Documents.Open(ref WordFile, ref Missing, ref RdOnly, ref Missing, ref Missing, 
                                        ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, 
                                        ref Missing, ref Visible, ref Missing, ref Missing, ref Missing, 
                                        ref Missing);

From there you can use Doc to access various parts of the document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文