jxl 导入excel

笔记本连接:http://note.youdao.com/share/?id=467650ec0f189b843f65d49e3186f12e&type=note

读取上传文件,存入本地服务器。

 

String fileName =fileData.getPath();
        //File file = new File(fileName);
        Date date=new Date();
        String name="F19010107+"+loginname+"("+DateTools.format(date, "yyyyMMddHHmmss")+").xls";//新建文件名称
        String wholeName=truepath+"/"+name; //存放文件路径的全名
        File whole = new File(wholeName);
        //保存文件
        File source=fileData;//源文件
//        File targetPath=new File(truepath);
//        if(!targetPath.exists()){
//            targetPath.mkdirs();
//        }    
        OutputStream out=null;
        InputStream in=null;
        try {
            out=new FileOutputStream(whole);
            in=new FileInputStream(source);
            byte[] buffer=new byte[1024];
            int len=0;
            while((len=in.read(buffer))!=-1){
                out.write(buffer, 0, len);
            }
            out.flush();
        }
        catch (FileNotFoundException e1) {
            e1.printStackTrace();
            throw new BusinessException(e1);
        }
        catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(e);
        }finally{
            try {
                out.close();
                in.close();
            }
            catch (IOException e) {
                e.printStackTrace();
                throw new BusinessException(e);
            }
            
        }

 

 

 

String newFilePath = writeFile();
                    //打开文件
                    File newFile = new File(newFilePath);
                    Workbook book = Workbook.getWorkbook(newFile) ;
                    //取得第一个sheet
                    Sheet sheet = book.getSheet(0);
                    //取得行数
                    int rows = sheet.getRows();

                        if(i==1001){//超过1000条提示
                            //关闭文件
                            book.close();
                            f8104Service.updateComCount(loginname,flagdto.getImportid());
                            //result = "{info:\'导入工资成功!\'}";
                            this.setInfo("当前文件记录过多,请将1000行以后数据另存后重新导入!");
                            return SUCCESS;
                        }
                        Cell [] cell = sheet.getRow(i);
                        count = i+1;
                        UaasComImportDetail dto = new UaasComImportDetail();
                        //dto.setLsh(lsh);
                        dto.setImportid(flagdto.getImportid());
                        dto.setLoginname(Long.valueOf(loginname));
                        for(int j=0; j<3; j++) {
                            row = j+1;
                            //getCell(列,行)System.out.print(sheet.getCell(j, i).getContents());
                            if(j==0){//证件号码
                                String aac147 = sheet.getCell(j, i).getContents().replaceAll("x", "X").replaceAll("X", "X").replaceAll(" ", "").toUpperCase();
                                //aac147 = aac147.substring(0, 18);
                                dto.setAac147(aac147);
                             }
                             if(j==1){//姓名
                                 dto.setAac003(sheet.getCell(j, i).getContents());
                             }
                             if(j==2){//劳动工资工资额
                                 String s= sheet.getCell(j, i).getContents();
                                 if(null==sheet.getCell(j, i).getContents() || "".equals(sheet.getCell(j, i).getContents())){
                                     dto.setAec007(0.00D);
                                 }else{
                                     Double x = Double.valueOf(s);
                                     //String xxx = String.format("%.2f", x);
                                   String gzje = String.format("%.2f", x);
                                     dto.setAec007(Double.valueOf(gzje));
                                 }
                                 //dto.setAec007(Number2(dto.getAec007()));
                             }
                        }
                        dto.setAeb001(comdto.getAeb001());//劳动工资单位编号
                        dto.setAae002(Integer.valueOf(aae002));//工资年月
                        dto.setAeb301(comdto.getAeb301());//行政区划
                        dto.setAec038(Integer.valueOf(i));//导入序号
                        dto.setAec009("9");//处理标志
                        dto.setAae100("1");//有效标志
                        if ( !"".equals(dto.getAac147()) && null != dto.getAac147()){
                            f8104Service.saveUaasComImportDetail(dto);
                        }
                        //System.out.print(count);
                        
                    }
                                        book.close();

 

这点代码,对于导入足够了

 

posted @ 2013-08-28 22:49  小伙、不一般  阅读(982)  评论(0编辑  收藏  举报