7

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.

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74

2 Answers2

10

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!

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
-1

I solved this by storing the file name in class field variable