Skip to content

Commit a431179

Browse files
authored
Merge pull request #349 from SyncfusionExamples/1046485-ModifyMergeSplitSamples
Task(1046485)-Modify merge and split samples in XlsIO
2 parents 8ff9d5e + 03f90c4 commit a431179

2 files changed

Lines changed: 14 additions & 57 deletions

File tree

  • Real Time Examples in Excel
    • Merge Excel Documents/.NET/MergeExcel/MergeExcel
    • SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument
Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,33 @@
1-
// See https://aka.ms/new-console-template for more information
2-
using Syncfusion.XlsIO;
3-
1+
using Syncfusion.XlsIO;
42

53
namespace MergeExcel
64
{
75
class Program
86
{
97
static void Main(string[] args)
108
{
11-
string inputPath = @"../../../Data/";
12-
13-
string outputPath = @"../../../Output/";
9+
string inputPath = Path.GetFullPath("Data");
10+
string outputPath = Path.GetFullPath("Output");
1411

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

17-
List<Stream> streams = new List<Stream>();
18-
19-
foreach (FileInfo file in files)
20-
{
21-
streams.Add(file.OpenRead());
22-
}
23-
24-
Stream mergedStream = MergeExcelDocuments(streams);
25-
26-
FileStream fileStream = new FileStream(Path.GetFullPath(outputPath + "MergedExcel.xlsx"), FileMode.Create, FileAccess.Write);
27-
mergedStream.Position = 0;
28-
mergedStream.CopyTo(fileStream);
29-
fileStream.Close();
30-
}
31-
32-
/// <summary>
33-
/// Merge Excel documents from the list of Excel streams
34-
/// </summary>
35-
/// <param name="streams">List of Excel document stream to be merged</param>
36-
/// <returns></returns>
37-
public static Stream MergeExcelDocuments(List<Stream> streams)
38-
{
3914
using (ExcelEngine excelEngine = new ExcelEngine())
4015
{
4116
IApplication application = excelEngine.Excel;
4217
application.DefaultVersion = ExcelVersion.Xlsx;
43-
IWorkbook workbook = application.Workbooks.Create(0);
4418

45-
//Loop through each Excel document and add the worksheets to the new workbook
46-
foreach (Stream stream in streams)
19+
IWorkbook mergedBook = application.Workbooks.Create(0);
20+
21+
foreach (FileInfo file in files)
4722
{
48-
stream.Position = 0;
49-
IWorkbook tempWorkbook = application.Workbooks.Open(stream);
50-
workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
23+
IWorkbook tempWorkbook = application.Workbooks.Open(Path.GetFullPath(file.FullName));
24+
mergedBook.Worksheets.AddCopy(tempWorkbook.Worksheets);
5125
tempWorkbook.Close();
5226
}
5327

54-
//Save the workbook to a memory stream
55-
MemoryStream memoryStream = new MemoryStream();
56-
workbook.Version = ExcelVersion.Xlsx;
57-
workbook.SaveAs(memoryStream);
58-
59-
return memoryStream;
28+
mergedBook.Version = ExcelVersion.Xlsx;
29+
mergedBook.SaveAs(Path.GetFullPath(Path.Combine(outputPath, "MergedExcel.xlsx")));
6030
}
6131
}
6232
}
63-
}
64-
65-
66-
67-
68-
69-
70-
33+
}

‎Real Time Examples in Excel/SplitExcelDocument/.NET/SplitExcelDocument/SplitExcelDocument/Program.cs‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace SplitExcel
55
{
66
class Program
77
{
8-
private static string inputPath = @"../../../Data/";
8+
private static string inputPath = Path.GetFullPath("Data/");
99

10-
private static string outputPath = @"../../../Output/";
10+
private static string outputPath = Path.GetFullPath("Output/");
1111
static void Main(string[] args)
1212
{
1313
string fileName = "Report.xlsx";
@@ -41,10 +41,4 @@ private static void SplitExcelDocument(string filePath)
4141
}
4242
}
4343
}
44-
}
45-
46-
47-
48-
49-
50-
44+
}

0 commit comments

Comments
 (0)