好办法,重新present Java中的Excel工作表中的值 [英] good way to represent a excel sheet value in Java

查看:161
本文介绍了好办法,重新present Java中的Excel工作表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想想看,我有一个Excel工作表中以下格式:


年龄

29
酒吧
27

现在我想读这些值(使用POI HSSF),并有对其进行处理。什么是做到这一点的最好办法?

请注意,我没有对象者在我的应用程序,becasue这可能会在Excel工作表中的值是任意的(即它可能不是人姓名和年龄)。所以,我需要有点用一些HashMap中来存储这些值。如果多行,是好有一个列表!?


解决方案

 公共类网{
    私人行headerColumns;
    私人列表<行>数据行;    公共电网(){
    数据行=新的LinkedList<行>();
    }    公共电网(INT rowCount等){
    数据行=新的ArrayList<行>(rowCount等);
    }    公共无效addHeaderRow(列表<串GT;头){
    this.headerColumns =新行(头);
    }    公共无效addDataRow(列表<串GT;数据){
    this.dataRows.add(新行(数据));
    }    公开名单<行> getAllData(){
    清单<行>数据=新的ArrayList<行>(1 + dataRows.size());
    data.add(this.headerColumns);
    data.addAll(数据行);
    返回的数据;
    }    公共行getHeaderColumns(){
    返回headerColumns;
    }    公开名单<行> getDataRows(){
    返回数据行;
    }
}类行{
    私人列表<串GT;数据;    公共行(列表<串GT;数据){
    this.data =数据;
    }    公共无效addColumn(字符串columnData){
    data.add(columnData);
    }    公开名单<串GT;的getData(){
    返回的数据;
    }
}

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 !?

解决方案

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;
    }
}

这篇关于好办法,重新present Java中的Excel工作表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆