Aspose.Words可以满足Word文档在任何平台上无需Microsoft Word的所有操作要求。 本文将与大家分享如何检查文件格式以及检测格式兼容性。
Aspose.Words for .NET官方最新版免费下载试用、历史版本下载、在线文档和帮助文件下载-huidu.com
有时我们需要在打开文件之前检查文档文件的格式,因为文件扩展名并不能保证文件内容合适。
例如,大家都知道水晶报表通常输出RTF格式的文档,但文档的扩展名是.doc。 因此,如果您不确定文件的实际内容是什么并且希望无异常地打开文件,则可以使用 FileFormatUtil.DetectFileFormat 技巧。 这是一个静态方法html 选择文件夹,接受文件名或包含文件数据的流对象。 此方法返回一个 FileFormatInfo 对象html 选择文件夹,其中包含有关文件类型的测量信息。
当您处理不同文件格式的多个文档时,您可能需要将可以由 Aspose.Words 处理的文件与不能处理的文件分开。 您可能还想知道为什么单个文档难以处理。
如果您尝试将文档加载到 Document 对象中,而 Aspose.Words 无法识别该文档格式或不支持该格式,Aspose.Words 将抛出异常。 您可以记录此异常并对其进行剖析,但 Aspose.Words 还提供了一种专门的方法来快速确定文件格式,而无需加载潜在的异常文档。
我们将在代码中执行以下步骤来检测所选文件夹中所有文件的格式兼容性,并按文件格式将它们分类到适当的子文件夹中。
获取所选文件夹中所有文件的集合。 循环收集。 对于每个文件:检查文件格式。 显示检测结果。 将文件链接到适当的文件夹。
本示例中使用了以下文件。 文件名位于右侧,其描述位于左侧。 测试支持的文件格式:
输入文件
类型
测试文件(xml).xml
FlatOPC OOXML 文档。
测试文件 (WordML).xml
Microsoft Word 2003 WordprocessingML 文档。
测试文件(rtf).rtf
富文本文档。
测试文件 (odt).odt
OpenDocument 文本格式(OpenOffice Writer)。
测试文件 (MHTML).mhtml
MHTML(网络存档)文档。
测试文件 (HTML).html
HTML 文档。
测试文件 (dotx).dotx
Office Open XML WordprocessingML 模板。
测试文件(dot).dot
Microsoft Word 97-2003 模板
测试文件(docx).docx
不带宏的 Office Open XML WordprocessingML 文档。
测试文档(docm).docm
有用于宏的 Office Open XML WordprocessingML 文档。
测试文件(doc).doc
Microsoft Word 97-2003 文档。
测试加密文档:
输入文件
类型
测试文件 (enc).doc
加密的 Microsoft Word 97-2003 文档。
测试文件(enc).docx
加密的 Office Open XML WordprocessingML 文档。
不支持的文件格式:
输入文件
类型
测试文件(pre97).doc
Microsoft Word 95 文档。
测试文件(JPG).jpg
JPEG 图像文件。
当我们处理文件夹的内容时,我们要做的第一件事是使用 Directory 类(来自 System.IO 命名空间)的 GetFiles 方法来获取该文件夹中所有文件的集合。
收集完所有文件后,剩下的工作由Aspose.Words组件中的FileFormatUtil.DetectFileFormat方法完成。 FileFormatUtil.DetectFileFormat 方法检测文件格式,但请注意,它仅检测文件格式,并不验证文件格式。 这意味着,虽然FileFormatUtil.DetectFileFormat的返回结果表明该文件格式是支持的格式之一,但很难保证该文件能够顺利打开。 这是因为FileFormatUtil.DetectFileFormat方法只读取了部分文件格式数据,这足以检测文件格式,但不足以完成验证。 以下代码演示了检测文件格式:
using System;
using System.Collections;
using System.IO;
using Aspose.Words;
using Aspose.Words.Tables;
using System.Diagnostics;
namespace Aspose.Words.Examples.CSharp.Loading_Saving
{
class CheckFormat
{
public static void Run()
{
// ExStart:CheckFormatCompatibility
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string supportedDir = dataDir + "OutSupported";
string unknownDir = dataDir + "OutUnknown";
string encryptedDir = dataDir + "OutEncrypted";
string pre97Dir = dataDir + "OutPre97";
// Create the directories if they do not already exist
if (Directory.Exists(supportedDir) == false)
Directory.CreateDirectory(supportedDir);
if (Directory.Exists(unknownDir) == false)
Directory.CreateDirectory(unknownDir);
if (Directory.Exists(encryptedDir) == false)
Directory.CreateDirectory(encryptedDir);
if (Directory.Exists(pre97Dir) == false)
Directory.CreateDirectory(pre97Dir);
// ExStart:GetListOfFilesInFolder
string[] fileList = Directory.GetFiles(dataDir);
// ExEnd:GetListOfFilesInFolder
// Loop through all found files.
foreach (string fileName in fileList)
{
// Extract and display the file name without the path.
string nameOnly = Path.GetFileName(fileName);
Console.Write(nameOnly);
// ExStart:DetectFileFormat
// Check the file format and move the file to the appropriate folder.
FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);
// Display the document type.
switch (info.LoadFormat)
{
case LoadFormat.Doc:
Console.WriteLine("tMicrosoft Word 97-2003 document.");
break;
case LoadFormat.Dot:
Console.WriteLine("tMicrosoft Word 97-2003 template.");
break;
case LoadFormat.Docx:
Console.WriteLine("tOffice Open XML WordprocessingML Macro-Free Document.");
break;
case LoadFormat.Docm:
Console.WriteLine("tOffice Open XML WordprocessingML Macro-Enabled Document.");
break;
case LoadFormat.Dotx:
Console.WriteLine("tOffice Open XML WordprocessingML Macro-Free Template.");
break;
case LoadFormat.Dotm:
Console.WriteLine("tOffice Open XML WordprocessingML Macro-Enabled Template.");
break;
case LoadFormat.FlatOpc:
Console.WriteLine("tFlat OPC document.");
break;
case LoadFormat.Rtf:
Console.WriteLine("tRTF format.");
break;
case LoadFormat.WordML:
Console.WriteLine("tMicrosoft Word 2003 WordprocessingML format.");
break;
case LoadFormat.Html:
Console.WriteLine("tHTML format.");
break;
case LoadFormat.Mhtml:
Console.WriteLine("tMHTML (Web archive) format.");
break;
case LoadFormat.Odt:
Console.WriteLine("tOpenDocument Text.");
break;
case LoadFormat.Ott:
Console.WriteLine("tOpenDocument Text Template.");
break;
case LoadFormat.DocPreWord60:
Console.WriteLine("tMS Word 6 or Word 95 format.");
break;
case LoadFormat.Unknown:
default:
Console.WriteLine("tUnknown format.");
break;
}
// ExEnd:DetectFileFormat
// Now copy the document into the appropriate folder.
if (info.IsEncrypted)
{
Console.WriteLine("tAn encrypted document.");
File.Copy(fileName, Path.Combine(encryptedDir, nameOnly), true);
}
else
{
switch (info.LoadFormat)
{
case LoadFormat.DocPreWord60:
File.Copy(fileName, Path.Combine(pre97Dir, nameOnly), true);
break;
case LoadFormat.Unknown:
File.Copy(fileName, Path.Combine(unknownDir, nameOnly), true);
break;
default:
File.Copy(fileName, Path.Combine(supportedDir, nameOnly), true);
break;
}
}
}
// ExEnd:CheckFormatCompatibility
Console.WriteLine("nChecked the format of all documents successfully.");
}
}
}