JSONをExcelに変換する

JSONをExcelワークブックに変換する

Aspose.Cells Javaライブラリが最適な決定を提供しているため、JSONをExcelファイルに変換する方法を悩む必要はありません。Aspose.Cells Java APIは、JSON形式をスプレッドシートに変換するサポートを提供します。ワークブックにJSONをインポートするための追加の設定には、JsonLoadOptionsクラスを使用できます。

以下のコード例は、JSONをExcelワークブックにインポートする方法を示しています。コードで生成されたxlsxファイルへの変換のために、source fileを参照してください。

//Load Source JSON file
Workbook workbook = new Workbook("sample.json");
//Save file to xlsx format
workbook.save("sample_out.xlsx");

以下のコード例は、JsonLoadOptionsクラスを使用して追加の設定を指定することで、JSONをExcelワークブックにインポートする方法を示しています。コードで生成されたxlsxファイルへの変換のために、source fileを参照してください。

//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のインポート時にレイアウトの場所を指定することもできます。コードで生成されたxlsxファイルへの変換のために、JSON文字列を参照してください。

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");