views:

448

answers:

3

Consider that I've a excel sheet in below format:

person age
Foo 29
Bar 27

Now I want to read these values (using POI HSSF) and have to process them. What's the best way to do that?

Note that I do not have a Object Person in my application, becasue the values that may come in excel sheet is arbitrary (i.e. it may not be the person name and age). So, I need to use some kinda HashMap to store these values. In case multiple rows, is it good to have a List !?

A: 

If the format is defined, make a class that accomodates all those fields.

If the format isn't defined, pass Row-s around, or Lists, or even DOM from excel-to-dom transformation. You have no choice. I'd recommend just stick to POI's native Row and Cell objects.

alamar
A: 

Yes, you cannot use map if you have multiple key values. And i didn't find some build-in class for this issue. You can try write some kind of wrapper. If you don't care of speed use simple 2D array like this:

String[][] filter = new String[initial width][initial height];

it can be Object instead of String;

Vanger
+2  A: 
public class Grid {
    private Row headerColumns;
    private List<Row> dataRows;

    public Grid() {
     dataRows = new LinkedList<Row>();
    }

    public Grid(int rowCount) {
     dataRows = new ArrayList<Row>(rowCount);
    }

    public void addHeaderRow(List<String> headers) {
     this.headerColumns = new Row(headers);
    }

    public void addDataRow(List<String> data) {
     this.dataRows.add( new Row(data) );
    }

    public List<Row> getAllData() {
     List<Row> data = new ArrayList<Row>(1+dataRows.size());
     data.add(this.headerColumns);
     data.addAll(dataRows);
     return data;
    }

    public Row getHeaderColumns() {
     return headerColumns;
    }

    public List<Row> getDataRows() {
     return dataRows;
    }
}

class Row {
    private List<String> data;

    public Row(List<String> data) {
     this.data = data;
    }

    public void addColumn(String columnData) {
     data.add(columnData);
    }

    public List<String> getData() {
     return data;
    }
}
eqbridges
thank you for such a detailed solution. :)
Veera