I want to merge multiple Excel files with EPPlus in C#.
I did the following:
using (MemoryStream protocolStream = new MemoryStream())
{
ExcelPackage pck = new ExcelPackage();
HashSet<string> wsNames = new HashSet<string>();
foreach (var file in files)
{
ExcelPackage copyPck = new ExcelPackage(new FileInfo(file));
foreach (var ws in copyPck.Workbook.Worksheets)
{
string name = ws.Name;
int i = 1;
while (!wsNames.Add(ws.Name))
name = ws.Name + i++;
ws.Name = name;
var copiedws = pck.Workbook.Worksheets.Add(name);
copiedws.WorksheetXml.LoadXml(ws.WorksheetXml.DocumentElement.OuterXml);
}
}
pck.SaveAs(protocolStream);
protocolStream.Position = 0;
using (FileStream fs = new FileStream(resultFile, FileMode.Create))
protocolStream.CopyTo(fs);
}
But I get the following error in pck.SaveAs(protocolStream)
:
System.ArgumentOutOfRangeException
in System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) in System.Collections.Generic.List
1.get_Item(Int32 index) in OfficeOpenXml.ExcelStyleCollection
1.get_Item(Int32 PositionID)
I also tried it with the Worksheet.Copy method, but I lose the styling with it.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Here is an example of merging several files into one by coping all worksheets from source excel files.
var files = new string[] { @"P:\second.xlsx", @"P:\second.xlsx" };
var resultFile = @"P:\result.xlsx";
ExcelPackage masterPackage = new ExcelPackage(new FileInfo(@"P:\first.xlsx"));
foreach (var file in files)
{
ExcelPackage pckg = new ExcelPackage(new FileInfo(file));
foreach (var sheet in pckg.Workbook.Worksheets)
{
//check name of worksheet, in case that worksheet with same name already exist exception will be thrown by EPPlus
string workSheetName = sheet.Name;
foreach (var masterSheet in masterPackage.Workbook.Worksheets)
{
if (sheet.Name == masterSheet.Name)
{
workSheetName = string.Format("{0}_{1}", workSheetName, DateTime.Now.ToString("yyyyMMddhhssmmm"));
}
}
//add new sheet
masterPackage.Workbook.Worksheets.Add(workSheetName, sheet);
}
}
masterPackage.SaveAs(new FileInfo(resultFile));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With