How to propagate named ranges from a template

Jul 12, 2010 at 1:42 PM

Hi,

Is it possible to propagate the named ranges from a template to the saved workbook?

I have a simple .xlsx file, which is empty apart from a couple of numbers and a named range. When I call:

 

using( ExcelPackage p = new ExcelPackage("myTemplate.xlsx", true)){

        ExcelWorksheet ws = p.Workbook.Worksheets[1];

        ws.Cells[1,1].value = "hello";

        Byte [] bin = p.GetAsByteArray();

        File.WriteAllBytes("myOutput.xlsx", bin);

 }

 

I notice that all is well, apart from the template's named ranges are missing.

Do you know how I would preserve the named ranges from the template in the saved workbook? I also tried adding a name in code and that didn't get saved either.

 

Thanks,

Phil

 

 

 

Coordinator
Jul 19, 2010 at 7:18 AM
Hi, Sorry for my delayed answer, i'm on vaccation for a few weeks now. Named ranges are supposed to work. You can use the Workbook.Names for global names and Worksheet.Names for worksheet local names. I know there was a bug in version 2.6, so make sure you use version 2.7. If you still dont get it to work, create an issue and upload your template, so I can have a look at it. Jan
Aug 5, 2010 at 5:30 PM
Edited Aug 5, 2010 at 5:34 PM

Hi Jan,

 

I'm using myself a template with a workbook named range but i have the same issue.

Despite all my effort i was unable to keep this named range in the new file created.

EPPlus version is 2.7.0.1 and here is the code i'm using

 

using OfficeOpenXml;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo existingFile = new FileInfo(@".\Test.xlsx");
            FileInfo template = new FileInfo(@".\Template.xlsx");
            using (ExcelPackage p = new ExcelPackage(existingFile, template))
            {
                // Planning to do some stuff here
                 p.Save();
            }
        }
    }
}

 

if i'm trying to create my named range before the p.save() it occurs an exception because the named range already exist

Best regards

Sonia

Coordinator
Aug 9, 2010 at 6:36 AM

Hi,

There is a bug when loading named ranged if you have a named ranged set to a #REF address. Check out names inside the template and set them to a valid address or remove them.

I'll try to check in a fix for this problem later on.

If this don't solve you problem, please upload your template and I will have a look at it. (There is an issue on this problem in the issuetracker, so upload your file there).

Jan