Skip to content

Commit

Permalink
Merge-Excel-Documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohan2401 committed Nov 21, 2023
1 parent 3a60d9f commit 3e69023
Show file tree
Hide file tree
Showing 7 changed files with 109 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}") = "MergeExcel", "MergeExcel\MergeExcel.csproj", "{B68720C9-71D9-4706-B1E2-7C207699EF1D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8553F0E-B638-4BC4-9E5B-2FC2C818FB4A}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="23.2.4" />
</ItemGroup>

<ItemGroup>
<Folder Include="Data\" />
<Folder Include="Output\" />
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// See https://aka.ms/new-console-template for more information
using Syncfusion.XlsIO;


namespace MergeExcel
{
class Program
{
static void Main(string[] args)
{
string inputPath = @"../../../Data/";

string outputPath = @"../../../Output/";

FileInfo[] files = new DirectoryInfo(inputPath).GetFiles();

List<Stream> streams = new List<Stream>();

foreach (FileInfo file in files)
{
streams.Add(file.OpenRead());
}

Stream mergedStream = MergeExcelDocuments(streams);

FileStream fileStream = new FileStream(outputPath + "MergedExcel.xlsx", FileMode.Create, FileAccess.Write);
mergedStream.Position = 0;
mergedStream.CopyTo(fileStream);
fileStream.Close();
}

/// <summary>
/// Merge Excel documents from the list of Excel streams
/// </summary>
/// <param name="streams">List of Excel document stream to be merged</param>
/// <returns></returns>
public static Stream MergeExcelDocuments(List<Stream> streams)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(0);

//Loop through each Excel document and add the worksheets to the new workbook
foreach (Stream stream in streams)
{
stream.Position = 0;
IWorkbook tempWorkbook = application.Workbooks.Open(stream);
workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
tempWorkbook.Close();
}

//Save the workbook to a memory stream
MemoryStream memoryStream = new MemoryStream();
workbook.Version = ExcelVersion.Xlsx;
workbook.SaveAs(memoryStream);

return memoryStream;
}
}
}
}


0 comments on commit 3e69023

Please sign in to comment.