我需要从两列中依次打印Excel数据。第一列具有单一值,另一列具有多个值

时间:2020-04-25 17:11:46

标签: java excel selenium apache-poi

我想从Excel工作表中读取数据,该工作表的第一列具有单个值,而另一列中的许多值(如垃圾值)则需要按顺序格式打印第一列值和第二列值。我不知道怎么办。我正在使用#java#apache-poi #selenium

excel数据是图像: excel screenshot

    public static void main(String[] args) throws Exception {

        // Step 1: Open the excel workbook
        XSSFWorkbook wbook= new XSSFWorkbook("C:\\Selenium\\Data\\Mydata.xlsx");
        // Step 2: Go to the Sheet
                XSSFSheet sheet= wbook.getSheet("Reg Types");

                // Step a) Get the number of the rows of the sheet
                int rowCount = sheet.getLastRowNum();
                System.out.println("The row countof the regtype: "+rowCount);
                // Step b) Get the number of the columns of the sheet
                int colCount = sheet.getRow(0).getLastCellNum();
                System.out.println("The column countof the regtype: "+colCount);

                    for (int i=1;i<=rowCount; i++)
                    {
                        // Step 3a: Go to the regtypecolumn
                        XSSFCell  regType= sheet.getRow(i).getCell(0);
                        // Step 3b: Save the value of the  regtype
                        String regTypeValue = regType.getStringCellValue();
                        //4 Print regtype and create query
                         System.out.println(regTypeValue);

                        // Step 5: Go to the optioncolumn junk values
                        XSSFCell  option= sheet.getRow(i).getCell(15);
                        // Step 5a: Save the value of the  option 
                        String optionValue = option.getStringCellValue();
                        String trimOptionValue=optionValue.replaceAll("\\s+","").replaceAll(",", " ");
                        //4 Print option and create query
                        System.out.println(trimOptionValue);

                    // Step 6: Close the workbook
                    wbook.close();              
    }
}
}


[Current output is][1]


[Expected output][2]


  [1]: https://i.stack.imgur.com/kjS6y.png
  [2]: https://i.stack.imgur.com/YMQrF.png

当前输出

001 AFD PB_P PB_DL EGS_D1 EGS_D2 PC01 PC02 PC04M WREC

10A AFD PB_P PB_DL EGS_D1 EGS_D2 PC01 PC02 PC03 PC04 PC01M PC02M

预期输出

001 AFD

001 PB_P

001 PB_DL

001 EGS_D1

001 EGS_D2

10A AFD

10A PB_P

10A PB_DL

10A EGS_D1

10A EGS_D2

0 个答案:

没有答案