2015-10-15 154 views
-1

我有一个看起来像这样的Excel文件:远程读取Excel文件

enter image description here

我试图现在要做的就是提供DEST_NAME_DISPLAY和检索DEST_ID。

正如你可能已经注意到的那样,这个文件包含很多目标,所以我不想遍历它们来找到一个DEST_ID。这可能吗?如果是,如何?

+1

更长的版本'所以我不想遍历他们都找到了DEST_ID。这可能吗?“是的。 '如果是这样,怎么样?'通过使用数据库。如果这不是一个选项,我认为循环是你能做的唯一的事情。 – BackSlash

+0

@wero,我纠正了它。当我发布这个问题时,我正在考虑excel文件扩展,所以我把它和xml混淆了。谢谢 –

+0

@BackSlash奥克感谢您的答案。令人遗憾的是,数据库不是一个选项,因为excem文件位于远程FTP服务器上... –

回答

1

由于文件很少更改,因此您可以将其读入到Map中。

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

之后,你可以得到这样的价值:

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

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

+0

我还会补充说一个数据库会更好。一旦应用程序退出,哈希映射将从内存中删除,因此每次都必须重新读取excel文件,除非您每次将映射序列化为文件。如果OP使用db,则该数据将在应用程序退出后保留。 – BackSlash

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


    } 
}