![](https://img.51dongshi.com/20250108/wz/18369965052.jpg)
文件檢驗教程(文件檢驗教程 莊琳)Aspose.Words無需Microsoft Word也可在任何平臺上滿足Word文檔的一切操作需求。本文將與大家分享如何檢測文件格式和檢查格式兼容性。Aspose.Words for .NET官方最新版免費下載試用,歷史版本下載,在線文檔和幫助文件下載-慧都網有時我們需要在打開文件之前檢測文檔文件的格式,電腦因為文件擴展名不能保證文件內容是合適的。例如,大家都知道,Crystal Reports通常以RTF格式輸出文檔,但文檔的擴展名卻是.doc。因此,如果你不確定文件的實際內容是什么并且希望在打開文件過程中不要出現異常,則可以使用FileFormatUtil.DetectFileFormat方法。 這是一個靜態(tài)方法,它接受包含文件數據的文件名或流對象。該方法返回一個FileFormatInfo對象,該對象包含檢測到的有關文件類型的信息。當你處理各種文件格式的多個文檔時,你可能需要將那些可以由Aspose.Words處理的文件和那些不能處理的文件分開。你可能還想知道為什么某些文檔無法處理。如果你嘗試將文件加載到Document對象中并且Aspose.Words無法識別文件格式或不支持該格式,Aspose.Words將出現異常。你可以記錄這些異常并對其進行分析,但Aspose.Words還提供了一種專門的方法,可以快速確定文件格式,而不需要加載可能有異常的文檔。我們將在代碼中執(zhí)行以下步驟,以檢查所選文件夾中所有文件的格式兼容性,并按文件格式將它們排序到適當的子文件夾中。獲取所選文件夾中所有文件的集合。循環(huán)收集。對于每個文件:檢查文件格式。顯示檢查結果。將文件移動到適當的文件夾。此示例中使用了以下文件。文件名在左側,其描述在右側。測試支持的文件格式:輸入文件類型測試文件(XML).xmlFlatOPC OOXML文檔。測試文件(WordML).xmlMicrosoft Word 2003 電腦 WordprocessingML文檔。測試文件(rtf).rtf富文本格式文檔。測試文件(odt).odtOpenDocument文本格式(OpenOffice Writer)。測試文件(MHTML).mhtmlMHTML(Web檔案)文檔。測試文件(HTML).htmlHTML文檔。測試文件(dotx).dotxOffice Open XML WordprocessingML模板。測試文件(dot).dotMicrosoft Word 97 - 2003模板測試文件(docx).docx沒有宏的Office Open XML WordprocessingML文檔。測試文件(docm).docm有宏的Office Open XML WordprocessingML文檔。測試文件(doc).docMicrosoft Word 97 - 2003文檔。測試加密文檔:電腦輸入文件類型測試文件(enc).doc加密的Microsoft Word 97 - 2003文檔。測試文件(enc).docx加密的Office Open XML WordprocessingML文檔。不支持的文件格式:輸入文件類型測試文件(pre97).docMicrosoft Word 95文檔。測試文件(JPG).jpgJPEG圖像文件。當我們處理文件夾中的內容時,我們首先要做的是使用Directory類的GetFiles方法(來自System.IO命名空間)獲取此文件夾中所有文件的集合。收集完所有文件后,其余工作由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 existif (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:GetListOfFilesInFolderstring[] 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:CheckFormatCompatibilityConsole.WriteLine("\nChecked the format of all documents successfully.");}}}