【问题标题】:how to get all values with poi excel and java如何使用 poi excel 和 java 获取所有值
【发布时间】:2020-04-09 18:06:23
【问题描述】:

你好,这段代码我只是在我的数据库中得到第一行 我需要得到所有线路请应该做什么 我正在使用 Apache POI 读取 Excel 文档。我需要将所有数据存储在我的数据库中,但是使用此代码,我只需保存第一行,请我应该做什么! 如何阅读我的excel中的所有行谢谢

@PostMapping("/upload")
public ResponseEntity<?> addRfp  (@RequestParam("file") MultipartFile file) throws IOException, InvalidFormatException  {
    ZipSecureFile.setMinInflateRatio(0);



    FileInputStream fis =  (FileInputStream) file.getInputStream();
    XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
    XSSFSheet mySheet = myWorkBook.getSheetAt(0);
    Iterator<Row> rowIterator = mySheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {       
            final DataFormatter df = new DataFormatter();
            Rfp rfp = new Rfp();
           for(int i =0 ; i < mySheet.getPhysicalNumberOfRows() ; i++)
       {
           Row nextRow = mySheet.getRow(row.getRowNum() + 1);
           if (nextRow != null)
           {
          System.out.println(df.formatCellValue(row.getCell(3)));
           rfp.setIde(df.formatCellValue(row.getCell(0)));
           rfp.setUnit(df.formatCellValue(row.getCell(1)));
            rfp.setRFPID(df.formatCellValue(row.getCell(2)));
            rfp.setREQUESTID(df.formatCellValue(row.getCell(3)));
            rfp.setINCOMING(df.formatCellValue(row.getCell(4)));
            rfp.setPROJECTTITLE(df.formatCellValue(row.getCell(5)));
            rfp.setSERVICELINE(df.formatCellValue(row.getCell(6)));
            rfp.setBUSINESSUNIT(df.formatCellValue(row.getCell(7)));
            rfp.setREQUSITIONBUSINESSUNIT(df.formatCellValue(row.getCell(8)));
            rfp.setCOUNTRY(df.formatCellValue(row.getCell(9)));
            rfp.setTARGETPROPOSE(df.formatCellValue(row.getCell(10)));
            rfp.setRFX(df.formatCellValue(row.getCell(11)));
            rfp.setWORKFLOWTRACKING(df.formatCellValue(row.getCell(12)));
            rfp.setPROTOCOL(df.formatCellValue(row.getCell(13)));
            rfp.setJOURFIXKUNDE(df.formatCellValue(row.getCell(14)));
            rfp.setREJECTEDSTATUS(df.formatCellValue(row.getCell(15)));
            rfp.setCLOSEDREASON(df.formatCellValue(row.getCell(16)));
            rfp.setPRACTICE(df.formatCellValue(row.getCell(17)));
            rfp.setSTARTDATE(df.formatCellValue(row.getCell(18)));
            rfp.setENDDATE(df.formatCellValue(row.getCell(19)));
            rfp.setVERTRAGSART(df.formatCellValue(row.getCell(20)));
            rfp.setRFPCOMMENT(df.formatCellValue(row.getCell(21)));
            rfp.setPERSONDAYS(df.formatCellValue(row.getCell(22)));
            rfp.setACCOUNTEXECUTIVE(df.formatCellValue(row.getCell(23)));
            rfp.setACCOUNTSALESMANAGER(df.formatCellValue(row.getCell(24)));
            rfp.setEXPERTSALES(df.formatCellValue(row.getCell(25)));
            rfp.setDELIVERYUNIT(df.formatCellValue(row.getCell(26)));
            rfp.setDELIVERYRESPONSIBLE(df.formatCellValue(row.getCell(27)));
            rfp.setREQUISITIONER(df.formatCellValue(row.getCell(28)));
            rfp.setPROCUREMENT(df.formatCellValue(row.getCell(29)));
            rfp.setREFERENCENUMBER(df.formatCellValue(row.getCell(30)));
            rfp.setATOSCONTRACTNO(df.formatCellValue(row.getCell(31)));
            rfp.setCANDIDATES(df.formatCellValue(row.getCell(32)));
            rfp.setITEC(df.formatCellValue(row.getCell(33)));
            rfp.setTCV(df.formatCellValue(row.getCell(34)));
            rfp.setUPDATED(df.formatCellValue(row.getCell(35)));
            rfp.setUPDATEDBY(df.formatCellValue(row.getCell(36)));
            rfp.setPORTFOLIO(df.formatCellValue(row.getCell(37)));
            rfp.setPATH(df.formatCellValue(row.getCell(38)));
            rfp.setRMPROTOCOL(df.formatCellValue(row.getCell(39)));
            rfp.setRM(df.formatCellValue(row.getCell(40)));
            rfp.setWFM_ID(df.formatCellValue(row.getCell(41)));
            rfp.setNEEDCONFOPART(df.formatCellValue(row.getCell(42)));
            rfp.setCONFIRMATIONOFPARTICIPATION(df.formatCellValue(row.getCell(43)));
            rfp.setPROPOSE(df.formatCellValue(row.getCell(44)));
            rfp.setLASTCALL(df.formatCellValue(row.getCell(45)));
            rfp.setLASTCALLDATE(df.formatCellValue(row.getCell(46)));
            rfp.setSKILLS(df.formatCellValue(row.getCell(47)));
            rfp.setLEISTUNGSERBRINGUNG(df.formatCellValue(row.getCell(48)));
            rfp.setROLLEFUNKTION(df.formatCellValue(row.getCell(49)));
            rfp.setINHALTSCOPE(df.formatCellValue(row.getCell(50)));
            rfp.setLAUFZEITPERSPEKTIVE(df.formatCellValue(row.getCell(51)));
            rfp.setLAUFZEITANGEFRAGT(df.formatCellValue(row.getCell(52)));
            rfp.setPHASE(df.formatCellValue(row.getCell(53)));
            rfp.setFRISTVERLAENGERUNG(df.formatCellValue(row.getCell(54)));
            rfp.setCLOSEDSTATUS(df.formatCellValue(row.getCell(55)));
            rfp.setDEFERRED_TILL(df.formatCellValue(row.getCell(56)));
            rfp.setSIPA_YN(df.formatCellValue(row.getCell(57)));
            rfp.setPO_NO(df.formatCellValue(row.getCell(58)));
            rfp.setNESSIE_NO(df.formatCellValue(row.getCell(59)));
            rfp.setNUMBER_OF_PERSONS(df.formatCellValue(row.getCell(60)));
            rfp.setATT_JOB_SITE(df.formatCellValue(row.getCell(61)));
            rfp.setATT_LANGUAGE(df.formatCellValue(row.getCell(62)));
            rfp.setATT_MAIN_TOPICS(df.formatCellValue(row.getCell(63)));
            rfp.setREF_RFPID(df.formatCellValue(row.getCell(64)));
            rfp.setREF_HISTORY(df.formatCellValue(row.getCell(65)));
            rfp.setREF_REJECTIONS(df.formatCellValue(row.getCell(66)));
            rfp.setRV_REJ_STATUS_DETAILS(df.formatCellValue(row.getCell(67)));
            rfp.setRV_ALLIANCE_MANAGER(df.formatCellValue(row.getCell(68)));
            rfp.setRV_FINAL_STATUS(df.formatCellValue(row.getCell(69)));
            rfp.setRV_CUST_LIST_DATE(df.formatCellValue(row.getCell(70)));
            rfp.setRV_FINAL_NOTE(df.formatCellValue(row.getCell(71)));
            rfp.setRV_ATOS_ROOT_CAUSES(df.formatCellValue(row.getCell(72)));
            rfp.setCURRENCY(df.formatCellValue(row.getCell(73)));
            rfp.setEXTERNALID(df.formatCellValue(row.getCell(74)));
            rfp.setRESULTSTATUS(df.formatCellValue(row.getCell(75)));
            rfp.setREASONCODE(df.formatCellValue(row.getCell(76)));
            rfp.setSYSID(df.formatCellValue(row.getCell(77)));
            rfp.setNCRM_COMMENT(df.formatCellValue(row.getCell(78)));
            rfp.setIDEMAND_COMMENT(df.formatCellValue(row.getCell(79)));
            rfp.setOFFSHORE_COMMENT(df.formatCellValue(row.getCell(80)));
            rfp.setRV_RC_UPDBY(df.formatCellValue(row.getCell(81)));
            rfp.setRV_RC_DATE(df.formatCellValue(row.getCell(82)));
            rfp.setRV_FINALCOMMENT(df.formatCellValue(row.getCell(83)));
            rfp.setRV_FINALDATE(df.formatCellValue(row.getCell(84)));
            rfp.setTCV_EURO(df.formatCellValue(row.getCell(85)));
            rfp.setREM_ACTIVE(df.formatCellValue(row.getCell(86)));
            rfp.setREM_SEND(df.formatCellValue(row.getCell(87)));
            rfp.setREMCOP_ACTIVE(df.formatCellValue(row.getCell(88)));
            rfp.setREMCOP_SEND(df.formatCellValue(row.getCell(89)));
            rfp.setIDEMAND(df.formatCellValue(row.getCell(90)));
            rfp.setIDEMAND_URL(df.formatCellValue(row.getCell(91)));
            rfp.setLABOUR_LEASING_YN(df.formatCellValue(row.getCell(92)));
            rfp.setCAS_NO(df.formatCellValue(row.getCell(93)));
            rfp.setPROLONGATION(df.formatCellValue(row.getCell(94)));
            rfp.setRV_SKILLS_MISS_1(df.formatCellValue(row.getCell(95)));
            rfp.setRV_SKILLS_MISS_2(df.formatCellValue(row.getCell(96)));
            rfp.setRV_SKILLS_MISS_3(df.formatCellValue(row.getCell(97)));
            rfp.setREF_RENEWAL_DATE(df.formatCellValue(row.getCell(98)));
            rfprepository.save(rfp);
           }
       }}
        }


    return null;    
}}

【问题讨论】:

  • 这个问题看起来像 another one of your questions 的副本。
  • 请不要重复同一个问题。相反,如果您有新的见解,请通过编辑它来改进您的第一个问题。但我怀疑你是否完全理解了基础知识。为什么你认为你需要cellIterator?您可以通过索引获得所有需要的单元格。为什么每一行都使用return null

标签: java spring-boot file-upload apache-poi


【解决方案1】:

我用这种方式获取所有行。

FileInputStream fis =  (FileInputStream) file.getInputStream();
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
for(Row row : mySheet){
   final DataFormatter df = new DataFormatter();
   System.out.println(df.formatCellValue(row.getCell(0)));
}

【讨论】:

最近更新 更多