DOC和DOCX有何区别?如何使用Aspose实现快速互转?

Microsoft Word文档主要有两种类型,DOC和DOCX。可以使用C#或VB.NET以编程方式将DOCX转换为DOC,也可以将DOCX转换为DOCX。Docx厉害一点,它是被压缩过的文档,体积更小,能处理更加复杂的内容,访问速度更快。 可以逐个处理和转换每个文档,或者,如果需要处理大量文件,可以批量转换DOC和DOCX文件。让我们探索以下用例以详细了解该主题:

  • 使用C#或VB.NET将DOCX转换为DOC文件
  • 使用C#或VB.NET将DOCX批量转换为DOC文件
  • 使用C#或VB.NET将DOC转换为DOCX文件
  • 使用C#或VB.NET将DOC批量转换为DOCX

>>Aspose.Words for .NET已经更新至v20.10,此常规的每月版本中有89项改进和修复,包括添加了对加载CHM文档的支持等新功能,欢迎下载体验。(点击下载最新版

Word处理控件Aspose.Words功能演示:使用C#实现DOCX和DOC格式互转


使用C#或VB.NET将DOCX转换为DOC文件

DOC和DOCX之间的主要区别在于DOC文件格式是二进制文件,其中DOCX文件格式是包含所有相关XML文件的ZIP文件。您可以按照以下步骤将DOCX文件转换为DOC文档:

  • 指定源文件的加载格式
  • 加载输入的DOCX文件
  • 使用SaveFormat保存输出DOC文件

下面的代码段显示了如何使用C#或VB.NET将DOCX转换为DOC:

// Specify LoadFormat of input word document
Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
options.LoadFormat = Aspose.Words.LoadFormat.Docx ;

// Load source DOCX file
Aspose.Words.Document document = new Aspose.Words.Document(dataDir + "file.doc", options);

// Convert DOCX to DOC file
document.Save(dataDir + "test.doc", Aspose.Words.SaveFormat.Docx);

使用C#或VB.NET将DOCX批量转换为DOC文件

可能需要在.NET应用程序中将数百个DOCX文件转换为DOC格式。您只需执行一次程序即可轻松转换一批文件。请按照以下步骤转换一批DOCX文件:

  • 从目录或文件夹加载所有DOCX文件
  • 使用LoadOptions类加载每个DOCX文件
  • 将每个输入文件的扩展名更改为DOC
  • 将输出文件另存为DOC格式文件

以下代码显示了如何使用C#或VB.NET将一批DOCX文件转换为DOC格式文件:

string[] files = Directory.GetFiles(dataDir, "*.docx");
foreach (var file in files)
{
    // Specify LoadFormat of input word document
    Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
    options.LoadFormat = Aspose.Words.LoadFormat.Docx;

    // Load source DOCX file
    Aspose.Words.Document document = new Aspose.Words.Document(file, options);

    var filename = Path.ChangeExtension(file, ".doc");

    // Convert DOCX to DOC file
    document.Save(filename, Aspose.Words.SaveFormat.Doc);
}

使用C#或VB.NET将DOC转换为DOCX文件

可以使用C#或VB.NET在基于.NET Framework的应用程序中将Microsoft Word DOC文件转换为DOCX格式文件。以下是执行转换的步骤:

  • 设置输入DOC文件的LoadFormat
  • 使用Document类初始化源文档
  • 将输出文件保存为DOCX格式

下面的代码显示了如何使用C#或VB.NET将DOC转换为DOCX:

// Specify LoadFormat of input word document
Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
options.LoadFormat = Aspose.Words.LoadFormat.Doc;

// Load source DOC file
Aspose.Words.Document document = new Aspose.Words.Document(dataDir + "test.doc" , options);

// Convert DOC to DOCX file
document.Save(dataDir + "test.docx" , Aspose.Words.SaveFormat.Docx);

使用C#或VB.NET将DOC批量转换为DOCX

可以处理将多个文件和不同批次转换为DOC到DOCX的转换。您需要按照以下步骤转换此类文件:

  • 从目录加载所有DOC文件
  • 从LoadFormat枚举设置DOC值
  • 加载输入文件
  • 使用SaveFormat保存输出DOCX文件

下面的代码示例说明了如何使用C#或VB.NET将一批DOC文件转换为DOCX文件:

string[] files = Directory.GetFiles(dataDir, "*.doc");
foreach (var file in files)
{
    // Specify LoadFormat of input word document
    Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
    options.LoadFormat = Aspose.Words.LoadFormat.Doc;

    // Load source DOC file
    Aspose.Words.Document document = new Aspose.Words.Document(file, options);

    var filename = Path.ChangeExtension(file, ".docx");

    // Convert DOC to DOCX file
    document.Save(filename, Aspose.Words.SaveFormat.Docx);
}

如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。