![Convert JSON to Excel Linux](images/Convert-JSON-to-Excel-C.png#center)
MS Excel is a rich spreadsheet manipulation software that allows you to organize the data in the form of worksheets and workbooks. Alongside, it provides features such as data sorting, graph plotting, mathematical computations and etc. On the other hand, JSON is a popular format to store structured data. In certain cases, JSON data needs to be converted to Excel worksheets. For such cases, this article shows how to import data from JSON files to Excel worksheets on Linux.
.NET JSON to Excel Converter API for Linux
For importing data from JSON files to Excel worksheets, we will use Aspose.Cells for .NET. It is a feature-rich API that allows you to create, manipulate, and convert Excel files. In order to use this API on Linux, you need to set up .NET environment, which can be done within a few steps. The following are the prerequisites that you would need to set up the environment.
Once you have set up the environment and created the application, you can proceed to install Aspose.Cells for .NET via NuGet using NuGet Package Manager extension for Visual Studio Code.
Learn how to create a console application on Linux using Visual Studio Code.
Import Data from JSON to Excel on Linux
The following are the steps to import data from JSON to Excel XLSX:
- Create a new Workbook object.
- Read data from JSON using System.IO.File.ReadAllText(string) method.
- Create an object of JsonLayoutOptions class to set additional options.
- Import data from JSON to Excel using JsonUtility.ImportData() method.
- Save the Excel file using Workbook.Save(string) method.
The following code sample shows how to import data from JSON to Excel on Linux.
// Create a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Read JSON File | |
string jsonInput = File.ReadAllText("Data.json"); | |
// Set JsonLayoutOptions | |
JsonLayoutOptions options = new JsonLayoutOptions(); | |
options.ArrayAsTable = true; | |
// Import JSON Data | |
JsonUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, options); | |
// Save Excel file | |
workbook.Save("Import-Data-JSON-To-Excel.xlsx"); |
Input JSON Data
[
{
Name: "John Smith",
Contract:
[
{
Client:
{
Name: "A Company"
},
Price: 1200000
},
{
Client:
{
Name: "B Ltd."
},
Price: 750000
},
{
Client:
{
Name: "C & D"
},
Price: 350000
}
]
},
{
Name: "Tony Anderson",
Contract:
[
{
Client:
{
Name: "E Corp."
},
Price: 650000
},
{
Client:
{
Name: "F & Partners"
},
Price: 550000
}
]
},
{
Name: "Jimmy Adams",
Contract:
[
{
Client:
{
Name: "MIT"
},
Price: 800000
},
{
Client:
{
Name: "SnB"
},
Price: 520000
}
]
},
]
Output Excel Worksheet
![JSON to Excel Linux](images/JSON-to-Excel.png#center)
Import JSON to Excel with Formatting Styles
While importing the data from the JSON file, you can apply styles to the output Excel file. The following are the steps to perform this operation.
- Create a new Workbook object.
- Read the JSON file using System.IO.File.ReadAllText(string) method.
- Create an object of the CellsFactory class.
- Create a Style object using CreateStyle() method.
- Set the desired properties such as Style.Font.Color.
- Import data from JSON to Excel using JsonUtility.ImportData() method.
- Save the Excel file using Workbook.Save(string) method.
The following code sample shows how to apply styles while importing data from JSON to Excel on Linux.
// Create a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Read JSON File | |
string jsonInput = File.ReadAllText("Data.json"); | |
// Set Styles | |
CellsFactory factory = new CellsFactory(); | |
Style style = factory.CreateStyle(); | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
style.Font.Color = System.Drawing.Color.BlueViolet; | |
style.Font.IsBold = true; | |
// Set JsonLayoutOptions | |
JsonLayoutOptions options = new JsonLayoutOptions(); | |
options.TitleStyle = style; | |
options.ArrayAsTable = true; | |
// Import JSON Data | |
JsonUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, options); | |
// Save Excel file | |
workbook.Save("Import-Data-JSON-To-Excel.xlsx"); |
Output Excel Worksheet
![JSON to Excel Linux](images/JSON-to-Excel-C.png#center)
Get a Free License
You can get a temporary license and use Aspose.Cells for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to import JSON data to Excel worksheets on Linux. In addition, you have seen how to apply styles in JSON to Excel conversion. Furthermore, you can explore more about Aspose.Cells for .NET using the documentation. Also, you can ask your questions via our forum.