简体   繁体   中英

How to convert Excel sheet into a Vector using Jxl

So I have this variable PV1 who stores from an Excel file the first row. But I want a variable that stores the entire row in a vector.

I use System.out.println() just to see if it takes the good column.

String PV1;
for(int col = 0;col < columns;col++) 
 {
   for(int row = 1;row < rows;row++) 
    {
      PV1 = sheet.getCell(1, row).getContents();
      System.out.println(Double.parseDouble(PV1)); 
    }  
 }

I am using jxl to access the Excel file.

Any help would be much appreciated!

Edit: This is the table and i need to store in PV1 all the rows.

This will help you

    String PV1;
    Object[] data = Object[columns]
    for(int col = 0;col < columns;col++) 
     {
       for(int row = 1;row < rows;row++) 
      {
         PV1 = sheet.getCell(1, row).getContents();
         data[col] = PV1;
         System.out.println(Double.parseDouble(PV1)); 
      }  
     }

If I understand correctly you need a vector that should hold all the columns in the first row.

If that is the case, you can do something like this:

Vector<Double> firstRow = new Vector<>();
if(rows > 0){
    for(int col = 0;col < columns;col++){
        String pv = sheet.getCell(1, col).getContents();
        firstRow.add(Double.parseDouble(pv)); 
    } 
}

You should probably consider using a List instead of a Vector .

From your comment it seems that you want to retrieve a specific column from all the rows. You can do that like this:

int pv1ColumnIndex = 1;
List<Double> pv1Columns = new ArrayList<>();
for(int row = 1;row < rows;row++){// row = 1 to skip the header
    String pv1 = sheet.getCell(pv1ColumnIndex, row).getContents();
    pv1Columns.add(Double.parseDouble(pv1)); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM