2012-09-28 52 views
0
Date Emp_Code Emp_Name Cardno Shift_Start IN OUT Shift_End Status Emp_Late Left_Early Hours_Worked O T OS 
26. Jan. 2011 001 KL Acharya 000001 9:00 9:15 18:34 18:00 P 0.15 0.00 9.19 0:00 0:34 
26. Jan. 2011 002 Seemakiran Upadhya 000002 9:00   18:00 A    0:00  
26. Jan. 2011 013 Sumana Ravishankar 000013 9:00   18:00 A    0:00  
26. Jan. 2011 017 Gopalkrishna Prabhau KV 000017 9:00   18:00 A    0:00  
26. Jan. 2011 021 Sarath Modali 000021 20:00 21:02 6:40 6:00 P 1.02 0.00 9.38 0:00 0:40 
26. Jan. 2011 023 Siddharth Singh 000023 9:00   18:00 A    0:00  
26. Jan. 2011 034 Meghana K 000034 9:00   18:00 A    0:00  
26. Jan. 2011 036 Rajendra KS 000036 9:00   18:00 A    0:00  
26. Jan. 2011 037 Rajesh K 000037 9:00   18:00 A    0:00  
26. Jan. 2011 039 Mahesh PR 000039 9:00   18:00 A    0:00  
26. Jan. 2011 041 Krishnamoorthy A 000041 9:00   18:00 A    0:00  
26. Jan. 2011 047 Smruti Ranjan Panda 000047 9:00   18:00 A    0:00  
26. Jan. 2011 049 Raghuraman K 000049 9:00 1:12  18:00 MS 0.00   0:00  
26. Jan. 2011 055 Raghavendra HS 000055 9:00   18:00 A    0:00  
26. Jan. 2011 063 Anoop Chandran U 000063 9:00   18:00 A    0:00  
26. Jan. 2011 069 Ramesh Kumar Hegde 000069 9:00   18:00 A    0:00  
26. Jan. 2011 070 Mohan T 000070 9:00   18:00 A    0:00  
26. Jan. 2011 078 Shurabh Chaubey 000078 20:00 9:38 10:49 6:00 A 0.00 0.00 1.11 0:00  
26. Jan. 2011 079 Sourabha Mahopatra 000079 9:00   18:00 A    0:00  

這是我的Excel表中包含一些空白單元格之間。所以我不能正確閱讀表格。任何機構都可以讓我做到這一點。因此,在閱讀行數據時,列數減少時,它會在它們之間形成空白單元格。閱讀java中的Excel表格。該excel表包含一些銀行細胞以及

+0

也是真的XLS或csv? – dngfng

+0

目前尚不清楚你的問題是什麼......你能否澄清你想爲空白單元格發生什麼,你想跳過它們還是數它們?到目前爲止您嘗試過哪些方法,哪些方法無效? – Gagravarr

+0

它可能會幫助你 http://stackoverflow.com/a/12067137/1211000 – swamy

回答

0

與Office文檔交互的唯一真正的解決辦法是使用一個API,如Apache POI

0

使用POI-3.8!
這可以處理空白單元格!

package com.vaadin.addon.tableimport; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Row; 

public class ExcelImport implements Serializable { 

    private static final long serialVersionUID = -8404407996727936498L; 

    protected HSSFWorkbook workbook; 

    /** The totals row. */ 
    protected Row titleRow, headerRow, totalsRow; 
    protected Row hierarchicalTotalsRow; 

    protected FileInputStream is = null; 

    /**輔助存儲*/ 
    private List<List<Object>> data; 
    private int headLineNum; 

    public ExcelImport(File file, String dealString) { 
     //獲得表頭行數 
     headLineNum = reflex(dealString); 
     //System.out.println("表頭行數 : " + headLineNum); 

     try { 
      is = new FileInputStream(file); 
      workbook = new HSSFWorkbook(is); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     HSSFSheet childSheet = workbook.getSheetAt(0); 
     //System.out.println("有Physical行數" + childSheet.getPhysicalNumberOfRows()); 
     //System.out.println("有行數" + childSheet.getLastRowNum() + " , " +childSheet.getFirstRowNum()); 
     Object o; 

     data = new ArrayList<List<Object>>(); 
     for (int aa = headLineNum; aa < childSheet.getPhysicalNumberOfRows(); aa++) { 
      data.add(new ArrayList<Object>()); 
      for (int bb = 0; bb < childSheet.getRow(aa).getLastCellNum(); bb++) { 
       data.get(aa-headLineNum).add(new String()); 
      } 
     } 

     for (int j = 0; j < childSheet.getPhysicalNumberOfRows(); j++) { 
      HSSFRow row = childSheet.getRow(j); 
      //System.out.println("有Physical列數" + row.getPhysicalNumberOfCells()); 
      //System.out.println("有列數" + row.getLastCellNum()); 


      if (null != row) { 
       for (int k = 0; k < row.getLastCellNum(); k++) { 
        HSSFCell cell = row.getCell(k); 

        if (null != cell) { 
         switch (cell.getCellType()) { 
         case HSSFCell.CELL_TYPE_NUMERIC: // 數字 
          //System.out.print(cell.getNumericCellValue() + " "); 
          //o = String.valueOf(cell.getNumericCellValue()); 
          o = cell.getNumericCellValue(); 
          break; 
         case HSSFCell.CELL_TYPE_STRING: // 字符串 
          //System.out.print(cell.getStringCellValue() + " "); 
          //o = String.valueOf(cell.getStringCellValue()); 
          o =cell.getStringCellValue(); 
          break; 
         case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean 
          //System.out.println(cell.getBooleanCellValue() + " "); 
          //o = String.valueOf(cell.getBooleanCellValue()); 
          o =cell.getBooleanCellValue(); 
          break; 
         case HSSFCell.CELL_TYPE_FORMULA: // 公式 
          //System.out.print(cell.getCellFormula() + " "); 
          //o = String.valueOf(cell.getCellFormula()); 
          o =cell.getCellFormula(); 
          break; 
         case HSSFCell.CELL_TYPE_BLANK: // 空值 -->Null Value 
          //System.out.println(" "); 
          o = " "; 
          break; 
         case HSSFCell.CELL_TYPE_ERROR: // 故障 
          //System.out.println(" "); 
          o = " "; 
          break; 
         default: 
          //System.out.print("未知類型 "); 
          o = " "; 
          break; 
         } 
        } else { 
         //System.out.print("- "); 
         o = "- "; 
        } 
        if(j == headLineNum) { 
         data.get(j-headLineNum).set(k, getBottomStringById(dealString,k)); 
        } 
        if(j > headLineNum) { 
         data.get(j-headLineNum).set(k, o); 
        } 

       } 
      } 
      //System.out.println(); 
     } 

//  System.out.println("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*="); 
//  for (List<Object> ll : data) { 
//   for (Object ss : ll) { 
//    System.err.print(String.valueOf(ss) + " , "); 
//   } 
//   System.err.println("*"); 
//  } 
    } 

    /**獲得表頭行數*/ 
    private int reflex(String dealString) { 
     int rows = 1; 
     TableHeadDeal thd = new TableHeadDeal(dealString); 
     rows = thd.rows; 
     return rows; 
    } 

    /**獲得表頭底部字串*/ 
    private String getBottomStringById(String dealString, int id) { 
     String BottomStr = new String(); 
     TableHeadDeal thd = new TableHeadDeal(dealString); 
     BottomStr = thd.getTableHeadBottomParams().get(id); 
     return BottomStr; 
    } 

    public List<List<Object>> getListDate() { 
     return data; 
    } 

}