Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,70 +1,33 @@
// See https://aka.ms/new-console-template for more information
using Syncfusion.XlsIO;

using Syncfusion.XlsIO;

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

string outputPath = @"../../../Output/";
string inputPath = Path.GetFullPath("Data");
string outputPath = Path.GetFullPath("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(Path.GetFullPath(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)
IWorkbook mergedBook = application.Workbooks.Create(0);

foreach (FileInfo file in files)
{
stream.Position = 0;
IWorkbook tempWorkbook = application.Workbooks.Open(stream);
workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
IWorkbook tempWorkbook = application.Workbooks.Open(Path.GetFullPath(file.FullName));
mergedBook.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;
mergedBook.Version = ExcelVersion.Xlsx;
mergedBook.SaveAs(Path.GetFullPath(Path.Combine(outputPath, "MergedExcel.xlsx")));
}
}
}
}







}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace SplitExcel
{
class Program
{
private static string inputPath = @"../../../Data/";
private static string inputPath = Path.GetFullPath("Data/");

private static string outputPath = @"../../../Output/";
private static string outputPath = Path.GetFullPath("Output/");
static void Main(string[] args)
{
string fileName = "Report.xlsx";
Expand Down Expand Up @@ -41,10 +41,4 @@ private static void SplitExcelDocument(string filePath)
}
}
}
}






}
Loading