How to take a values from excel matching specific condition and assign it to an array?

StackOverflow https://stackoverflow.com/questions/23440960

  •  14-07-2023
  •  | 
  •  

Pregunta

I have an excel in following format

Category     Name                 Title
Map          Map1                 Title Automaition Map1
Topic        Topic1               Title Automaition Topic1
Topic        Topic2               Title Automaition Topic2
Submap       Submap1              Title Automaition Submap1
Sub Topic    SubTop1              Title Automaition SubTopic1

I want to take the values from Title column for all the Categories as Topic and store in an array. I have written the following code for taking the values from Title column for Categories as Topic

public class DitaExcel {

    @Test
    public void Test() throws IOException {
    {
  FileInputStream file = new FileInputStream(new File("C://DitaExchange.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);


            for (Row row : sheet) {
                for (Cell cell : row) {
                    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());

                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:


                            if (cell.getRichStringCellValue().getString().equalsIgnoreCase("Topic"))
                            {
                                System.out.println(row.getCell(2));

                                break;
                            }
                        break;



                    }}}}}}

I am getting the value as Title Automaition Topic1 Title Automaition Topic2

But I want to capture these values inside an array. How can I capture these values inside an array. I am expecting my output to be like this

{Title Automaition Topic1, Title Automaition Topic2}

Can someone please help ?

Thanks

¿Fue útil?

Solución

Change your code like this...

    List list = new ArrayList();

code..
....
...



    if (cell.getRichStringCellValue().getString().equalsIgnoreCase("Topic"))
     {
         list.add(row.getCell(2));

         break;
     }

...
...

    System.out.print("\t" + list);

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top