package cn.edu.seu.ise.dca.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ExcelReader {
	private Workbook workbook;
	private Sheet currentSheet;
	
	public ExcelReader(String filePath) {
		File file = new File(filePath);
		try {
			workbook = Workbook.getWorkbook(file);
			currentSheet = workbook.getSheet(0);
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public void setCurrentSheet(int position) {
		currentSheet = workbook.getSheet(position);
	}
	
	public String read(int column, int row) {
		return currentSheet.getCell(column, row).getContents();
	}
	
	public int getRows() {
		return currentSheet.getRows();
	}
	
	public List<String> readRows(int row) {
		List<String> rows = new ArrayList<String>();
		for(int i = 0; i < currentSheet.getColumns(); i++) {
			rows.add(currentSheet.getCell(i, row).getContents());
		}
		return rows;
	}
	
	public void close() {
		workbook.close();
	}

}
