-1

下面的代码是读取一个大的excel文件。我收到“无法从 org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch 的数字单元格获取文本值”错误。我该如何解决。第 1 列是数值,第 2 列是字符串值。

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
    import org.apache.poi.hssf.usermodel.HSSFSheet;   
    import org.apache.poi.hssf.usermodel.HSSFRow;   
    import org.apache.poi.hssf.usermodel.HSSFCell;   
    import java.io.FileInputStream;   
    import java.io.IOException;   
    import java.util.Iterator;   
    import java.util.Vector;   

    public class ReadExcel {   

        public static void main(String[] args) throws Exception {   

            String filename = "C:/Documents and Settings/oemiola/My Documents/Defects_Input.xls";   
            FileInputStream fis = null;   

            try {   
                fis = new FileInputStream(filename);   
                HSSFWorkbook workbook = new HSSFWorkbook(fis);   
                HSSFSheet sheet = workbook.getSheetAt(0);   
                Iterator rowIter = sheet.rowIterator();    

                while(rowIter.hasNext()){   
                    HSSFRow myRow = (HSSFRow) rowIter.next();   
                    Iterator cellIter = myRow.cellIterator();   
                    Vector<String> cellStoreVector=new Vector<String>();   
                    while(cellIter.hasNext()){   
                        HSSFCell myCell = (HSSFCell) cellIter.next();   
                        String cellvalue = myCell.getStringCellValue();   
                        cellStoreVector.addElement(cellvalue);   
                    }   
                    String firstcolumnValue = null;   
                    String secondcolumnValue = null;   

                    int i = 0;   
                    firstcolumnValue = cellStoreVector.get(i).toString();    
                    secondcolumnValue = cellStoreVector.get(i+1).toString();   

                    insertQuery(firstcolumnValue,secondcolumnValue);   
               }   

            } catch (IOException e) {   

                e.printStackTrace();   

            } finally {   

                if (fis != null) {   

                    fis.close();   
                }   
            }   

        }   

        private static void insertQuery(String firstcolumnvalue,String secondcolumnvalue) {   

            System.out.println(firstcolumnvalue +  " "  +secondcolumnvalue);   


        }   

    }  
4

1 回答 1

0

用于Cell#getCellType确定单元格中是否有数字或字符串。如果它是数字,则使用getNumericCellValue而不是getStringCellValue.

于 2013-07-01T19:20:35.983 回答