码迷,mamicode.com
首页 > 编程语言 > 详细

java读取excel

时间:2014-08-19 20:46:05      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   os   io   for   cti   

/*
     * this function will read from excel 
     * and will return the items of excel
     */
    
    public static String[][] readExcel(String config) throws IOException
    {
        
        File f=new File(config);
        if(!f.exists())
        {
            return null;
        }
        FileInputStream fs = new FileInputStream(f);
        //create a workbook 
        Workbook wb =  new HSSFWorkbook(fs);
     
        Sheet sheet = wb.getSheetAt(0);
        int rows=sheet.getLastRowNum();
        Row firstRow=sheet.getRow(0);
        int columns=firstRow.getLastCellNum();
        String[][] data=new  String[rows+1][columns]; 
          for(int rownum=0;rownum<=sheet.getLastRowNum();rownum++)    {
                //for (Cell cell : row) 
                 Row row = sheet.getRow(rownum);

                 if (row == null) {

                     continue;

                 }
                String value;
                for(int cellnum=0;cellnum<=row.getLastCellNum();cellnum++){
                    
                    Cell cell=row.getCell(cellnum);
                    // filter the null cells 
                    if(cell==null)
                    {
                        continue;
                    }
                    else {
                        value="";
                    }
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                           // System.out.println(cell.getRichStringCellValue().getString());
                            value=cell.getRichStringCellValue().getString();
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                //System.out.println(cell.getDateCellValue());
                                value=cell.getDateCellValue().toString();
                                
                                
                            } else {
                               // System.out.println(cell.getNumericCellValue());
                                value=Double.toString((int)cell.getNumericCellValue());
                                
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            //System.out.println(cell.getBooleanCellValue());
                            value=Boolean.toString(cell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            //System.out.println(cell.getCellFormula());
                            value=cell.getCellFormula().toLowerCase();
                            break;
                        default:
                            value=" ";
                            System.out.println();
                    }
                    System.out.println(value);
               
                    data[rownum][cellnum]=value;
     
                }
          }        
        return data;
        
    }

 

java读取excel,布布扣,bubuko.com

java读取excel

标签:style   blog   color   java   os   io   for   cti   

原文地址:http://www.cnblogs.com/tobecrazy/p/3922979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!