zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java版Word开发工具Aspose.Words基础教程:检测文件格式并检查格式兼容性

JAVA 检测 格式 word 检查 基础教程 开发工具 兼容性
2023-09-11 14:14:49 时间

Aspose.Words for Java是功能丰富的文字处理API,开发人员可以在自己的Java应用程序中嵌入生成,修改,转换,呈现和打印Microsoft Word支持的所有格式的功能。它不依赖于Microsoft Word,但是它提供了Microsoft Word通过其API支持的功能。

Aspose.Words for Java最新下载https://www.evget.com/product/564/download


有时有必要在打开之前确定文档的格式,因为文件扩展名不能保证文件的内容适当。例如,众所周知,Crystal Reports经常以RTF格式输出文档,但是给它们提供.doc扩展名。

如果您不确定文件的实际内容是什么,Aspose.Words可以获取有关文件类型的信息,从而避免出现异常。

检测文件格式无异常

当您处理各种文件格式的多个文档时,可能需要将Aspose.Words可以处理的文件与不能处理的文件分开。您可能还想知道为什么某些文档无法处理。

如果您尝试将文件加载到 Document 对象中,而Aspose.Words无法识别该文件格式或不支持该格式,则Aspose.Words将引发异常。您可以捕获这些异常并进行分析,但是Aspose.Words还提供了DetectFileFormat方法,该 方法使我们可以快速确定文件格式,而无需加载可能存在异常的文档。此方法返回一个 FileFormatInfo 对象,该对象包含检测到的有关文件类型的信息。

检查文件格式兼容性

我们可以检查所选文件夹中所有文件的格式兼容性,然后按格式将它们分类到相应的子文件夹中。

由于我们正在处理文件夹中的内容,因此我们要做的第一件事是使用 Directory类的GetFiles方法(从System.IO命名空间)获得此文件夹中所有文件的集合。

下面的代码示例演示如何获取文件夹中所有文件的列表:

File [] fileList =  new  File(dataDir).listFiles();

收集所有文件后,其余工作由DetectFileFormat 方法完成,该方法检查文件格式。下面的代码示例演示如何遍历收集的文件列表,检查每个文件的格式以及将每个文件移动到适当的文件夹:

// The path to the documents directory.
String dataDir = Utils.getDataDir(CheckFormatCompatibility.class);

String supportedDir = dataDir + "OutSupported" + File.separator;
String unknownDir = dataDir + "OutUnknown" + File.separator;
String encryptedDir = dataDir + "OutEncrypted" + File.separator;
String pre97Dir = dataDir + "OutPre97" + File.separator;

File[] fileList = new File(dataDir).listFiles();

// Loop through all found files.
for (File file : fileList) {
	if (file.isDirectory())
		continue;

	// Extract and display the file name without the path.
	String nameOnly = file.getName();
	System.out.print(nameOnly);

	// Check the file format and move the file to the appropriate folder.
	String fileName = file.getPath();
	FileFormatInfo info = FileFormatUtil.detectFileFormat(fileName);

	// Display the document type.
	switch (info.getLoadFormat()) {
	case LoadFormat.DOC:
		System.out.println("\tMicrosoft Word 97-2003 document.");
		break;
	case LoadFormat.DOT:
		System.out.println("\tMicrosoft Word 97-2003 template.");
		break;
	case LoadFormat.DOCX:
		System.out.println("\tOffice Open XML WordprocessingML Macro-Free Document.");
		break;
	case LoadFormat.DOCM:
		System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Document.");
		break;
	case LoadFormat.DOTX:
		System.out.println("\tOffice Open XML WordprocessingML Macro-Free Template.");
		break;
	case LoadFormat.DOTM:
		System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Template.");
		break;
	case LoadFormat.FLAT_OPC:
		System.out.println("\tFlat OPC document.");
		break;
	case LoadFormat.RTF:
		System.out.println("\tRTF format.");
		break;
	case LoadFormat.WORD_ML:
		System.out.println("\tMicrosoft Word 2003 WordprocessingML format.");
		break;
	case LoadFormat.HTML:
		System.out.println("\tHTML format.");
		break;
	case LoadFormat.MHTML:
		System.out.println("\tMHTML (Web archive) format.");
		break;
	case LoadFormat.ODT:
		System.out.println("\tOpenDocument Text.");
		break;
	case LoadFormat.OTT:
		System.out.println("\tOpenDocument Text Template.");
		break;
	case LoadFormat.DOC_PRE_WORD_60:
		System.out.println("\tMS Word 6 or Word 95 format.");
		break;
	case LoadFormat.UNKNOWN:
	default:
		System.out.println("\tUnknown format.");
		break;
	}

	// Now copy the document into the appropriate folder.
	if (info.isEncrypted()) {
		System.out.println("\tAn encrypted document.");
		fileCopy(fileName, new File(encryptedDir, nameOnly).getPath());
	} else {
		switch (info.getLoadFormat()) {
		case LoadFormat.DOC_PRE_WORD_60:
			fileCopy(fileName, new File(pre97Dir + nameOnly).getPath());
			break;
		case LoadFormat.UNKNOWN:
			fileCopy(fileName, new File(unknownDir + nameOnly).getPath());
			break;
		default:
			fileCopy(fileName, new File(supportedDir + nameOnly).getPath());
			break;
		}
	}
}

使用File类的Move方法将文件从相同的System.IO名称空间移动到适当的子文件夹中。上面的示例中使用了以下文件。文件名在左边,描述在右边:

文件组输入文件类型
支持的文件格式Test File (Doc).docMicrosoft Word 95 / 6.0或Microsoft Word 97 – 2003文档。
Test File (Dot).dotMicrosoft Word 95 / 6.0或Microsoft Word 97 – 2003模板。
Test File (Docx).docx没有宏的Office Open XML WordprocessingML文档。
Test File (Docm).docm带有宏的Office Open XML WordprocessingML文档。
Test File (Dotx).dotxOffice Open XML WordprocessingML模板。
Test File (Dotm).dotm带有宏的Office Open XML WordprocessingML模板。
Test File (XML).xmlFlatOPC OOXML文档。
Test File (RTF).rtf富文本格式文档。
Test File (WordML).xmlMicrosoft Word 2003 WordprocessingML文档。
Test File (HTML).htmlHTML文档。
Test File (MHTML).mhtmlMHTML(网络存档)文档。
Test File (Odt).odtOpenDocument文本(OpenOffice Writer)。
Test File (Ott).ottOpenDocument文档模板。
Test File (DocPreWord60).docMicrosoft Word 2.0文档。
加密文件Test File (Enc).doc加密的Microsoft Word 95 / 6.0或Microsoft Word 97 – 2003文档。
Test File (Enc).docx加密的Office Open XML WordprocessingML文档。
不支援的档案格式Test File (JPG).jpgJPEG图像文件。