Nice way to represent excel sheet value in Java
Please note that I have an Excel sheet below:
<B> person <B> ageFoo 29
Bar 27
Now I want to read these values (using POI HSSF) and process them. What's the best way to do this?
Note that I do not have an Object in my application because the values that may appear in the excel sheet are arbitrary (for example, it might not be the person's name and age). So, I need to use some kind of HashMap to store these values. In the case of multiple lines, is it good to have a list !?
+1
a source to share
3 answers
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;
}
}
+3
a source to share
Yes, you cannot use a map if you have multiple key values. And I haven't found a built-in class for this problem. You can try writing some kind of wrapper. If you don't need speed, use a simple 2D array like this:
String[][] filter = new String[initial width][initial height];
it can be Object instead of String;
0
a source to share