Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cells merged and set its value, but it not work?

Tags:

c#

epplus

//use EPPlus.dll

using OfficeOpenXml

string path = @"C:\Users\Superman\Desktop\recc\1996.xlsx";
            ExcelPackage package = new ExcelPackage(new FileInfo(path));
            var sheet3 = package.Workbook.Worksheets[3];
            sheet3.Cells["A1:B5"].Merge = true;
            var mergedId = sheet3.MergedCells[1, 1];
            sheet3.Cells[mergedId].First().Value = "123"; // error: System.InvalidOperationException : Sequence does not contain any elements
            package.Save();

What's wrong? How to do it?

like image 645
Superman Avatar asked Feb 23 '17 06:02

Superman


People also ask

Why is my merge cells not working?

If Merge & Center is disabled, ensure that you're not editing a cell—and the cells you want to merge aren't formatted as an Excel table. Cells formatted as a table typically display alternating shaded rows, and perhaps filter arrows on the column headings.


2 Answers

To answer why the exception from using the First() method - I would bet money that your sheet3 in excel is empty. Remember that the Cells object only contains references to cell that have actual content. But if all of the cells in excel are empty then so is the Cells collection in EPPlus.

For example, this works fine when creating a brand new sheet:

using (var package = new ExcelPackage(fi))
{
    var brandNewSheet = package.Workbook.Worksheets.Add("BrandNewSheet");
    brandNewSheet.Cells["A1"].LoadFromCollection(new[] {"A", "B", "C", "D", "E"});
    brandNewSheet.Cells["B1"].LoadFromCollection(new[] {"A", "B", "C", "D", "E"});
    brandNewSheet.Cells["A1:B5"].Merge = true;
    var mergedId = brandNewSheet.MergedCells[1, 1];
    brandNewSheet.Cells[mergedId].First().Value = "123";
    package.Save();
}

But if you comment out the LoadFromCollection calls you will get the runtime exception:

using (var package = new ExcelPackage(fi))
{
    var brandNewSheet = package.Workbook.Worksheets.Add("BrandNewSheet");
    //brandNewSheet.Cells["A1"].LoadFromCollection(new[] {"A", "B", "C", "D", "E"});
    //brandNewSheet.Cells["B1"].LoadFromCollection(new[] {"A", "B", "C", "D", "E"});
    brandNewSheet.Cells["A1:B5"].Merge = true;
    var mergedId = brandNewSheet.MergedCells[1, 1];
    brandNewSheet.Cells[mergedId].First().Value = "123"; //Cells is empty so: System.InvalidOperationException: Sequence contains no elements
    package.Save();
}

As others have explain, there is no need to call First() to get what you want but figured I was at least address that.

like image 165
Ernie S Avatar answered Nov 03 '22 00:11

Ernie S


just a suggestion: use "using"

and a small change in your code:

    using (ExcelPackage package = new ExcelPackage(new FileInfo(path)))
    {
        var sheet3 = package.Workbook.Worksheets[3];
        sheet3.Cells["A1:B5"].Merge = true;
        var mergedId = sheet3.MergedCells[1, 1];
        sheet3.Cells["A1"].Value = "123"; 
        package.Save();
    }

I think the range wasn't recognized anymore, because you have merged it

EDIT1:

this code works for me like a charm. I think you don't need .First()

using (ExcelPackage package = new ExcelPackage(new FileInfo(path)))
            {
                var sheet3 = package.Workbook.Worksheets[3];
                sheet3.Cells["A1:B5"].Merge = true;
                var mergedId = sheet3.MergedCells[1, 1];
                sheet3.Cells[mergedId].Value = "123";
                package.Save();
            }
like image 34
tretom Avatar answered Nov 02 '22 23:11

tretom