-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from SyncfusionExamples/882017-CSV-with-JSON-t…
…o-Excel 882017 - Add the samples for CSV with JSON to Excel
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...N-to-Excel/NET Standard/Convert-CSV-with-JSON-to-Excel/Convert-CSV-with-JSON-to-Excel.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.9.34310.174 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-CSV-with-JSON-to-Excel", "Convert-CSV-with-JSON-to-Excel\Convert-CSV-with-JSON-to-Excel.csproj", "{B0D94766-5A7A-46CA-97ED-434D8B9CD8F0}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B0D94766-5A7A-46CA-97ED-434D8B9CD8F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B0D94766-5A7A-46CA-97ED-434D8B9CD8F0}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B0D94766-5A7A-46CA-97ED-434D8B9CD8F0}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B0D94766-5A7A-46CA-97ED-434D8B9CD8F0}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {437408F7-FF54-40E7-B811-F0E810036BEC} | ||
EndGlobalSection | ||
EndGlobal |
15 changes: 15 additions & 0 deletions
15
...V-with-JSON-to-Excel/Convert-CSV-with-JSON-to-Excel/Convert-CSV-with-JSON-to-Excel.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<RootNamespace>Convert_CSV_with_JSON_to_Excel</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="25.2.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
12 changes: 12 additions & 0 deletions
12
...dard/Convert-CSV-with-JSON-to-Excel/Convert-CSV-with-JSON-to-Excel/Data/InputTemplate.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
NameValue;AddressValue;CityValue; "{ | ||
"Information": "", | ||
"InformationHistoryFixed": "", | ||
"customerNotes": [], | ||
"bookingNotes": [] | ||
}" | ||
Ramesh;3street;Newyork;"{ | ||
"Information": "Sample Information", | ||
"InformationHistoryFixed": "Sample History", | ||
"customerNotes": ["Note 1", "Note 2"], | ||
"bookingNotes": ["Booking 1", "Booking 2"] | ||
}" |
99 changes: 99 additions & 0 deletions
99
...cel/NET Standard/Convert-CSV-with-JSON-to-Excel/Convert-CSV-with-JSON-to-Excel/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using Syncfusion.XlsIO; | ||
|
||
namespace Convert_CSV_with_JSON_to_Excel | ||
{ | ||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
bool startConcatenation = false; | ||
string startCellAddress = ""; | ||
List<string> concatenatedValues = new List<string>(); | ||
|
||
using (ExcelEngine excelEngine = new ExcelEngine()) | ||
{ | ||
IApplication application = excelEngine.Excel; | ||
application.DefaultVersion = ExcelVersion.Xlsx; | ||
|
||
//Loads an CSV file | ||
FileStream fileStream = new FileStream(@"../../../Data/InputTemplate.csv", FileMode.Open, FileAccess.Read); | ||
IWorkbook workbook = application.Workbooks.Open(fileStream, ";"); | ||
IWorksheet worksheet = workbook.Worksheets[0]; | ||
|
||
for (int row = 1; row <= worksheet.UsedRange.LastRow; row++) | ||
{ | ||
for (int col = 1; col <= worksheet.UsedRange.LastColumn; col++) | ||
{ | ||
IRange cell = worksheet.UsedRange[row, col]; | ||
string cellValue = cell.DisplayText; | ||
|
||
//Checks the cellvalue starts with { | ||
if (cellValue.StartsWith("\"{\n") || cellValue.StartsWith(" \"{")) | ||
{ | ||
startConcatenation = true; | ||
startCellAddress = cell.AddressLocal; | ||
concatenatedValues.Add(cellValue); | ||
continue; //Skip to the next cell | ||
} | ||
|
||
//Concatenate the JSON value to the list | ||
if (startConcatenation && !string.IsNullOrEmpty(cellValue)) | ||
{ | ||
|
||
concatenatedValues.Add(cellValue); | ||
cell.Clear(); | ||
} | ||
|
||
//Update the JSON value in the respective cell | ||
if (cellValue.Contains("}\"")) | ||
{ | ||
startConcatenation = false; | ||
//Concatenate the values | ||
string concatenatedValue = string.Join(" ", concatenatedValues); | ||
|
||
//Update the corresponding cell with the concatenated value | ||
worksheet.Range[startCellAddress].Value = concatenatedValue; | ||
|
||
//Clear the list for the next iteration | ||
concatenatedValues.Clear(); | ||
|
||
//Reset the start cell address for the next iteration | ||
startCellAddress = ""; | ||
} | ||
} | ||
} | ||
|
||
//Check for blank row | ||
List<int> rowsToDelete = new List<int>(); | ||
for (int row = 1; row <= worksheet.UsedRange.LastRow; row++) | ||
{ | ||
bool isRowEmpty = true; | ||
for (int col = 1; col <= worksheet.UsedRange.LastColumn; col++) | ||
{ | ||
IRange cell = worksheet.Range[row, col]; | ||
if (!string.IsNullOrEmpty(cell.Value)) | ||
{ | ||
isRowEmpty = false; | ||
break; | ||
} | ||
} | ||
if (isRowEmpty) | ||
{ | ||
rowsToDelete.Add(row); | ||
} | ||
} | ||
|
||
//Delete the blank rows | ||
for (int i = 0; i < rowsToDelete.Count; i++) | ||
{ | ||
worksheet.DeleteRow(rowsToDelete[i] - i); | ||
} | ||
|
||
worksheet.UsedRange.WrapText = false; | ||
worksheet.UsedRange.AutofitColumns(); | ||
FileStream outputStream = new FileStream("output.xlsx", FileMode.Create, FileAccess.Write); | ||
workbook.SaveAs(outputStream); | ||
} | ||
} | ||
} | ||
} |