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 表,其中包含一些空白单元格。所以我也无法正确阅读表格。任何机构都可以给我一种方法吗?所以在逐行阅读时,当它在中间找到一个空白单元格时,列数会减少。

4

3 回答 3

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;
    }

}
于 2012-09-28T11:55:00.690 回答
0

要与 Office 文档交互,唯一真正的解决方案是使用 API,例如Apache POI

于 2012-09-28T10:35:55.993 回答
0

一路APACHE POI库

于 2012-09-28T10:36:47.677 回答