excel讀取

  // 構造 HSSFWorkbook 對象,strPath 傳入文件路徑  
        HSSFWorkbook wb=null;
        try {
            wb = new HSSFWorkbook(new FileInputStream("C:/Users/Gavin/Desktop/test.xls"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        // 讀取文件中的第一張表格  
        HSSFSheet sheet = wb.getSheetAt(0);  
        List<CmsKeyword> list = new ArrayList<CmsKeyword>();
        int firstRowIndex = sheet.getFirstRowNum();  
        int lastRowIndex = sheet.getLastRowNum();  
        for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){  
            Row row = sheet.getRow(rIndex);  
            if(row != null){  
                int firstCellIndex = row.getFirstCellNum();  
                int lastCellIndex = row.getLastCellNum();  
                for(int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex ++){  
                    Cell cell = row.getCell(cIndex);  
                    String value = "";  
                    if(cell != null){  
                        value = cell.toString();  
                        if(!value.equals("")){
                            System.out.print(value+"\t");  
                            CmsKeyword keyword1 = new CmsKeyword();
                            keyword1.setName(value);
                            keyword1.setUrl("<span><a class=\"name\" target=\"_blank\" style=\"color:blue\">"+value+"</a></span>");
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date date = null;
                            try {
                                date = new SimpleDateFormat("yyyy-MM-dd").parse(format.format(new Date()));
                            } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }     
                            java.sql.Date  sqlDate  =  new java.sql.Date(date.getTime());  
                            keyword1.setCreateTime(sqlDate);
                            String [] name = new String[1];
                            name[0]=value;
                            List<CmsKeyword> key=manager.findByKeyWordName(name);
                            if(key.size()>0){
                                keyword1.setExplanation(key.get(0).getExplanation());
                                System.out.println(key.get(0).getExplanation());
                                
                            }
                            
                            manager.deleteByName(value);
                            keyword1.setClassifyId("75");
                            manager.save(keyword1);
                        }
                    }  
                }  
                System.out.println();  
            }  
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章