您可以通过 Aspose.Total for Android via Java 在您的 Android 应用程序中将 DOT 转换为 JSON 格式。通过使用文档操作和转换 API Aspose.Words for Android via Java ,您可以将 DOT 导出为 HTML。之后,通过使用 Aspose.Cells for Android via Java ,您可以将 HTML 转换为 JSON。
在 Android 中将 DOT 转换为 JSON 格式
- 使用 Dotument 类打开 DOT 文件
- 使用 [Save]( https://reference.aspose.com/words/java/com.aspose.words/Dotument#save(java.lang.String,com.aspose.words.SaveOptions) 将 DOT 转换为 HTML ) 方法
- 使用 Workbook 类加载 HTML 文档
- 使用 Save 将文档保存为 JSON 格式方法
// 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 在 Android 中将受保护的 DOT 转换为 JSON 格式
使用 API,您还可以打开受密码保护的文档。如果您的输入 DOT 文档受密码保护,则您无法在不使用密码的情况下将其转换为 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在Android中将DOT转换为范围内的JSON
在将 DOT 转换为 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(); |