Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not use ExcelPackage.Workbook.Names if there are any invaild names. #560

Open
DamianRice opened this issue Dec 19, 2021 · 3 comments
Open
Assignees

Comments

@DamianRice
Copy link

DamianRice commented Dec 19, 2021

I was trying to use C# loading Excel Name Manager, however, the name contains invalid chars like "\0". How to deal with that? I can't even 'var name = pkg.Workbook.Names;'.

Error Like this: System.ArgumentException:“Name \0 contains invalid characters”

using (ExcelPackage pkg = new ExcelPackage(filePatha))
        {
            var name = pkg.Workbook.Names;
            foreach (ExcelNamedRange excelNamedRange in name)
            {
                Console.WriteLine($"IsNameHidden:{excelNamedRange.IsNameHidden}\n" +
                                  $"Name:{excelNamedRange.Name}\n" +
                                  $"Address:{excelNamedRange.Address}\n" +
                                  $"Formula:{excelNamedRange.Formula}\n" +
                                  $"Text:{excelNamedRange.Text}\n" +
                                  $"Value:{excelNamedRange.Value}\n" +
                                  $"FullAddressAbsolute:{excelNamedRange.FullAddressAbsolute}\n" +
                                  $"---------------------------------\n");
            }
            Console.Read();
        }

The class ‘ExcelNamedRangeCollection’ shows

    /// <param name="Name">The name</param>
    /// <param name="Range">The range</param>
    /// <returns></returns>
    public ExcelNamedRange Add(string Name, ExcelRangeBase Range)
    {
      if (!ExcelAddressUtil.IsValidName(Name))
        throw new ArgumentException("Name contains invalid characters or is not valid.");
      if (this._wb != Range._workbook)
        throw new InvalidOperationException("The range must be in the same package. ");
      return this.AddName(Name, Range);
    }

Maybe an update with StringBuilder to solve strings like \0 could be implemented?

@JanKallman
Copy link
Contributor

I'll have a look at it.

@JanKallman
Copy link
Contributor

What is the actual problem here?
The name can't contain invalid characters when you add it. You should however be able to load it from an existing workbook, but it will give you an error in Excel later when you open it.

@DamianRice
Copy link
Author

What I mean is "Excel Defined Names MAY contains invalid chars".
If using Aspose or other packages to auto-generate Defined Names, it may cause that names contained invalid characters.
Such as "\0" "_123"。
I've tried to use Excel Name Manager to create names contained invalid char and it will be banned by Excel.
However, I solved this problem with OpenXML

@OssianEPPlus OssianEPPlus added the alt-enhancement alternative enchancement label for organization purposes label Feb 8, 2024
@JanKallman JanKallman added needs investigation and removed alt-enhancement alternative enchancement label for organization purposes labels Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants