在 .Net 和 C# 中导入 doc 和 docx 文件

发布于 2024-10-08 17:21:13 字数 359 浏览 0 评论 0原文

我正在编写一个文本编辑器,我想添加导入 .doc 和 .docx 文件的可能性。我知道我可以使用 OLE 自动化,但如果我使用最新的 OLE 库,它就无法与使用旧版本 Word 的人一起使用,如果我使用旧版本,它将无法读取.docx 文件。 有什么想法吗? 谢谢

编辑:另一种解决方案是,就像我的应用程序使用 HTML 和 RTF 一样,使用命令行将 .doc 和 .docx 文件转换为这些格式之一,如下所示: http://www.snee.com/bobdc.blog/ 2007/09/using-word-for-command-line-co.html

I'm writing a text editor and I want to add the possibility to import .doc and .docx files. I know that I could use OLE Automation, but if I use a recent OLE library, it won't work with those people with an older version of Word, and if instead I use an older version, it won't be able to read .docx files.
Any ideas?
Thanks

EDIT: Another solution would be that, like my application works with HTML and RTF, convert .doc and .docx files with command line to one of these formats, something like this: http://www.snee.com/bobdc.blog/ 2007/09/using-word-for-command-line-co.html

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

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

发布评论

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

评论(3

心碎的声音 2024-10-15 17:21:14

它适用于 Office 2003 PIA,在我运行 Office 2010 的计算机上进行了测试:

using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;

public string GetHtmlFromDoc(string path)
    var wordApp = new Application {Visible = false};

//Cargar documento
            object srcPath = path;
            var wordDoc = wordApp.Documents.Open(ref srcPath);

            //Guardarlo en HTML
            string destPath = Path.Combine(Path.GetTempPath(), "word" + (new Random().Next()) + ".html");
            if (wordDoc != null)
            {
                object oDestPath = destPath;
                object exportFormat = WdSaveFormat.wdFormatHTML;
                wordDoc.SaveAs(ref oDestPath, ref exportFormat);
            }

            //Cerrar
            wordDoc.Close();
            wordApp.Quit();

            //Comprobar que el archivo existe);
            if (File.Exists(destPath))
            {
               return File.ReadAllText(destPath, Encoding.Default);
}
return null;
}

It's works with the Office 2003 PIA, tested in my computer running Office 2010:

using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;

public string GetHtmlFromDoc(string path)
    var wordApp = new Application {Visible = false};

//Cargar documento
            object srcPath = path;
            var wordDoc = wordApp.Documents.Open(ref srcPath);

            //Guardarlo en HTML
            string destPath = Path.Combine(Path.GetTempPath(), "word" + (new Random().Next()) + ".html");
            if (wordDoc != null)
            {
                object oDestPath = destPath;
                object exportFormat = WdSaveFormat.wdFormatHTML;
                wordDoc.SaveAs(ref oDestPath, ref exportFormat);
            }

            //Cerrar
            wordDoc.Close();
            wordApp.Quit();

            //Comprobar que el archivo existe);
            if (File.Exists(destPath))
            {
               return File.ReadAllText(destPath, Encoding.Default);
}
return null;
}
百变从容 2024-10-15 17:21:14

为什么不使用 Office 主要互操作程序集< /a>(PIA)?

我认为您必须决定要支持哪些版本的 Word。我建议你选择Word 2003作为最低的。这将允许您使用 Office 2003 PIA 并针对它们进行编程。在计算机中安装 PIA 也会安装绑定重定向,因此它们可以在 Word 上使用较新的版本。通过 Office 2003 PIA 使用 Word 2007 或 2010 打开 .docx 文件应该没有问题,尽管我自己没有尝试过。

Why don't you use the Office Primary Interop Assemblies (PIAs)?

I think you will have to decide which versions of Word you want to support. I suggest you settle on Word 2003 as the lowest. That will allow you to use the Office 2003 PIAs and program against them. Installing PIAs in a machine installs binding redirects as well, so they work with newer versions on Word. There should be no problem in opening .docx files with Word 2007 or 2010 through Office 2003 PIAs, although I haven't tried this myself.

无声情话 2024-10-15 17:21:14

您应该能够使用 OpenXML 库或 .NET 中的 xpath 来读取/导入 docx 文件的内容。

You should be able to use the OpenXML libraries or xpath in .NET to read / import the contents of a docx file.

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