简体   繁体   English

读取远程Excel文件

[英]Read remote Excel file

I have an Excel file that looks like this: 我有一个如下所示的Excel文件:

在此处输入图片说明

What I'm trying to do now is supply the DEST_NAME_DISPLAY and retrieve the DEST_ID. 我现在想做的是提供DEST_NAME_DISPLAY并检索DEST_ID。

As you might have noticed, this file contains a lot of destination, so I don't want to loop over them all to find a DEST_ID. 您可能已经注意到,该文件包含很多目标,因此我不想遍历所有文件以查找DEST_ID。 Is this possible? 这可能吗? If it is, how? 如果是,怎么办?

Since the file rarely changes you can read it into a Map. 由于文件很少更改,因此您可以将其读入地图。

For example like this: How to group the values which are in excel to a HashMap 例如这样的示例: 如何将excel中的值分组到HashMap中

Afterwards, you can get the value like this: 之后,您可以获取如下值:

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

Edit: BackSlash points out, a database is better because the data will remain after the application quits. 编辑: BackSlash指出,数据库更好,因为在应用程序退出后数据将保留。 Of course it is a little harder to set up, but worth considering if you need persistance. 当然,设置起来有些困难,但是值得考虑是否需要持久性。

In java8 you can use the functional programming features to get it done.Following is the psuedo code written in javascript. 在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);

The following is the longer version which is written in Java 8 以下是用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);


  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM