Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions FAQ/Conversions/.NET/JSON to CSV/JSON to CSV.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36127.28 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSON to CSV", "JSON to CSV\JSON to CSV.csproj", "{30711D59-1CD1-4D4F-A418-10F6E91247C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E22A3F55-7C52-46AD-8EA4-5F5BEB67552B}
EndGlobalSection
EndGlobal
5 changes: 5 additions & 0 deletions FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Data/Input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "Id": 1, "Name": "Apple", "Price": 0.99 },
{ "Id": 2, "Name": "Banana", "Price": 0.59 },
{ "Id": 3, "Name": "Cherry", "Price": 2.99 }
]
22 changes: 22 additions & 0 deletions FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/JSON to CSV.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>JSON_to_CSV</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="*" />
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Output\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Empty file.
35 changes: 35 additions & 0 deletions FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Data;
using Newtonsoft.Json;
using Syncfusion.XlsIO;

class Program
{
static void Main()
{
//Load JSON file
string jsonPath = Path.GetFullPath("Data/Input.json");
string jsonData = File.ReadAllText(jsonPath);

//Deserialize to DataTable
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonData);

//Initialize ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];

//Import DataTable to worksheet
sheet.ImportDataTable(dataTable, true, 1, 1);

//Saving the workbook as CSV
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream, ",");

//Dispose streams
outputStream.Dispose();
}
}
}
44 changes: 44 additions & 0 deletions FAQ/Conversions/.NET/JSON to CSV/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# How to convert JSON document to CSV format document?

Step 1: Create a New C# Console Application Project.

Step 2: Name the Project.

Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) and [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json) NuGet packages as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).

Step 4: Include the following namespaces in the **Program.cs** file.

```csharp
using System.Data;
using Newtonsoft.Json;
using Syncfusion.XlsIO;
```

Step 5: Include the below code snippet in **Program.cs** to convert JSON document to CSV format document.
```csharp
//Load JSON file
string jsonPath = Path.GetFullPath("Data/Input.json");
string jsonData = File.ReadAllText(jsonPath);

//Deserialize to DataTable
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonData);

//Initialize ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];

//Import DataTable to worksheet
sheet.ImportDataTable(dataTable, true, 1, 1);

//Saving the workbook as CSV
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream, ",");

//Dispose streams
outputStream.Dispose();
}
```