Search code examples
apache-poixssf

How can I get the workbook file name i.e. excel file name


I am working on an existing .xlsx file. Can any one share with me that, how can I get my current file name?

I mean, suppose I am working on test.xlsx file. How can I get the name of workbook "test.xlsx" using apache poi.


Solution

  • POI can open a workbook from any InputStream you care to throw at it. (Files are lower memory, but you can use a stream if you want, and many people do). If a stream isn't file backed, then it doesn't have a filename, so no amount of pleading with POI will get you one!

    If you are opening a Workbook from a File, then that File object knows the filename. Ask that for it! Otherwise, if opening from an InputStream, there most likely isn't a filename, so there's nothing to supply.

    Finally, be aware that unlike the name of a Sheet, which is stored in the file, the name of the file itself isn't something magic. Take test.xls, copy it to test2.xls, and also store it in a database blob field. Load all. All of them are the same file, but two of them have different filenames, and one has no filename at all!