读远程Excel文件

时间:2015-10-15 09:45:07

标签: java excel csv

我有一个如下所示的Excel文件:

enter image description here

我现在要做的是提供DEST_NAME_DISPLAY并检索DEST_ID。

您可能已经注意到,此文件包含大量目标,因此我不想将它们全部循环以查找DEST_ID。这可能吗?如果是,怎么样?

2 个答案:

答案 0 :(得分:1)

由于文件很少更改,您可以将其读入地图。

例如:How to group the values which are in excel to a HashMap

之后,您可以获得如下值:

Map<String, Integer> map = ... // from file
Integer value = map.get(key);

编辑:BackSlash指出,数据库更好,因为数据将在应用程序退出后保留。当然,设置起来有点困难,但值得考虑的是你是否需要坚持。

答案 1 :(得分:1)

在java8中,您可以使用函数式编程功能来完成它。以下是用javascript编写的伪代码。

var myColumnDefs = [
    {key:"Tokyo", value:"2211"},
    {key:"Akihabara", value:"2211"},
    {key:"Kyoto",value:"3344"}]
var input = "Kyoto"
var p = myColumnDefs.filter(function(x){ return x.key==input }).map(function(x){return x.value});
alert(p);

以下是用Java 8编写的较长版本

import java.util.Arrays;
class Location{
    public String areaCode;
    public String areaName;
    public String otherInfo;

    public Location(String areaName,String areaCode,String otherInfo){
        this.areaCode = areaCode;
        this.areaName = areaName;
        this.otherInfo = otherInfo;
    }

}
public class Main {

  public static void main(String[] args) {
    Location[] locations = {
        new Location("Tokyo","2211","TK"),
        new Location("Akihabara","2211","AHR"),
        new Location("Kyoto","3344","KT")
    };
    String input = "Tokyo";
    Location[] output =(Location[]) Arrays.stream(locations).filter(x->x.areaName.equals(input)).toArray(size -> new Location[size]);

    System.out.println(output[0].areaCode);


  }
}
相关问题