如何读取“.doc”文件或如何使用ASP.NET将其转换为“.docx”

这是我面临的两个问题,在这两个问题中应该解决,以使我的项目工作。

所以这些是:

  1. 如何阅读“.doc”文件, 而不使用Word自动化或Aspose.Words等任何付费SDK

    (如果第一个不可能那么)

  2. 如何将“.doc”文件转换为“.docx”? 不使用Word自动化或Aspose.Words等任何付费SDK

搜索了很多,我发现只有.docx的开源解决方案。

这是在服务器上完成的,因此没有安装Word。

看看NPOI – 它是用.NET编写的,是免费的开源软件。 该路线图打算将来支持创建新格式,但是现在您可以使用它来读取旧格式并使用其他解决方案来编写新格式,这是一个开放标准(请参阅此处的MS规范 )。

如果需要开源,可以使用OpenXML SDK 。 或者使用Interop.Word API在.NET中有一个选项。 您可以使用此api打开文件并将其另存为docx。

http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word(v=office.11​​).aspx

但这需要在机器上安装。

有一个Microsoft批量转换工具,它做到了这一点。 我在这里找到了一个参考。

否则我认为你别无选择,只能使用Word Automation。 毕竟,即使OpenOffice也无法打开一些.doc文件并将它们转换为.docx / OpenXML,这意味着自己编写任何类型的解析工具都会很麻烦。

您可能希望为此纯.NET解决方案提供一个镜头:

b2xtranslator

它不需要您在服务器上安装任何Office应用程序。

我也遇到了同样的问题。 如果要将.doc转换为.docx,可以使用Microsoft.Office.Interop.Word库。 这个对我有用。 这是代码。

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Word = Microsoft.Office.Interop.Word; using System.Reflection; using System.IO; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Word._Application application = new Word.Application(); object fileformat = Word.WdSaveFormat.wdFormatXMLDocument; DirectoryInfo directory = new DirectoryInfo(@"D:\abc"); foreach (FileInfo file in directory.GetFiles("*.doc", SearchOption.AllDirectories)) { if (file.Extension.ToLower() == ".doc") { object filename = file.FullName; object newfilename = file.FullName.ToLower().Replace(".doc", ".docx"); Word._Document document = application.Documents.Open(filename); document.Convert(); document.SaveAs(newfilename, fileformat); document.Close(); document = null; } } application.Quit(); application = null; } } } 

它也适合你..