Step 1: Get the sheet
reference.
Step 2: Get each cell in
that row and add it to list and return.
for(int
column=0; column< sheet.getColumns(); column++){
list.add(sheet.getCell(column, row));
public class NoSheetWithGivenNameException extends RuntimeException{ NoSheetWithGivenNameException(String name){ super("No xls sheet with given name " + name); } }
public class SheetColumnExceedException extends RuntimeException{ SheetColumnExceedException(int actualColumns, int askingForColumn){ super("Total columns are : " + actualColumns + " application looking for column " + askingForColumn); } }
public class SheetIndexOutOfBoundsException extends RuntimeException{ SheetIndexOutOfBoundsException(int index, int actual){ super("looking for sheet : " + (index+1) + " but actual sheets are : " + actual); } }
public class SheetRowExceedException extends RuntimeException{ SheetRowExceedException(int actualRows, int askingForRow){ super("Total rows are : " + actualRows + " application looking for row " + askingForRow); } }
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; import java.util.List; import java.util.ArrayList; public class XLSUtils { private Workbook workbook = null; public XLSUtils(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 Sheet getSheet(int index){ checkWorkbookInitialized(); if(index >= workbook.getNumberOfSheets()) throw new SheetIndexOutOfBoundsException(index, workbook.getNumberOfSheets()); return workbook.getSheet(index); } public Sheet getSheet(String name){ checkWorkbookInitialized(); Sheet sheet = workbook.getSheet(name); if(sheet == null) throw new NoSheetWithGivenNameException(name); return sheet; } public String[] getSheetNames(){ checkWorkbookInitialized(); return workbook.getSheetNames(); } public int getNumberOfRows(Sheet sheet){ checkWorkbookInitialized(); return sheet.getRows(); } public int getNumberOfColumns(Sheet sheet){ checkWorkbookInitialized(); return sheet.getColumns(); } public int getNumberOfRows(int sheetIndex){ Sheet sheet = getSheet(sheetIndex); return getNumberOfRows(sheet); } public int getNumberOfRows(String sheetName){ Sheet sheet = getSheet(sheetName); return getNumberOfRows(sheet); } public int getNumberOfColumns(int sheetIndex){ Sheet sheet = getSheet(sheetIndex); return getNumberOfColumns(sheet); } public int getNumberOfColumns(String sheetName){ Sheet sheet = getSheet(sheetName); return getNumberOfColumns(sheet); } public Cell getCell(int sheetIndex, int column, int row){ Sheet sheet = getSheet(sheetIndex); return getCell(sheet, column, row); } public Cell getCell(String sheetName, int column, int row){ Sheet sheet = getSheet(sheetName); return getCell(sheet, column, row); } public Cell getCell(Sheet sheet, int column, int row){ int actualColumns = sheet.getColumns()-1; int actualRows = sheet.getRows() - 1; if(column > actualColumns) throw new SheetColumnExceedException(actualColumns, column); if(row > actualRows) throw new SheetColumnExceedException(actualRows, row); return sheet.getCell(column, row); } public Cell getCell(int sheetIndex, String loc){ Sheet sheet = getSheet(sheetIndex); return getCell(sheet, loc); } public Cell getCell(String sheetName, String loc){ Sheet sheet = getSheet(sheetName); return getCell(sheet, loc); } public Cell getCell(Sheet sheet, String loc){ return sheet.getCell(loc); } List<Cell> getEntireRow(int sheetIndex, int row){ Sheet sheet = getSheet(sheetIndex); return getEntireRow(sheet, row); } List<Cell> getEntireRow(String sheetName, int row){ Sheet sheet = getSheet(sheetName); return getEntireRow(sheet, row); } List<Cell> getEntireRow(Sheet sheet, int row){ int totalRows = sheet.getRows()-1; if(row > totalRows) throw new SheetRowExceedException(totalRows, row); List<Cell> list = new ArrayList<Cell> (); for(int column=0; column< sheet.getColumns(); column++){ list.add(sheet.getCell(column, row)); } return list; } }
import jxl.*; import java.util.List; public class TestUtils { public static void main(String[] args) { XLSUtils util = new XLSUtils("D:\\data.xls"); List<Cell> list = util.getEntireRow("employee", 1); for(Cell cell : list){ System.out.print(cell.getContents() + " "); } util.closeWorkbook(); } }
Output
2 Rama Krishna Gurram
No comments:
Post a Comment