-
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.
- Loading branch information
Showing
7 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Real Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel.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}") = "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 added
BIN
+16.4 KB
Real Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel/Data/First.xlsx
Binary file not shown.
Binary file added
BIN
+10.3 KB
Real Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel/Data/Second.xlsx
Binary file not shown.
Binary file added
BIN
+12.4 KB
Real Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel/Data/Third.xlsx
Binary file not shown.
19 changes: 19 additions & 0 deletions
19
Real Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel/MergeExcel.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,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 added
BIN
+53.2 KB
...Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel/Output/MergedExcel.xlsx
Binary file not shown.
65 changes: 65 additions & 0 deletions
65
Real Time Examples in Excel/Merge Excel Documents/.NET Core/MergeExcel/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,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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|