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

BigBadOwl
  • 669
  • 2
  • 9
  • 22

2 Answers2

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";                        
        }
    }
}
fuglede
  • 17,388
  • 2
  • 54
  • 99
daniloquio
  • 3,822
  • 2
  • 36
  • 56
  • My range is a single cell. The above code claims namedRange.Start.Row is -1. – BigBadOwl Jul 05 '12 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 06 '12 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); }
                }
BigBadOwl
  • 669
  • 2
  • 9
  • 22
  • 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 '12 at 10:55