Convert JSON to Excel

Converti JSON in Excel Workbook

Non c’è bisogno di chiedersi come convertire JSON in file Excel, perché la libreria Aspose.Cells Java ha la soluzione migliore. L’API Aspose.Cells Java fornisce il supporto per la conversione del formato JSON in fogli di calcolo. Puoi utilizzare la classe JsonLoadOptions per specificare le impostazioni aggiuntive per l’importazione del JSON nel Workbook.

Il seguente esempio di codice dimostra l’importazione del JSON nel foglio di lavoro di Excel. Si prega di vedere il codice per convertire il file di origine nel file xlsx generato dal codice a titolo di riferimento.

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

L’esempio di codice seguente che utilizza la classe JsonLoadOptions per specificare impostazioni aggiuntive dimostra l’importazione di JSON in Excel Workbook. Si prega di consultare il codice per convertire file di origine nel file xlsx generato dal codice per riferimento.

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

L’esempio di codice seguente dimostra l’importazione di una stringa JSON in Excel Workbook. È anche possibile specificare la posizione del layout durante l’importazione di JSON. Si prega di consultare il codice per convertire la stringa JSON nel file xlsx generato dal codice per riferimento.

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