代码:
//读取Excel文件路径
JXLReadUtil jxlTool = JXLReadUtil.getInstance("D:/test/demo.xls");
Sheet sheet = jxlTool.getSheet(0);
int rows = sheet.getRows();
System.out.println("rows="+rows);
List<String[]> list = new ArrayList<String[]>();
for (int j = 0; j < rows; j++){
list.add(jxlTool.getContentsViaRow(sheet, j));
}
if (list.size() > 0){
List<Map<String, Object>> listTwo = new ArrayList<Map<String, Object>>();
//读取excel行号,下标从0开始
for (int j = 3; j < list.size(); j++){
Map<String, Object> map = new HashMap<String, Object>();
String[] names = list.get(j);
System.out.println("length="+names.length);
if(names.length==5){
map.put("key", names[3].trim());
map.put("value", names[4].trim());
listTwo.add(map);
}
}
File of = new File("D:\\test\\demo.properties"); //生成的properties文件
FileOutputStream os = new FileOutputStream(of, true);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os,512));
System.out.println("data="+listTwo);
for (int i = 0; i < listTwo.size(); i++){
String loginName = listTwo.get(i).get("key").toString();
String name = listTwo.get(i).get("value").toString();
dos.write(loginName.getBytes("utf-8"));//utf-8这样不会乱码,中文的还是用gb2312吧,utf-8最后展示在myeclipse里还是乱码了
dos.write('=');
dos.write(name.getBytes("utf-8"));
dos.write("\r\n".getBytes());
dos.flush();
}
}else {
System.out.println("no data");
}
运行效果: