diff --git a/Real Time Examples in Excel/Merge Excel Documents/.NET/MergeExcel/MergeExcel/Program.cs b/Real Time Examples in Excel/Merge Excel Documents/.NET/MergeExcel/MergeExcel/Program.cs index 303f9af6..c69188bb 100644 --- a/Real Time Examples in Excel/Merge Excel Documents/.NET/MergeExcel/MergeExcel/Program.cs +++ b/Real Time Examples in Excel/Merge Excel Documents/.NET/MergeExcel/MergeExcel/Program.cs @@ -1,6 +1,4 @@ -// See https://aka.ms/new-console-template for more information -using Syncfusion.XlsIO; - +using Syncfusion.XlsIO; namespace MergeExcel { @@ -8,63 +6,28 @@ 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 streams = new List(); - - 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(); - } - - /// - /// Merge Excel documents from the list of Excel streams - /// - /// List of Excel document stream to be merged - /// - public static Stream MergeExcelDocuments(List 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"))); } } } -} - - - - - - - +} \ No newline at end of file diff --git a/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs b/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs index f414ed22..0d3cf9ba 100644 --- a/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs +++ b/Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs @@ -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"; @@ -41,10 +41,4 @@ private static void SplitExcelDocument(string filePath) } } } -} - - - - - - +} \ No newline at end of file