Skip to content

Commit 7e9e381

Browse files
authored
Merge pull request #196 from SyncfusionExamples/41924-JSONtoCSVExample
41924-Prepare UG documentation for converting JSON to CSV
2 parents 0ffe33d + 2c71cff commit 7e9e381

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36127.28 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSON to CSV", "JSON to CSV\JSON to CSV.csproj", "{30711D59-1CD1-4D4F-A418-10F6E91247C6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{30711D59-1CD1-4D4F-A418-10F6E91247C6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E22A3F55-7C52-46AD-8EA4-5F5BEB67552B}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "Id": 1, "Name": "Apple", "Price": 0.99 },
3+
{ "Id": 2, "Name": "Banana", "Price": 0.59 },
4+
{ "Id": 3, "Name": "Cherry", "Price": 2.99 }
5+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>JSON_to_CSV</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Newtonsoft.Json" Version="*" />
13+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="Output\*">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>

FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Output/.gitkeep

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Data;
2+
using Newtonsoft.Json;
3+
using Syncfusion.XlsIO;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
//Load JSON file
10+
string jsonPath = Path.GetFullPath("Data/Input.json");
11+
string jsonData = File.ReadAllText(jsonPath);
12+
13+
//Deserialize to DataTable
14+
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonData);
15+
16+
//Initialize ExcelEngine
17+
using (ExcelEngine excelEngine = new ExcelEngine())
18+
{
19+
IApplication application = excelEngine.Excel;
20+
application.DefaultVersion = ExcelVersion.Xlsx;
21+
IWorkbook workbook = application.Workbooks.Create(1);
22+
IWorksheet sheet = workbook.Worksheets[0];
23+
24+
//Import DataTable to worksheet
25+
sheet.ImportDataTable(dataTable, true, 1, 1);
26+
27+
//Saving the workbook as CSV
28+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite);
29+
workbook.SaveAs(outputStream, ",");
30+
31+
//Dispose streams
32+
outputStream.Dispose();
33+
}
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# How to convert JSON document to CSV format document?
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
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).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System.Data;
13+
using Newtonsoft.Json;
14+
using Syncfusion.XlsIO;
15+
```
16+
17+
Step 5: Include the below code snippet in **Program.cs** to convert JSON document to CSV format document.
18+
```csharp
19+
//Load JSON file
20+
string jsonPath = Path.GetFullPath("Data/Input.json");
21+
string jsonData = File.ReadAllText(jsonPath);
22+
23+
//Deserialize to DataTable
24+
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonData);
25+
26+
//Initialize ExcelEngine
27+
using (ExcelEngine excelEngine = new ExcelEngine())
28+
{
29+
IApplication application = excelEngine.Excel;
30+
application.DefaultVersion = ExcelVersion.Xlsx;
31+
IWorkbook workbook = application.Workbooks.Create(1);
32+
IWorksheet sheet = workbook.Worksheets[0];
33+
34+
//Import DataTable to worksheet
35+
sheet.ImportDataTable(dataTable, true, 1, 1);
36+
37+
//Saving the workbook as CSV
38+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite);
39+
workbook.SaveAs(outputStream, ",");
40+
41+
//Dispose streams
42+
outputStream.Dispose();
43+
}
44+
```

0 commit comments

Comments
 (0)