Mike3355
Mike3355

Reputation: 12081

How to read values line by line from Excel file, and place them into a Map?

My goal is to read an excel document with know format, read each cell and column one at a time. Each row will be set into a bean called Item which will be put into a map with its key as it item number. Once I do this I can continue with the rest of the program and put into a database. I currently a whole column at a time and trying to figure out how to do this.

public class Excel {

    private Items items;

    Excel(Items items) {
        this.items = items;
    }

    public static void main(String[] args) {

        JFileChooser fileChooser = new JFileChooser();
        int returnVlue = fileChooser.showOpenDialog(null);

        Map<Long, Items> map1 = new HashMap<Long, Items>();

        String mat, thick, size, lbsPerSheet, lbs;

        if (returnVlue == JFileChooser.APPROVE_OPTION) {

            try {
                Workbook workbook = new HSSFWorkbook(new FileInputStream(
                        fileChooser.getSelectedFile()));
                Sheet sheet = workbook.getSheetAt(0);

                for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {

                    Row row = rit.next();

                    for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
                        Cell cell = cit.next();
                        cell.setCellType(Cell.CELL_TYPE_STRING);

                        mat = cell.getStringCellValue();
                        thick = cell.getStringCellValue();
                        size = cell.getStringCellValue();
                        lbsPerSheet = cell.getStringCellValue();
                        lbs = cell.getStringCellValue();

                        System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\t");
                    }
                    System.out.println();

                }

            } catch (Exception e) {
                // TODO: handle exception
            }

        }

    }
}

output

Mat 11960120 Thick 11960120 Size 11960120 lbs Per Sheet 11960120 lbs 11960120   
Mat 0.119 Thick 0.119 Size 0.119 lbs Per Sheet 0.119 lbs 0.119  
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 250 Thick 250 Size 250 lbs Per Sheet 250 lbs 250    
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 18860120 Thick 18860120 Size 18860120 lbs Per Sheet 18860120 lbs 18860120   
Mat 0.188 Thick 0.188 Size 0.188 lbs Per Sheet 0.188 lbs 0.188  
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 383 Thick 383 Size 383 lbs Per Sheet 383 lbs 383    
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 25060120 Thick 25060120 Size 25060120 lbs Per Sheet 25060120 lbs 25060120   
Mat 0.25 Thick 0.25 Size 0.25 lbs Per Sheet 0.25 lbs 0.25   
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 510 Thick 510 Size 510 lbs Per Sheet 510 lbs 510    
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 25072144 Thick 25072144 Size 25072144 lbs Per Sheet 25072144 lbs 25072144   
Mat 0.25 Thick 0.25 Size 0.25 lbs Per Sheet 0.25 lbs 0.25   
Mat 72x144 Thick 72x144 Size 72x144 lbs Per Sheet 72x144 lbs 72x144 
Mat 734.4 Thick 734.4 Size 734.4 lbs Per Sheet 734.4 lbs 734.4  
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 31360120 Thick 31360120 Size 31360120 lbs Per Sheet 31360120 lbs 31360120   
Mat 0.313 Thick 0.313 Size 0.313 lbs Per Sheet 0.313 lbs 0.313  
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 633.2616 Thick 633.2616 Size 633.2616 lbs Per Sheet 633.2616 lbs 633.2616   
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 37560120 Thick 37560120 Size 37560120 lbs Per Sheet 37560120 lbs 37560120   
Mat 0.375 Thick 0.375 Size 0.375 lbs Per Sheet 0.375 lbs 0.375  
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 758.7 Thick 758.7 Size 758.7 lbs Per Sheet 758.7 lbs 758.7  
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 50060120 Thick 50060120 Size 50060120 lbs Per Sheet 50060120 lbs 50060120   
Mat 0.5 Thick 0.5 Size 0.5 lbs Per Sheet 0.5 lbs 0.5    
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 1011.6 Thick 1011.6 Size 1011.6 lbs Per Sheet 1011.6 lbs 1011.6 
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 75060120 Thick 75060120 Size 75060120 lbs Per Sheet 75060120 lbs 75060120   
Mat 0.75 Thick 0.75 Size 0.75 lbs Per Sheet 0.75 lbs 0.75   
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 1517.4 Thick 1517.4 Size 1517.4 lbs Per Sheet 1517.4 lbs 1517.4 
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 10060120 Thick 10060120 Size 10060120 lbs Per Sheet 10060120 lbs 10060120   
Mat 1 Thick 1 Size 1 lbs Per Sheet 1 lbs 1  
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120 
Mat 2023.2 Thick 2023.2 Size 2023.2 lbs Per Sheet 2023.2 lbs 2023.2 
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41   

Excel File

11960120        0.119   60x120  250.00   $0.4100 
18860120        0.188   60x120  383.00   $0.4100 
25060120        0.250   60x120  510.00   $0.4100 
25072144        0.250   72x144  734.40   $0.4100 
31360120        0.313   60x120  633.26   $0.4100 
37560120        0.375   60x120  758.70   $0.4100 
50060120        0.500   60x120  1011.60  $0.4100 
75060120        0.750   60x120  1517.40  $0.4100 
10060120        1.000   60x120  2023.20  $0.4100 

I would like to read the above excel file much like a two dimensional array. For example:

array[0][0] = 11960120; array[0][1] = 0.119; array[0][2]= "60x120";

If I could just read the row and columns like the above use getter and setters from the Items class then simply place the "Item" into a map and wrap it with the item number, for example 11960120 is in cell [0][0] in the above excel example.

To be more clear I want to read the excel file like this

11960120    0.119   60x120  250.00   $0.4100 

then read the next line

18860120    0.188   60x120  383.00   $0.4100

Each cell will be set into the Items class.

Upvotes: 0

Views: 8969

Answers (2)

djangofan
djangofan

Reputation: 29669

You could use the Apache Metamodel library to query the Excel spreadsheet. The method I show below preserves column names so you could write a method to get values by column name and row number. If you don't want/have column names, this code would have to be adjusted:

private static org.slf4j.Logger logger = LoggerFactory.getLogger( "Data" );

public static Object[][] getExcelData( File excelFile, String sheetName ) 
{   
    ExcelConfiguration conf = new ExcelConfiguration( 1, true, false );
    DataContext dataContext = DataContextFactory.createExcelDataContext( excelFile, conf );
    DataSet dataSet = dataContext.query()
            .from( sheetName )
            .selectAll()
            .where("run").eq("Y")
            .execute();
    List<Row> rows = dataSet.toRows();
    Object[][] myArray = get2ArgArrayFromRows( rows );
    return myArray;
}

/**
 * Gets a 2D Object array from a List of Row objects that is only 2 args wide.
 * @param rows
 * @return
 */
public static Object[][] get2ArgArrayFromRows( List<Row> rows ) {
    Object[][] myArray = new Object[rows.size()][2];
    int i = 0;
    SelectItem[] cols = rows.get(0).getSelectItems();
    for ( Row r : rows ) {
        Object[] data = r.getValues();
        for ( int j = 0; j < cols.length; j++ ) {
            if ( data[j] == null ) data[j] = ""; // force empty string where there are NULL values
        }
        myArray[i][0] = cols;
        myArray[i][2] = data;
        i++;
    }
    logger.info( "Row count: " + rows.size() );
    logger.info( "Column names: " + Arrays.toString( cols ) );
    return myArray;
}

Then, if you want to, use can use Google Guava to convert a 2-D array into a List. (I think converting to a Map would be problematic if there were dupes?):

Double[][] array;
List<List<Double>> list = Lists.transform(Arrays.asList(array),
  new Function<Double[], List<Double>>() {
    @Override public List<Double> apply(Double[] row) {
      return Arrays.asList(row);
    }
  }
}

Upvotes: 1

RealSkeptic
RealSkeptic

Reputation: 34608

Your current loop:

          for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {

                Row row = rit.next();

                for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
                    Cell cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);

                    mat = cell.getStringCellValue();
                    thick = cell.getStringCellValue();
                    size = cell.getStringCellValue();
                    lbsPerSheet = cell.getStringCellValue();
                    lbs = cell.getStringCellValue();

                    System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\t");
                }
                System.out.println();

            }

...is basically saying: "For each row and each cell in this sheet, put the value of that particular cell into these five different variables". The cell is the current cell, and getting its string value and putting it in all of your variables doesn't actually make sense. You probably wanted the first cell to go to mat, the second to go to thick and so on. You can do either of these two things:

  1. Assign to the values without looping:

            for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
    
                Row row = rit.next();
    
                Iterator<Cell> cit = row.cellIterator()
                Cell cell;
    
                if ( cit.hasNext() ) {
                    cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    mat = cell.getStringCellValue();
                }
                if ( cit.hasNext() ) {
                    cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    thick = cell.getStringCellValue();
                }
                if ( cit.hasNext() ) {
                    cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    size = cell.getStringCellValue();
                }
                if ( cit.hasNext() ) {
                    cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    lbsPerSheet = cell.getStringCellValue();
                }
                if ( cit.hasNext() ) {
                    cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    lbs = cell.getStringCellValue();
                }
    
                System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\n");
    
            }
    
  2. Use an array and loop:

          for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
    
                Row row = rit.next();
                String[] cells = new String[row.getPhysicalNumberOfCells()];
    
                int i = 0;
                for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
                    Cell cell = cit.next();
                    cell.setCellType(Cell.CELL_TYPE_STRING);
    
                    cells[i++] = cell.getStringCellValue();
    
    
                }
    
                // At this point you can put the values of the cells in
                // your map entry.
    
                System.out.println(Arrays.toString(cells);
    
            }
    

Upvotes: 1

Related Questions