Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read all cell value using Apache POI?

I want to read all cell value from excel sheet using Apache POI and store it into one dimensional string array .Is is possible to read data from excel row by row and extract value from each cell?

Please help me to solve this problem.

Thanks

like image 868
Sameek Mishra Avatar asked Jun 30 '11 05:06

Sameek Mishra


People also ask

How do you read a cell value in Excel?

We can get the value of a cell (its content) by using the INDEX Function. The INDEX Function looks up a cell contained within a specified range and returns its value. In the example above, the range specified in the INDEX formula is “A1:J10”, the row is cell “C3” (“3”), and the column is cell “C4” (“5”).

Which method is used in the Apache POI library to fetch the cell values from row identify the correct format of invoking the library functions to get the value from the cell?

Apache POI provides a FormulaEvaluator class, which enables us to calculate the results of formulas in Excel sheets. So, we can use FormulaEvaluator to calculate the cell value at runtime directly.


2 Answers

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadExcelFile {

public static void main( String [] args ) { String fileName="C:\\temp\\testPOI.xls"; //Read an Excel File and Store in a Vector Vector dataHolder=readExcelFile(fileName); //Print the data read printCellDataToConsole(dataHolder); } public static Vector readExcelFile(String fileName) { /** --Define a Vector --Holds Vectors Of Cells */ Vector cellVectorHolder = new Vector(); try{ /** Creating Input Stream**/ //InputStream myInput= ReadExcelFile.class.getResourceAsStream( fileName ); FileInputStream myInput = new FileInputStream(fileName); /** Create a POIFSFileSystem object**/ POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); /** Create a workbook using the File System**/ HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); /** Get the first sheet from workbook**/ HSSFSheet mySheet = myWorkBook.getSheetAt(0); /** We now need something to iterate through the cells.**/ Iterator rowIter = mySheet.rowIterator(); while(rowIter.hasNext()){ HSSFRow myRow = (HSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); Vector cellStoreVector=new Vector(); while(cellIter.hasNext()){ HSSFCell myCell = (HSSFCell) cellIter.next(); cellStoreVector.addElement(myCell); } cellVectorHolder.addElement(cellStoreVector); } }catch (Exception e){e.printStackTrace(); } return cellVectorHolder; } private static void printCellDataToConsole(Vector dataHolder) { for (int i=0;i<dataHolder.size(); i++){ Vector cellStoreVector=(Vector)dataHolder.elementAt(i); for (int j=0; j < cellStoreVector.size();j++){ HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j); String stringCellValue = myCell.toString(); System.out.print(stringCellValue+"\t"); } System.out.println(); } }

}

like image 108
Rupok Avatar answered Oct 18 '22 07:10

Rupok


There's a document on Java world, It's POI-Fect showing you how this is achieved.

Example:

String fileName = "C:/MyExcelFile.xls"; // file we are interested in
POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workbook.getSheetAt(0); //Get first Excel Sheet

//STRING and NUMERIC are now Enums from org.apache.poi.ss.usermodel
for (Row row : sheet) {
        for (Cell cell: row) {
            switch (cell.getCellTypeEnum()) {
                case STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t\t");
                    break;
                case NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t\t");
                    break;
                default:
            }
        }
        System.out.println("");
    }
like image 8
Buhake Sindi Avatar answered Oct 18 '22 07:10

Buhake Sindi