日期:2014-05-20  浏览次数:20777 次

怎样使用java对比两份execl文件并读取其中的不同?
我现在有两份数据,execl格式的。
其中一份是完整的文件A,一份是少了一些数据的文件B(这份文件比较新)。
A中有所有完整的字段,
如:
编号,姓名,班级,身高,年龄。
B中的属性只有
编号,姓名,班级,身高。
我现在想读取A文件,然后存到内存中,遍历B中数据,在B中把编号相同的人的年龄给添加上。应该如何操作呢?
求完整的代码。
我想利用这份代码来学习一下execl表的读取,序列的遍历等内容。。
求高手帮忙。

------解决方案--------------------

public static void main(String[] args) throws Exception {
String str="";
String result="";
Map map=returnMap("D:\\old.csv");
FileReader fr=new FileReader("D:\\new.csv");
BufferedReader br=new BufferedReader(fr);
while((str=br.readLine())!=null){
String s[]=str.split(",");
if(map.get(s[4])!=null){

}
result=result+str+"\n";
}

write(result);


}

public static Map returnMap(String file) throws Exception{
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String str="";
Map<String,String> map=new HashMap<String, String>();
while((str=br.readLine())!=null){
String s[]=str.split(",");
String sku=s[4];
String carrier=s[16];
map.put(sku, carrier);
}
return map;
}
public static void write(String result) throws Exception{
FileWriter fw=new FileWriter("D:\\new.csv");
PrintWriter pw=new PrintWriter(fw);
pw.write(result);
pw.flush();
}

这是读的txt文档或者csv文件的,读execl的还没有改出来。。
等会得出去,回来看看再弄吧。
------解决方案--------------------
上面那份没改完。刚才一看不对,改了一下。
出去了。回来再弄。。。

public class Test1 {
public static void main(String[] args) throws Exception {
String str="";
String result="";
Map map=returnMap("D:\\old.csv");
//读取一份新的文件,判断每一行
FileReader fr=new FileReader("D:\\new.csv");
BufferedReader br=new BufferedReader(fr);
while((str=br.readLine())!=null){
String s[]=str.split(",");
if(map.get(s[4])!=null){
result=result+str+map.get(s[4])+"\n";
}
}

write(result);
}
//读取一份原版文件,然后把其中的sku和carrier制作成一个map。并返回
public static Map returnMap(String file) throws Exception{
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String str="";
Map<String,String> map=new HashMap<String, String>();
while((str=br.readLine())!=null){
String s[]=str.split(",");
String sku=s[4];
String carrier=s[16];
map.put(sku, carrier);
}
return map;
}

public static void write(String result) throws Exception{
FileWriter fw=new FileWriter("D:\\test.csv");
PrintWriter pw=new PrintWriter(fw);
pw.write(result);
pw.flush();
}
}