.NET C# - Importing Excel File - Sheet Name!

3.8k Views Asked by At

I'm using the NPOI Library to import some data from an excel sheet to my database.

One early issue I'm curious about, is typically, to begin, you would have something like this:

snippet

 using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);
                    HSSFSheet sheet = templateWorkbook.GetSheet("Sales");

My query is, what happens when the name of the worksheet is different each time, e.g. I plan to import sales data, and the sheets within the workbooks I receive from suppliers are titled by date.

Is there any way that you can specify GetSheet(); to just get the first sheet in the workbook?

Any pointers would be much appreciated,

thanks guys :)

1

There are 1 best solutions below

1
On BEST ANSWER

Without trying it out I'd assume that you're looking for getSheetAt.

So it would be:

HSSFSheet sheet = templateWorkbook.GetSheetAt(0);