Skip to content

Commit

Permalink
Merge pull request #98 from SyncfusionExamples/882017-CSV-with-JSON-t…
Browse files Browse the repository at this point in the history
…o-Excel

882017 - Add the samples for CSV with JSON to Excel
  • Loading branch information
Mohan2401 authored May 14, 2024
2 parents 1c47eac + 29a286c commit fca26c5
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
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
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>
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"]
}"
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);
}
}
}
}

0 comments on commit fca26c5

Please sign in to comment.