import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
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 ExcelCellData {
public static void main(String[] args) throws Exception {
FileInputStream file = new FileInputStream(new File("Excel file with path.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet =
workbook.getSheetAt(0);
Double numericValue_Cell=
sheet.getRow(3).getCell(3).getNumericCellValue();
// a particular cell value
// a particular cell value
System.out.println("numericValue_Cell = "+numericValue_Cell);
String stringValue_Cell =
sheet.getRow(2).getCell(2).getStringCellValue();
System.out.println("stringValue_Cell = "+stringValue_Cell);
boolean booleanValue_Cell =
sheet.getRow(4).getCell(1).getBooleanCellValue();
System.out.println("booleanValue_Cell = "+booleanValue_Cell);
String
formulaValue_Cell = sheet.getRow(3).getCell(3).getCellFormula();
System.out.println("formulaValue_Cell = "+formulaValue_Cell);
System.out.println("\n-----Iteration Part -----\n");
//To read all
the values of respective type
Iterator iterateROWS =
sheet.iterator();
while(iterateROWS.hasNext()) {
Row row = iterateROWS.next();
Iterator iterateColumns = row.cellIterator(); |
while(iterateColumns.hasNext())
{
Cell cell = iterateColumns.next();
switch(cell.getCellType()) {
//To get only numberic(double) values used in the sheet
case Cell.CELL_TYPE_NUMERIC:
String numericValue_All=
String.valueOf(cell.getNumericCellValue());
System.out.println("numericValue_All = "+numericValue_All);
break ;
//To get only String values used in the sheet
case Cell.CELL_TYPE_STRING:
String
stringValue_All = String.valueOf(cell.getStringCellValue());
System.out.println("stringValue_All = "+stringValue_All);
break ;
//To get only boolean values used in the sheet
case Cell.CELL_TYPE_BOOLEAN:
String
booleanValue_All = String.valueOf(cell.getBooleanCellValue());
System.out.println("booleanValue_All = "+booleanValue_All);
break ;
//To get only formulas used in the sheet
case Cell.CELL_TYPE_FORMULA:
String
formulaValue_All = String.valueOf(cell.getCellFormula());
System.out.println("formulaValue_All = "+formulaValue_All);
break ;
}}}}}
Output:
numericValue_Cell
= 479.0
stringValue_Cell
= TTT
booleanValue_Cell
= true
formulaValue_Cell
= C5*D3+C1
-----Iteration
Part -----
numericValue_All
= 123.0
numericValue_All
= 56.0
numericValue_All
= 100.1
numericValue_All
= 2.0
numericValue_All
= 55.0
numericValue_All
= 11.01
numericValue_All = 66.44
stringValue_All
= Aaa
stringValue_All
= Bbb
stringValue_All
= yyyy
stringValue_All
= eee
stringValue_All
= DDD
stringValue_All
= TTT
stringValue_All
= SSS
stringValue_All = ggg
booleanValue_All
= true
booleanValue_All
= false
booleanValue_All = false
formulaValue_All
= C5*D3+C1
formulaValue_All = C1+C4
No comments:
Post a Comment