将JSON转换为Excel
Contents
[
Hide
]
Aspose.Cells支持将Json(JavaScript对象表示法)文件转换为Excel工作簿。
将JSON转换为Excel工作簿
不必纠结如何将JSON转换为Excel文件,因为Aspose.Cells Java库有最佳决策。Aspose.Cells Java API支持将JSON格式转换为电子表格。您可以使用JsonLoadOptions类指定导入JSON到工作簿的附加设置。
以下示例演示了将JSON导入Excel工作簿的代码。请查看代码以将source file转换为代码生成的xlsx文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Load Source JSON file | |
Workbook workbook = new Workbook("sample.json"); | |
//Save file to xlsx format | |
workbook.save("sample_out.xlsx"); |
以下使用JsonLoadOptions类指定附加设置的示例代码演示了将JSON导入到Excel工作簿。请参阅用于参考的代码将源文件转换为由代码生成的xlsx文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create an options of loading the file. | |
JsonLoadOptions options = new JsonLoadOptions(); | |
//Indicates whether importing each attribute of JsonObject object as one worksheet when all child nodes are array nodes. | |
options.setMultipleWorksheets(true); | |
Workbook book = new Workbook("sample.json", options); | |
//save file to xlsx format | |
book.save("sample_out2.xlsx"); |
以下代码示例演示了将JSON字符串导入到Excel工作簿。您还可以在导入JSON时指定布局的位置。请参阅用于参考的代码将JSON字符串转换为由代码生成的xlsx文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String inputJson = "[" + | |
" { BEFORE: 'before cell', TEST: 'asd1', AFTER: 'after cell' },"+ | |
" { BEFORE: 'before cell', TEST: 'asd2', AFTER: 'after cell' },"+ | |
" { BEFORE: 'before cell', TEST: 'asd3', AFTER: 'after cell' },"+ | |
" { BEFORE: 'before cell', TEST: 'asd4', AFTER: 'after cell' }"+ | |
" ]"; | |
String sheetName = "Sheet1"; | |
int row = 3; | |
int column = 2; | |
//create a Workbook object | |
Workbook book = new Workbook(); | |
Worksheet worksheet = book.getWorksheets().get(sheetName); | |
//set JsonLayoutOptions to treat Arrays as Table | |
JsonLayoutOptions jsonLayoutOptions = new JsonLayoutOptions(); | |
jsonLayoutOptions.setArrayAsTable(true); | |
JsonUtility.importData(inputJson, worksheet.getCells(), row, column, jsonLayoutOptions); | |
//save file to xlsx format | |
book.save("out.xlsx"); |