6

I've been pulling my hair out trying to set the value of a named range (in this case, a single named cell) using the ExcelPackage (3.0.1) library, it should be a simple as this:

ExcelNamedRange er = xlPackage.Workbook.Names["Customer"];
er.Value = "Foo Bar";

I'm obviously doing it wrong - has anyone got an example I can follow

Thanks

2 Answers 2

10

I looked for ExcelPackage documentation to see what type Names[] collection returns and found that documentatios will come soon, or at least that is what they said back in 2007.

I suggest you use EPPlus wich is a excel library (xlsx only) that have worked great to me.

official link

Now, to set a value for each cell in a named range:

ExcelWorksheet sheet = _openXmlPackage.Workbook.Worksheets["SheetName"];
using (ExcelNamedRange namedRange = sheet.Names["RangeName"])
{
    for (int rowIndex = Start.Row; rowIndex <= namedRange.End.Row; rowIndex++)
    {
        for (int columnIndex = namedRange.Start.Column; columnIndex <= namedRange.End.Column; columnIndex++)
        {
            sheet.Cells[rowIndex, columnIndex].Value = "no more hair pulling";                        
        }
    }
}
2
  • My range is a single cell. The above code claims namedRange.Start.Row is -1.
    – BigBadOwl
    Jul 5, 2012 at 10:04
  • 1
    It should work for a single cell, but I'm not 100% sure. I see you managed to solve your problem, glad you did. I'm leaving my answer here in case someone with Named Ranges issues is interested on trying EPPlus instead. Regards.
    – daniloquio
    Jul 6, 2012 at 16:50
2

I had to put in a work around using a cell value instead.

using (ExcelPackage xlPackage = new ExcelPackage(newFile))
            {
                foreach (ExcelWorksheet worksheet in xlPackage.Workbook.Worksheets)
                {

                    var dimension = worksheet.Dimension;
                    if (dimension == null) { continue; }
                    var cells = from row in Enumerable.Range(dimension.Start.Row, dimension.End.Row)
                                from column in Enumerable.Range(dimension.Start.Column, dimension.End.Column)
                                //where worksheet.Cells[row, column].Value.ToString() != String.Empty
                                select worksheet.Cells[row, column];
                    try
                    {
                        foreach (var excelCell in cells)
                        {
                            try
                            {
                                if (excelCell.Value.ToString().Equals("[Customer]")) { excelCell.Value = "Customer Name"; }

                            }
                            catch (Exception) { }
                        }

                    }
                    catch (Exception a) { Console.WriteLine(a.Message); }
                }
1
  • Is this to set the range that you will search though? so that you don't search though empty cells? I need something like this?
    – Pomster
    Oct 11, 2012 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.