这是我面临的两个问题,在这两个问题中,应该解决一个问题,以使我的项目正常工作。
所以这里是那些:
如何在不使用 Word 自动化或任何付费 SDK(如 Aspose.Words )的情况下阅读“.doc”文件。
(如果第一个不可能,那么)
如何将“.doc”文件转换为“.docx”?无需使用 Word 自动化或任何付费 SDK,如 Aspose.Words。
搜索了很多,我发现只有 .docx 的开源解决方案。
这是在服务器上完成的,所以没有安装 Word。
我也面临同样的问题。如果要将 .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;
}
}
}
它也对你有用..
OpenXML SDK
如果你想要开源,你可以使用。否则 .NET 中有一个使用 Interop.Word API 的选项。您可以使用此 api 打开文件并将其保存为 docx。
http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word(v=office.11).aspx
但这需要在机器上安装单词。
有一个微软批量转换工具可以做到这一点。我在这里找到了参考。
否则我认为你别无选择,只能使用 Word Automation。毕竟,即使是 OpenOffice 也无法打开一些 .doc 文件并将它们转换为 .docx / OpenXML,这意味着自己编写任何类型的解析工具都会很麻烦。