Step 1: Initialize
workbook.
Workbook
workbook = Workbook.getWorkbook(new
File(path));
Step 2: Call getNumberOfSheets()
method to get number of sheets in given xsl file.
public class WorkBookNotInitializedException extends RuntimeException{ WorkBookNotInitializedException(){ super("work book is not initialized"); } }
import java.io.File; import java.io.IOException; import jxl.*; import jxl.read.biff.BiffException; public class XSLUtils { private Workbook workbook = null; public XSLUtils(String path){ try { workbook = Workbook.getWorkbook(new File(path)); } catch (IOException ex) { ex.printStackTrace(); } catch (BiffException ex) { ex.printStackTrace(); } } private void checkWorkbookInitialized(){ if(workbook == null) throw new WorkBookNotInitializedException(); } public int getNoOfSheets(){ checkWorkbookInitialized(); return workbook.getNumberOfSheets(); } public void closeWorkbook(){ checkWorkbookInitialized(); workbook.close(); } }
public class TestUtils { public static void main(String[] args) { XSLUtils util = new XSLUtils("D:\\data.xls"); System.out.println("Number of sheets in xls file are " + util.getNoOfSheets()); util.closeWorkbook(); } }
Output
Number of sheets in xls file are 3
Prevoius Next Home
No comments:
Post a Comment