Skip to content

Instantly share code, notes, and snippets.

@erajanraja24
Last active February 10, 2016 09:42
Show Gist options
  • Save erajanraja24/47a2e94e8410a29b99a5 to your computer and use it in GitHub Desktop.
Save erajanraja24/47a2e94e8410a29b99a5 to your computer and use it in GitHub Desktop.
How to read data from excel cell using Apache POI in java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class writeexcel {
public static void main(String[] args) throws IOException
{
//Create an object variable to get the data
writeexcel t1=new writeexcel();
//Give the path where your excel file exist
String path="D:\\Eclipse project 1\\Javacheck\\test.xlsx";
System.out.println(" The cell value is " +t1.getvalue(0, 20, 0, path));
}
public Cell getvalue(int colno,int rowno,int shtno,String path) throws IOException
{
FileInputStream file;
file = new FileInputStream(new File(path));
XSSFWorkbook wb;
wb = new XSSFWorkbook(file);
XSSFSheet sh0=wb.getSheetAt(shtno);
Cell cell;
int rn = rowno;
Row r = wb.getSheetAt(shtno).getRow(rn);
if (r == null)
{
// No values exist in this row
r = wb.getSheetAt(shtno).createRow(rn);
}
cell=sh0.getRow(rowno).getCell(colno);
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK || cell.getCellType()==Cell.CELL_TYPE_NUMERIC
||cell.getCellType() == Cell.CELL_TYPE_STRING ||cell.getCellType() == Cell.CELL_TYPE_BOOLEAN )
{
// This cell is empty
if (cell == null)
{
cell = r.createCell(colno, Cell.CELL_TYPE_STRING);
wb.close();
return null;
}
wb.close();
return cell;
}
else
{
wb.close();
return cell;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment