Here is a screenshot of the test file I used for this example:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
// The following example code demonstrates how to get the Sheet
// names in an Excel spreadsheet file
public class GetExcelSheetNames {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("/Temp/Test1.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            // for each sheet in the workbook
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                System.out.println("Sheet name: " + workbook.getSheetName(i));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Here is the output of the program:
Sheet name: Sheet1 Sheet name: Sheet2 Sheet name: Sheet3Piece of cake!!!

No comments:
Post a Comment