Monday, November 19, 2012

Program to get contents of an excel sheet

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcelSheet {
   private String inputFile;
   
  public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
   }

  public void read() throws IOException  {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
            int rowCount = 0;
            int columnCount = 0;
            Cell rowData[] = null;
            // Get workbook instance
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet s = w.getSheet(0);
            rowCount = s.getRows();          
            columnCount = s.getColumns();
         
            // Reading Individual Row Content
            for (int i = 0; i < rowCount; i++) {
                // Get Individual Row
                rowData = s.getRow(i);
                if (rowData.length > 1) {
                    System.out.println("\n");
                    for (int j = 0; j < columnCount; j++) {
                        if (rowData[j] != null)
                            System.out.print(rowData[j].getContents() +"----");
                    }
                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        ReadExcelSheet test = new ReadExcelSheet();
        test.setInputFile("D:\\testcodeline\\test\\Published_List 186.xls");
        test.read();
    }
}