Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Code For Reading Excel file data


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Intermediate

Published: 23rd Apr 2009   Read: 34109 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This is code is useful for reading the data from Excel sheet

 


Sample Code
  1.  
  2. package testexcel;
  3.  
  4. import java.io.IOException;
  5. import java.io.*;
  6. import java.util.Iterator;
  7. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  8. import org.apache.poi.hssf.usermodel.HSSFCell;
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import org.apache.poi.hssf.usermodel.HSSFRow;
  12.  
  13.  
  14.  
  15. public class Readingexcel {
  16.  
  17.     public static void main(String[] args) {
  18.         try {
  19.              System.out.println("before reading");
  20.             POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("/home/something/Desktop/Test.xls"));
  21.             HSSFWorkbook wb = new HSSFWorkbook(fs);
  22.             HSSFSheet sheet = wb.getSheet("first");
  23.            
  24.             HSSFRow row;
  25.             HSSFCell cell;
  26.             String s;
  27.            
  28.             int rows; // No of rows
  29.             rows = sheet.getPhysicalNumberOfRows();
  30.              System.out.println(rows);
  31.              System.out.println(sheet.getRow(1).getPhysicalNumberOfCells());
  32.             int cols = 0; // No of columns
  33.             int tmp = 0;
  34.  
  35. // This trick ensures that we get the data properly even if it doesn’t start from first few rows
  36.             for (int i = 0; i < 10 || i < rows; i++) {
  37.                 row = sheet.getRow(i);
  38.                
  39.                 if (row != null) {
  40.                     tmp = sheet.getRow(i).getPhysicalNumberOfCells();
  41.                      
  42.  
  43.                     if (tmp > cols) {
  44.                         cols = tmp;
  45.                        
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             for (int r = 0; r < rows; r++) {
  51.                 row = sheet.getRow(r);
  52.                 if (row != null) {
  53.                     for (int c = 0; c < cols; c++) {
  54.                         cell = row.getCell((short) c);
  55.                         if (cell != null) {
  56. // Your code here
  57. // s = cell.getData();
  58.                            System.out.println(cell.getStringCellValue());
  59.  
  60.  
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.         } catch (Exception ioe) {
  66.             ioe.printStackTrace();
  67.         }
  68.  
  69.     }
  70. }
  71.  
Copyright GeekInterview.com


Next Article: Java Code For Password Encryption


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora221539
2. iamdvr136201
3. venki_madesh38311
4. Raju30602
5. Kiran.jakkaraju22720
6. Nidhi Saxena22063
7. parmod kumar duhan20523
8. yuvanmytri14968
9. venkat_kiran12112
10. chowsys10986

Active Coders

Refined Tags