All stories
Java

Excel Reader (JAVA)

H
hemant-kumar

August 21, 2012


package reader;

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

public class ExcelReader
{
            public HashMap<String,String[][]> readExcelAllSheets(File file) throws BiffException,IOException
            {
                        Workbook workbook = Workbook.getWorkbook(file);
                        Sheet sheets[] = workbook.getSheets();
                        HashMap<String,String[][]> hashMap = new HashMap<String, String[][]>();
                        int count=0,rows=0,cols=0;
                        while(count<sheets.length)
                        {
                                    rows = sheets[count].getRows();
                                    cols = sheets[count].getColumns();
                                    if(rows>0 && cols>0)
                                    {
                                                String[][] str = new String[rows][cols];
                                                for(int i=0; i<rows; i++)
                                                            for(int j=0; j<cols; j++)
                                                                        str[i][j] = (sheets[count].getCell(j,i).getContents()).trim();
                                                hashMap.put(sheets[count].getName(),str);
                                    }
                                    count++;
                        }
                        return hashMap;
            }

            public HashMap<String,String[][]> readExcelAllSheets(String filePath) throws BiffException,IOException
            {
                        File file = new File(filePath);
                        return readExcelAllSheets(file);
            }

            public String[][] readExcelSheet(File file,int sheetNo) throws BiffException,IOException
            {
                        Workbook workbook = Workbook.getWorkbook(file);
                        Sheet sheet = workbook.getSheet(sheetNo);
                        int rows = sheet.getRows();
                        int cols = sheet.getColumns();
                        String[][] str = new String[rows][cols];
                        for(int i=0; i<rows; i++)
                                    for(int j=0; j<cols; j++)
                                                str[i][j] = (sheet.getCell(j,i).getContents()).trim();
                        return str;
            }

            public String[][] readExcelSheet(String filePath,int sheetNo) throws BiffException,IOException
            {
                        File file = new File(filePath);
                        return readExcelSheet(file, sheetNo);
            }

            public String[][] readExcelFirstSheet(String filePath) throws BiffException,IOException
            {
                        return readExcelSheet(filePath,0);
            }

            public String[][] readExcelFirstSheet(File file) throws BiffException,IOException
            {
                        return readExcelSheet(file,0);
            }

            public String[][] readExcelLastSheet(File file) throws BiffException,IOException
            {
                        Workbook workbook = Workbook.getWorkbook(file);
                        Sheet sheet = workbook.getSheet(workbook.getNumberOfSheets()-1);
                        int rows = sheet.getRows();
                        int cols = sheet.getColumns();
                        String[][] str = new String[rows][cols];
                        for(int i=0; i<rows; i++)
                                    for(int j=0; j<cols; j++)
                                                str[i][j] = (sheet.getCell(j,i).getContents()).trim();
                        return str;
            }

            public String[][] readExcelLastSheet(String filePath) throws BiffException,IOException
            {
                        File file = new File(filePath);
                        return readExcelLastSheet(file);
            }

            public static void main(String args[])
            {
                        ExcelReader obj = new ExcelReader();
                        try
                        {
                                    String s[][] = obj.readExcelFirstSheet("e:\\FileToRead.xls");
                                    for(int i=0;i<s.length;i++)
                                    {
                                                for(int j=0;j<s[0].length;j++)
                                                            System.out.print(s[i][j] + " ");
                                                System.out.println();
                                    }
                        }
                        catch(Exception e)
                        {
                                    System.out.println("Invalid File");
                        }
            }
}
Java

0

If you found this helpful, give it some claps!

SHARE THIS ARTICLE

Share on X
LinkedIn

Responses0

Sign in to join the conversation

Sign in