通过 Java 将 DOCM 转换为 JSON 格式

无需使用 Microsoft® Word 或 Microsoft® Excel 即可将 DOCM 转换为 JSON 的 On Premise Java API

 

通过 Aspose.Total for Java 将 DOCM 转换为 JSON 格式是一个简单的两步过程。通过使用功能丰富的文档操作和转换 API Aspose.Words for Java ,您可以将 DOCM 导出为 HTML。之后,通过使用 Aspose.Cells for Java ,您可以将 HTML 转换为 JSON。

通过 Java 将 DOCM 转换为 JSON 格式

  1. 使用 Document 类打开 DOCM 文件
  2. 使用 [Save]( https://reference.aspose.com/words/java/com.aspose.words/Document#save(java.lang.String,com.aspose.words.SaveOptions) 将 DOCM 转换为 HTML ) 方法
  3. 使用 Workbook 类加载 HTML 文档
  4. 使用 [Save]( https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.lang.String,%20com.aspose.cells.) 将文档保存为 JSON 格式方法

转换要求

从您可以直接从基于 Maven 的项目轻松地使用 Aspose.Total for Java 并在您的 pom.xml 中包含库。

或者,您可以从 下载 获取 ZIP 文件。

// supports DOC, DOT, DOCX, DOCM, DOTX, DOTM, RTF, WordML, MOBI, ODT, and OTT input file formats
// load DOCX with an instance of Document
Document document = new Document("template.docx");
// call Save method while passing SaveFormat.HTML
document.save("html_output.html",SaveFormat.HTML);
// load the HTML file in an instance of Workbook
Workbook book = new Workbook("html_output.html");
// save HTML as JSON
book.save("output.json", SaveFormat.JSON);

通过 Java 将受保护的 DOCM 转换为 JSON 格式

使用 API,您还可以打开受密码保护的文档。如果您的输入 DOCM 文档受密码保护,则您无法在不使用密码的情况下将其转换为 JSON 格式。 API 允许您通过在 LoadOptions 对象中传递正确的密码来打开加密的文档。以下代码示例显示了如何尝试使用密码打开加密文档:

// supports DOC, DOT, DOCX, DOCM, DOTX, and DOTM file formats
// load DOCX with an instance of Document
Document document = new Document("template.docx", new LoadOptions("MyPassword"));
// call Save method while passing SaveFormat.HTML
document.save("html_output.html",SaveFormat.HTML);
// load the HTML file in an instance of Workbook
Workbook book = new Workbook("html_output.html");
// save HTML as JSON
book.save("output.json", SaveFormat.JSON);

通过 Java 将 DOCM 转换为 Range 中的 JSON

在将 DOCM 转换为 JSON 时,您还可以将范围设置为输出 JSON 格式。为了设置范围,您可以使用 Workbook 类打开转换后的 HTML,使用 Cells.createRange 方法创建要导出的数据范围,使用 Range 和 ExportRangeToJsonOptions 的引用调用 JsonUtility.exportRangeToJson 方法并将字符串 JSON 数据写入文件通过BufferedWriter.write 方法。

// supports DOC, DOT, DOCX, DOCM, DOTX, DOTM, RTF, WordML, MOBI, ODT, and OTT input file formats
// load DOCX with an instance of Document
Document document = new Document("template.docx");
// call Save method while passing SaveFormat.HTML
document.save("html_output.html",SaveFormat.HTML);
// load the HTML file in an instance of Workbook
Workbook book = new Workbook("html_output.html");
// access CellsCollection of the worksheet containing data to be converted
Cells cells = workbook.getWorksheets().get(0).getCells();
// create & set ExportRangeToJsonOptions for advanced options
ExportRangeToJsonOptions exportOptions = new ExportRangeToJsonOptions();
// create a range of cells containing data to be exported
Range range = cells.createRange(0, 0, cells.getLastCell().getRow() + 1, cells.getLastCell().getColumn() + 1);
// export range as JSON data
String jsonData = JsonUtility.exportRangeToJson(range, exportOptions);
// write data to disc in JSON format
BufferedWriter writer = new BufferedWriter(new FileWriter("output.json"));
writer.write(jsonData);
writer.close();

使用Java 探索DOCM转换选项

将DOCM转换为EXCEL (电子表格文件格式)