在 Java 中表示 Excel 工作表值的好方法 [英] good way to represent a excel sheet value in Java

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

问题描述

考虑一下我有以下格式的 Excel 表:

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

现在我想读取这些值(使用 POI HSSF)并且必须处理它们.最好的方法是什么?

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

请注意,我的应用程序中没有对象人员,因为 excel 表中可能出现的值是任意的(即它可能不是人名和年龄).所以,我需要使用某种 HashMap 来存储这些值.如果多行,有一个 List 好吗!?

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

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

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