将JSON转换为Excel

将JSON转换为Excel工作簿

不需要纠结如何将JSON转换为Excel文件,因为Apose.Cells for .NET库有最佳决策。 Aspose.Cells API支持将JSON格式转换为电子表格。 您可以使用JsonLoadOptions类指定将JSON导入工作簿的其他设置。

以下代码示例演示了将JSON导入到Excel工作簿。请参阅用于参考的代码将源文件转换为由代码生成的xlsx文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// create a Workbook object
Workbook wb = new Workbook("sample.json");
//save file to xlsx format
wb.Save("sample_out.xlsx");

以下使用JsonLoadOptions类指定附加设置的示例代码演示了将JSON导入到Excel工作簿。请参阅用于参考的代码将源文件转换为由代码生成的xlsx文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//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.MultipleWorksheets = true;
Workbook book = new Workbook("sample.json", options);
//save file to xlsx format
book.Save("sample_out.xlsx");

以下代码示例演示了将JSON字符串导入到Excel工作簿。您还可以在导入JSON时指定布局的位置。请参阅用于参考的代码将JSON字符串转换为由代码生成的xlsx文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
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.Worksheets[sheetName];
// set JsonLayoutOptions to treat Arrays as Table
JsonLayoutOptions jsonLayoutOptions = new JsonLayoutOptions();
jsonLayoutOptions.ArrayAsTable = true;
JsonUtility.ImportData(inputJson, worksheet.Cells, row, column, jsonLayoutOptions);
//save file to xlsx format
book.Save("out.xlsx");