java Excel 上传

来源:互联网 发布:unity3D音乐 编辑:程序博客网 时间:2024/06/11 11:16

/**
     * Excel数据上传到纳税人信息表中
     * @param File
     * @return void
     */

【注://file  jsp--file; Action  ---- private File file; get()ゲート メーソド】
   public void excel2db(File file) throws SQLException,
            FileNotFoundException, IOException, ParseException {
        HSSFWorkbook workbook;
        workbook = new HSSFWorkbook(new FileInputStream(file));
        if(file ==null){
            log.info("上传的Excel没找到");
            return;
        }
        HSSFSheet sheet = workbook.getSheet("nsrList");
        int rows = sheet.getPhysicalNumberOfRows();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       

        try {
            for (int r = 1; r < rows; r++) {
                HSSFRow row = sheet.getRow(r);
                if (row != null) {
                    try {
                        String t1 = "";
                        HSSFCell t1cell = row.getCell((short) 0);
                        if (t1cell != null) {
                            t1 = t1cell.getStringCellValue();
                        } else {
                            t1 = null;
                        }

                        String t2 = "";
                        HSSFCell recroadTypecell = row.getCell((short) 1);
                        if (recroadTypecell != null) {
                            t2 = recroadTypecell.getStringCellValue();
                        } else {
                            t2 = null;
                        }

                        String t3 = "";
                        HSSFCell t3cell = row.getCell((short) 2);
                        if (t3cell != null) {
                            t3 = t3cell.getStringCellValue();
                        } else {
                            t3 = null;
                        }

                        String t4 = "";
                        HSSFCell t4cell = row.getCell((short) 3);
                        if (t4cell != null) {
                            t4 = t4cell.getStringCellValue();
                        } else {
                            t4 = null;
                        }

                        String t5 = "";
                        HSSFCell t5cell = row.getCell((short) 4);
                        if (t5cell != null) {
                            t5 = t5cell.getStringCellValue();
                        } else {
                            t5 = null;
                        }
                        String t6 = "";
                        HSSFCell t6cell = row.getCell((short) 5);
                        if (t6cell != null) {
                            t6 = t6cell.getStringCellValue();
                        } else {
                            t6 = null;
                        }
                        String t7 = "";
                        HSSFCell t7cell = row.getCell((short) 6);
                        if (t7cell != null) {
                            t7 = t7cell.getStringCellValue();
                        } else {
                            t7 = null;
                        }
                        String t8 = "";
                        HSSFCell t8cell = row.getCell((short) 7);
                        if (t8cell != null) {
                            t8 = t8cell.getStringCellValue();
                        } else {
                            t8 = null;
                        }
                        String t9 = "";
                        HSSFCell t9cell = row.getCell((short) 8);
                        if (t9cell != null) {
                            t9 = t9cell.getStringCellValue();
                        } else {
                            t9 = null;
                        }
                        Integer t10 = 0;
                        HSSFCell t10cell = row.getCell((short) 9);
                         if (t10cell != null) {
                             t10 = t10cell.getCellType();
                        } else {
                            t10 = null;
                        }
                         
                        Double t11 = 0.0d;
                        String str11="";
                        HSSFCell t11cell = row.getCell((short) 10);// 营业执照字号
                        if (t11cell != null) {// 可能不是数字,比如:A0008
                            str11  = parseExcelDate(t11cell);
                            t11 = Double.parseDouble(t11cell.getStringCellValue());
                        }
                       
                        Date t12 = null;
                        String str12="";
                        HSSFCell t12cell = row.getCell((short) 11);
                        if(t12cell !=null){
                             str12 = parseExcelDate(t12cell);
                             if(str12 != null && !str12.equals("")){//9-10号修改
                                 t12 = sdf.parse(str12);
                             }
                        }else{
                            t12 = null;
                        }
                       
                       
                       
                        Date t13 = null;
                        HSSFCell t13cell = row.getCell((short) 12);
                        String temp13="";
                        if(t13cell !=null){
                            temp13 = parseExcelDate(t13cell);
                            if(temp13 !=null){
                                t13 = sdf.parse(temp13);
                            }
                        }else{
                            t13 = null;
                        }
                       
                       
                       

                        Date t14 = null;
                        String t114="";
                        HSSFCell t14cell = row.getCell((short) 13);
                        if(t14cell !=null){
                            t114 = parseExcelDate(t14cell);
                            if (t114 !=null) {
                                t14 = sdf.parse(t114);
                            }
                        }else{
                            t14 = null;
                        }
                       
                        Date t15 = null;
                        HSSFCell t115cell = row.getCell((short) 14);
                        String t115="";
                        if(t115cell != null){
                            t115 = parseExcelDate(t115cell);
                            if (t115 !=null) {
                                System.out.println(" t115 : "+t115);
                                t15 = sdf.parse(t115);   
                            }
                        }else{
                            t15 = null;
                        }
                       
                       
                        String t16 = "";
                        HSSFCell t16cell = row.getCell((short) 15);
                        if (t16cell != null) {
                            t16 = t16cell.getStringCellValue();
                        } else {
                            t16 = null;
                        }
                        String t17 = "";
                        HSSFCell t17cell = row.getCell((short) 16);
                        if (t17cell != null) {
                            t17 = t17cell.getStringCellValue();
                        } else {
                            t17 = null;
                        }
                       
                        Date t18 = null;
                        String t188="";
                        HSSFCell t18cell = row.getCell((short) 17);
                        if(t18cell != null){
                            t188 = parseExcelDate(t18cell);
                            if (t188 !=null) {
                                t18 = sdf.parse(t188);
                            }
                        }else{
                            t18 = null;
                        }
                       
                       
                        String t19 = "";
                        HSSFCell t19cell = row.getCell((short) 18);
                        if (t19cell != null) {
                            t19 = t19cell.getStringCellValue();
                            } else {
                            t19 = null;
                        }
                        String t20 = "";
                        HSSFCell t20cell = row.getCell((short) 19);
                        if (t20cell != null) {
                            t20 = t20cell.getStringCellValue();
                        } else {
                            t20 = null;
                        }
                        String t21 = "";
                        HSSFCell t21cell = row.getCell((short) 20);
                        if (t21cell != null) {
                            t21 = t21cell.getStringCellValue();
                        } else {
                            t21 = null;
                        }

                        TaxpayerInfo taxpayerInfo = new TaxpayerInfo();
                        taxpayerInfo.setRecroadType(t1);
                        taxpayerInfo.setTaxpayerId(t2);
                        taxpayerInfo.setTaxpayerNumber(t3);
                        taxpayerInfo.setTaxpayerName(t4);
                        taxpayerInfo.setTaxpayerRegAddress(t5);
                        taxpayerInfo.setTaxpayerDealAddress(t6);
                        taxpayerInfo.setTaxpayerDealScope(t7);
                        taxpayerInfo.setChargeTaxOrgan(t8);
                        taxpayerInfo.setBusinessOrganName(t9);
                        taxpayerInfo.setBusinessLicenceName(t10);
                        taxpayerInfo.setBusinessTradeName(t11);
                        taxpayerInfo.setIssuerDatetime(t12);
                        taxpayerInfo.setStartBusinessTime(t13);
                        taxpayerInfo.setValidityStart(t14);
                        taxpayerInfo.setValidityEnd(t15);
                        taxpayerInfo.setDealPattern(t16);
                        taxpayerInfo.setIndustry(t17);
                        taxpayerInfo.setApproveTaxRegTime(t18);
                        taxpayerInfo.setTaxpayerState(t19);
                        taxpayerInfo.setChargeTaxOfficer(t20);
                        taxpayerInfo.setStreetVillageTowns(t21);
                       
                        this.taxpayerInfoDAO.saveExceltoDB(taxpayerInfo);

                    } catch (ParseException e) {
                        e.printStackTrace();
                        log.info("excel数据格式异常:"+e.getMessage());
                    }
                }
            }// for
        } catch (Exception e) {
            log.info("读取Excel异常:"+e.getMessage());
            e.printStackTrace();
        }finally{
            log.info("纳税人资料读取完毕!");
        }
    }


原创粉丝点击