使用poi 導入 excel

@RequestMapping(value="/import")
    //導入
    public String impot(@RequestParam MultipartFile[] file,HttpServletRequest request) throws IOException{
        for(MultipartFile myfile : file){  
            if(myfile.isEmpty()){  
                System.out.println("文件未上傳");  
            }else{  
                System.out.println("文件長度: " + myfile.getSize());  
                System.out.println("文件類型: " + myfile.getContentType());  
                System.out.println("文件名稱: " + myfile.getName());  
                System.out.println("文件原名: " + myfile.getOriginalFilename());  
                System.out.println(request.getSession().getServletContext());
                System.out.println("========================================");  
                
                
              //如果用的是Tomcat服務器,則文件會上傳到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夾中  
                String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");  
                //這裏不必處理IO流關閉的問題,因爲FileUtils.copyInputStreamToFile()方法內部會自動把用到的IO流關掉,我是看它的源碼才知道的  
                FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
                
                try {
                    InputStream input = new FileInputStream(realPath+"\\"+myfile.getOriginalFilename());
                    POIFSFileSystem fs = new POIFSFileSystem(input);
                    HSSFWorkbook wb = new HSSFWorkbook(fs);
                    HSSFSheet sheet = wb.getSheetAt(0);
                    Iterator<Row> rows = sheet.rowIterator();
                    
                    while (rows.hasNext()) {
                        HSSFRow row = (HSSFRow) rows.next();
                        System.out.println("Row #" + row.getRowNum());
                        Iterator<Cell> cells = row.cellIterator();
                        /*System.out.println(row.getCell(0).getStringCellValue()+row.getCell(1).getStringCellValue()+row.getCell(2).getStringCellValue()+row.getCell(3).getNumericCellValue());*/
                        System.out.println(row.getCell(0)+"  "+row.getCell(1)+"  "+row.getCell(2)+"  "+row.getCell(3));
                        User user = new User();
                        //user.setUid((long)row.getCell(0).getNumericCellValue());
                        if(!row.getCell(1).toString().equals("姓名")&&!row.getCell(2).toString().equals("密碼")&&!row.getCell(3).equals("年齡")){
                            user.setUsername(row.getCell(1).toString());
                            user.setPassword(row.getCell(2).toString());
                            user.setAge((long)row.getCell(3).getNumericCellValue());
                            userService.save(user);  
                        }
                        /*while (cells.hasNext()) {
                            HSSFCell cell = (HSSFCell) cells.next();
                            System.out.println("Cell #" + cell.getCellNum());
                            String username;
                            String password;
                            Long a;
                            switch (cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                
                                System.out.println(cell.getNumericCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                                System.out.println(cell.getStringCellValue());
                                
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                System.out.println(cell.getBooleanCellValue());
                                break;    
                            case HSSFCell.CELL_TYPE_FORMULA:
                                System.out.println(cell.getCellFormula());
                                break;
                            default:
                                System.out.println("unsuported sell type");
                                break;
                            }
                        
                        }*/
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                
            }  
        }  
         return WebUtil.convertToJsonData("success");
        
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章