Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to read a excel file in java

//You need Apache POI library in order to work this code

//Using Apache POI
    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.IOException;  
    import org.apache.poi.hssf.usermodel.HSSFSheet;  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    import org.apache.poi.ss.usermodel.Cell;  
    import org.apache.poi.ss.usermodel.FormulaEvaluator;  
    import org.apache.poi.ss.usermodel.Row;  
    public class ReadExcelFileDemo  
    {  
    public static void main(String args[]) throws IOException  
    {  
    //obtaining input bytes from a file  
    FileInputStream fis=new FileInputStream(new File("C:demostudent.xls"));  
    //creating workbook instance that refers to .xls file  
    HSSFWorkbook wb=new HSSFWorkbook(fis);   
    //creating a Sheet object to retrieve the object  
    HSSFSheet sheet=wb.getSheetAt(0);  
    //evaluating cell type   
    FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();  
    for(Row row: sheet)     //iteration over row using for each loop  
    {  
    for(Cell cell: row)    //iteration over cell using for each loop  
    {  
    switch(formulaEvaluator.evaluateInCell(cell).getCellType())  
    {  
    case Cell.CELL_TYPE_NUMERIC:   //field that represents numeric cell type  
    //getting the value of the cell as a number  
    System.out.print(cell.getNumericCellValue()+ "		");   
    break;  
    case Cell.CELL_TYPE_STRING:    //field that represents string cell type  
    //getting the value of the cell as a string  
    System.out.print(cell.getStringCellValue()+ "		");  
    break;  
    }  
    }  
    System.out.println();  
    }  
    }  
    }  
Comment

PREVIOUS NEXT
Code Example
Java :: java logger 
Java :: java bukkit double jump 
Java :: Why should we mention a method throws some exception/s? 
Java :: android studio constraint layout proportional height 
Java :: select photo from camera android 
Java :: Java, how to compare Strings with String Arrays 
Java :: define a list java 
Java :: contains hashmap java 
Java :: difference between access specifiers and access modifiers in java 
Java :: java read directory 
Java :: Java make numbers 
Java :: java printf trailing whitespace 
Java :: days between two dates android 
Java :: getcurrencyinstance java example 
Java :: how to use deque as queue in java? 
Java :: java loop over string 
Java :: java add to map 
Java :: How to initialize a 2d array in Java? 
Java :: what does static mean java 
Java :: Java Converting int to double 
Java :: logical operators in java 
Java :: intent pick action video android 
Java :: java array loop 
Java :: Difference between Public, Private and Protected modifier in Java? 
Java :: hide element selenium 
Java :: java long to integer 
Java :: sort list java 8 
Java :: android sqlite select query 
Java :: how to output sum of even numbers in java between two user values 
Java :: glide dependency 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =