我面临两个问题,其中一个问题需要解决才能使我的项目正常工作。
这些问题如下:
如何读取“.doc”文件,不使用Word自动化或任何付费的SDK,如Aspose.Words。
(如果第一个问题不可行,则)
如何将“.doc”文件转换为“.docx”?不使用Word自动化或任何付费的SDK,如Aspose.Words。
我已经搜索了很多,只找到了.docx的开源解决方案。
由于服务器上没有安装Word,因此需要在服务器上完成此操作。
我面临两个问题,其中一个问题需要解决才能使我的项目正常工作。
这些问题如下:
如何读取“.doc”文件,不使用Word自动化或任何付费的SDK,如Aspose.Words。
(如果第一个问题不可行,则)
如何将“.doc”文件转换为“.docx”?不使用Word自动化或任何付费的SDK,如Aspose.Words。
我已经搜索了很多,只找到了.docx的开源解决方案。
由于服务器上没有安装Word,因此需要在服务器上完成此操作。
它不需要在服务器上安装任何Office应用程序。
b2xtranslator.WordprocessingMLMapping.Converter.Convert(new WordDocument(StructuredStorageReader(fileName)), WordprocessingDocument.Create(fileName + "x", DocumentType.Document))
。 - Keith我也遇到了同样的问题。如果您想将.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。
有一个名为Microsoft批量转换工具的工具可以做到这一点。我在这里找到了参考资料。
否则,我认为你别无选择,只能使用Word自动化。毕竟,即使是OpenOffice也难以打开某些.doc文件并将它们转换为.docx / OpenXML,这意味着编写任何类型的解析工具都会很麻烦。