Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 2.45 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Excel file reading in Java(Simple JSP and Servlet)
  2. import org.apache.poi.ss.usermodel.Sheet;
  3. import org.apache.poi.ss.usermodel.Workbook;
  4. import org.apache.poi.ss.usermodel.WorkbookFactory;
  5. import org.apache.poi.xssf.usermodel.XSSFCell;
  6. import org.apache.poi.xssf.usermodel.XSSFRow;
  7. import org.apache.poi.xssf.usermodel.XSSFSheet;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9.  
  10. private Vector importExcelSheet(String fileName)
  11. {
  12.     Vector cellVectorHolder = new Vector();
  13.     try
  14.     {
  15.         Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
  16.         Sheet sheet = workBook.getSheetAt(0);
  17.         Iterator rowIter = sheet.rowIterator();
  18.  
  19.         while(rowIter.hasNext())
  20.         {
  21.             XSSFRow row = (XSSFRow) rowIter.next();
  22.             Iterator cellIter = row.cellIterator();
  23.             Vector cellStoreVector=new Vector();
  24.  
  25.             while(cellIter.hasNext())
  26.             {
  27.                 XSSFCell cell = (XSSFCell) cellIter.next();
  28.                 cellStoreVector.addElement(cell);
  29.             }
  30.             cellVectorHolder.addElement(cellStoreVector);
  31.         }
  32.     }
  33.     catch (Exception e)
  34.     {
  35.         System.out.println(e.getMessage());
  36.     }
  37.     return cellVectorHolder;
  38. }
  39.        
  40. Vector dataHolder=importExcelSheet("Excel_file.xlsx");
  41.        
  42. import java.io.File;
  43.     import java.io.IOException;
  44.  
  45.     import jxl.Cell;
  46.     import jxl.CellType;
  47.     import jxl.Sheet;
  48.     import jxl.Workbook;
  49.     import jxl.read.biff.BiffException;
  50.  
  51.     public class ReadExcel {
  52.  
  53.    private String inputFile;
  54.  
  55.    public void setInputFile(String inputFile) {
  56.     this.inputFile = inputFile;
  57.         }
  58.  
  59.    public void read() throws IOException  {
  60.     File inputWorkbook = new File(inputFile);
  61.   Workbook w;
  62. try {
  63. w = Workbook.getWorkbook(inputWorkbook);
  64. // Get the first sheet
  65. Sheet sheet = w.getSheet(0);
  66. // Loop over first 10 column and lines
  67. for (int j = 0; j < sheet.getColumns(); j++) {
  68. for (int i = 0; i < sheet.getRows(); i++) {
  69. Cell cell = sheet.getCell(j, i);
  70. CellType type = cell.getType();
  71. if (cell.getType() == CellType.LABEL) {
  72. System.out.println("I got a label "
  73.                     + cell.getContents());
  74.  }
  75.   if (cell.getType() == CellType.NUMBER) {
  76. System.out.println("I got a number "
  77.                     + cell.getContents());
  78.  }
  79.  }
  80. }
  81. } catch (BiffException e) {
  82.  e.printStackTrace();
  83. }
  84. }
  85.  
  86. public static void main(String[] args) throws IOException {
  87. ReadExcel test = new ReadExcel();
  88. test.setInputFile("c:/temp/lars.xls");
  89. test.read();
  90.         }
  91.  
  92.     }