Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# EPPlus merge Excel Files

Tags:

c#

excel

epplus

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.List1.get_Item(Int32 index) in OfficeOpenXml.ExcelStyleCollection1.get_Item(Int32 PositionID)

I also tried it with the Worksheet.Copy method, but I lose the styling with it.

like image 904
AlteGurke Avatar asked Apr 28 '16 05:04

AlteGurke


People also ask

What C is used for?

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 in C language?

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.

What is the full name of C?

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.

Is C language easy?

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.


1 Answers

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));
like image 156
Vladimir Nesic Avatar answered Sep 30 '22 04:09

Vladimir Nesic