2
votes

I am using EPPlus to create Excel files. I need to get the name of the file containing a worksheet from an ExcelWorksheet or ExcelWorkbook object. I can't find any "name" property in the ExcelWorkbook object, or any way of getting from an ExcelWorkbook or ExcelWorksheet back to the containing ExcelPackage.

Is there a property or path of properties I can use to get from ExcelWorksheet to a file name?


To clarify my intent:

I am creating an API that will create table difference reports. It mainly works with ADO.NET DataTables, but it also has adapters that take interop Worksheets or EPPlus ExcelWorksheets, which it will convert to DataTables for processing.

I only need the file name containing the ExcelWorksheet so that it can be printed on the output report for clarity. The API is only really working at the scope of DataTables or objects that are roughly equivalent to tables, like Worksheets or ExcelWorksheets. It doesn't deal with DataSets, Workbooks, ExcelWorkbooks, or ExcelPackages (except when it outputs a report file using EPPlus).

So, I would very much like the API functions to require a bare minimum of parameters, like DataTables, Worksheets, ExcelWorksheets, and some bitflag options. It would also be nice to have the different overloads of the functions take analogous parameters (i.e. taking two DataTables or two Worksheets, or two ExcelWorksheets), and not require extra clutter parameters for the EPPlus input case.

With interop, it is extremely easy to get a file name from a Worksheet (mySheet.Parent.Name), and EPPlus provides easy ways to move down the object hierarchy (myPackage.Workbook.Worksheets[1]), so I assumed there would be some way to move back up the hierarchy from an ExcelWorksheet object.

1
Erm, that's backwards isn't it? You get a worksheet from a file. So you already know it. - Hans Passant
Well if you want to you could always get the source code and add it yourself. BUT you might need to do it then everytime a new release is released, just go to epplus.codeplex.com and go under "Source Code" - Donald Jansen
Thanks Donald, I don't know why I overlooked that. That's what open source is for isn't it. :D - JamesFaix
I believe it is just a matter of adding "public ExcelPackage Package {get {return _package;}} to Workbook.cs, but CodePlex is having some issues right now. - JamesFaix

1 Answers

4
votes

As suggested by Donald Jansen in the comments, the solution is to edit the EPPlus source code.

Add the following properties to ExcelWorkbook.cs:

//Get the package containing this workbook.
public ExcelPackage Package { get { return _package; } }
//Get the name of the file of this workbook.
public String Name { get { return _package.File.Name; } }

Now you can scale up and down the object hierarchy to your heart's content.