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

4

我面临两个问题,其中一个问题需要解决才能使我的项目正常工作。

这些问题如下:

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

    (如果第一个问题不可行,则)

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

我已经搜索了很多,只找到了.docx的开源解决方案。

由于服务器上没有安装Word,因此需要在服务器上完成此操作。


参考线程 - https://dev59.com/C0rSa4cB1Zd3GeqPW4bx - KV Prajapati
进一步参考线程 - https://dev59.com/63E95IYBdhLWcg3wN7Ov - Holf
5个回答

6
你或许可以尝试这个纯.NET解决方案:

b2xtranslator

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

1
能否添加一个使用示例? - zed
3
@zed 我已经将这个项目升级到了.NET Core,并提供一个示例代码:b2xtranslator.WordprocessingMLMapping.Converter.Convert(new WordDocument(StructuredStorageReader(fileName)), WordprocessingDocument.Create(fileName + "x", DocumentType.Document)) - Keith
这是 Microsoft 推荐的与本地 Office 格式交互的方式: https://learn.microsoft.com/en-us/archive/blogs/interoperability/binary-to-open-xml-b2x-translator-interoperability-for-the-office-binary-file-formats - olivier houssin

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

目前这个库似乎还不支持生产使用时读取.doc文件。 - Sam

2

我也遇到了同样的问题。如果您想将.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;




        }
    }
}

它也适用于您...。

文档.Convert()这行代码是必要的吗?它给我带来了问题,我认为.SaveAs()也应该处理转换成文件格式参数的工作。 - codemonkeyliketab

1

3
Op指定不允许安装Word。 - Andrew Barber

1

有一个名为Microsoft批量转换工具的工具可以做到这一点。我在这里找到了参考资料。

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


这需要在服务器上安装Microsoft Office兼容性包,因此这不是正确的选择。 - Ishan Dhingra
1
两点:原帖中没有指定“Word”。我认为可以独立安装MS Office兼容性包。其次,其中的实用工具,例如“' "C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme <input file> <output file>'”,可能可以单独使用或与少量依赖项一起使用。值得一试。 - Holf
我同意,但我不确定这个问题,因为这不是我的服务器,我将从托管提供商那里获得空间,所以这可能会在某些主机上引起问题。 - Ishan Dhingra
当然,我理解托管的问题,这真是一件痛苦的事情。您可能会发现,您可以将wordconv.exe(以及可能还有其他一两个依赖的dll)与您的网站一起部署,并从您的代码中执行它。不确定这在许可证方面是否可行! :-) - Holf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接