package com.smtscript.lib.jsexcel; import org.mozilla.javascript.NativeArray; import org.mozilla.javascript.Wrapper; import jxl.Sheet; import jxl.Workbook; import com.smtscript.run.ScriptRunScope; public class JSExcelReader { protected ScriptRunScope _parentScope; protected Workbook _workbook; public JSExcelReader(ScriptRunScope parentScope, String fileName) throws Exception { _parentScope = parentScope; _workbook = Workbook.getWorkbook(_parentScope.__findExistFile__(fileName, true).open()); } public ScriptRunScope __parentScope__() { return _parentScope; } public void close() { if(_workbook == null) return; _workbook.close(); _workbook = null; } public JSExcelReaderSheet readSheet(Object id) throws Exception { if(id instanceof Wrapper) id = ((Wrapper)id).unwrap(); if(id instanceof Double) id = (int)(double)((Double)id); Sheet sheet; if(id instanceof String) sheet = _workbook.getSheet((String)id); else if(id instanceof Integer) sheet = _workbook.getSheet((Integer)id); else throw new Exception("unsupport id type : " + id.getClass().getName()); if(sheet == null) throw new Exception("can't find sheet :" + id.toString()); return newSheet(sheet); } public NativeArray getSheetNames() { return new NativeArray(_workbook.getSheetNames()); } public NativeArray getSheets() { Sheet[] sheets = _workbook.getSheets(); JSExcelReaderSheet[] rdSheets = new JSExcelReaderSheet[sheets.length]; for(int i = 0; i < sheets.length; i ++) { rdSheets[i] = newSheet(sheets[i]); } return new NativeArray(rdSheets); } protected JSExcelReaderSheet newSheet(Sheet sheet) { return new JSExcelReaderSheet(sheet, this); } }