前段时间我哥备份手机通讯录备份的是xls表格格式,然后如今导入不进去了,用各种软件都是错误。
我导出为vcard的(后缀是vcf),以文本格式打开看了下。发现vcard是非常有格式规范的。顿时认为有戏了。能够尝试将excel表格转换成vcard格式。然后导入。
BEGIN:VCARD
N:;移动客服;;;;;;
TEL;CELL:10086
STARRED:False
UID:514
VERSION:3.0
REV:20140602
END:VCARD
非常明显的格式,第二行就是姓名。tel那一行就是号码。
而保存的那个表格格式例如以下:
中国移动 | 10086 | |
中国联通 | 10010 |
这种话,事实上就非常easy了。仅仅要把vcf格式里面的姓名和号码填充一下就能够了,而那个UID我则直接给的是循环数。
大体思路有了,再给实现。
1.读取xls,直接用的是非常老的jxl包。如今貌似已经不更新了。而一開始用这个操作表格的时候,提示Unable to recognize OLE stream。度娘告诉我,这是由于:
你是用什么2007 ? jxl眼下停止更新了。不能读写 excel2007 假设你要读写就用 apache POI ,几乎相同的使用方法支持 2003、2007 网上有非常多实例。无语,然后直接用wps打开xls。另存为老版本号的xls。ok。
2。jxl的语法,
參考这篇
3,buffer写文件
这部分能够參看网络上非常多东西。
jxl.jar的百度网盘连接:戳我下载
直接给出代码吧,
类名:Oper.java
package core;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Oper {
void test(){
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter(new File("res/test.txt")));
out.write("hello"+"\r\n"+"hello");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void work(){
try {
Workbook book=Workbook.getWorkbook(new File("res/3.xls"));
BufferedWriter out=new BufferedWriter(new FileWriter(new File("res/out.vcf")));
//get the first sheet
Sheet sheet=book.getSheet(0);
for(int i=0;i<231;i++){
Cell cell1=sheet.getCell(0,i);
String name=cell1.getContents();
Cell cell2=sheet.getCell(1,i);
String phone1=cell2.getContents();
Cell cell3=sheet.getCell(2,i);
String phone2=cell3.getContents();
String phone;
if(phone1.equals(""))phone=phone2;else phone=phone1;
out.write("BEGIN:VCARD\r\n"+"N:;"+name+";;;;;;\r\n"+"TEL;CELL:"+phone+"\r\nUID:"+i+"\r\n"+"VERSION:3.0\r\nREV:20140602\r\nEND:VCARD\r\n");
out.write(name);
}
out.close();
book.close();
} catch (BiffException | IOException e) {
System.out.println("not find");
e.printStackTrace();
}
}
public static void main(String[] args) {
new Oper().work();
}
}
我已经用wps查看过了。有231行数据,所以循环了231次。
读入的文件在project文件夹下/res/3.xls,输出是/res/out.vcf.然后,通过各种手机助手就行导入文件夹了。