Convertir-JSON-a-Excel

Convertir JSON a libro de Excel

No es necesario preguntarse cómo convertir JSON a un archivo de Excel, porque la biblioteca Aspose.Cells Java tiene la mejor decisión. El Aspose.Cells Java API brinda soporte para convertir el formato JSON a hojas de cálculo. Puedes usarJsonLoadOptions class para especificar configuraciones adicionales para importar JSON a Workbook.

El siguiente ejemplo de código muestra la importación de JSON a Excel Workbook. Por favor vea el código para convertirarchivo fuente al archivo xlsx generado por el código como referencia.

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

El siguiente ejemplo de código que usa la clase JsonLoadOptions para especificar configuraciones adicionales demuestra la importación de JSON a Excel Workbook. Por favor vea el código para convertirarchivo fuente al archivo xlsx generado por el código como referencia.

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

El siguiente ejemplo de código muestra la importación de la cadena JSON a un libro de Excel. También puede especificar la ubicación del diseño al importar JSON. Consulte el código para convertir la cadena JSON en el archivo xlsx generado por el código como referencia.

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