您的位置:首页 > 其它

利用jxl实现excel文件上传解析,并获取地址获得高德经纬度

2018-10-26 10:45 330 查看

文件上传功能一直是我最头疼的地方,写这个博客只是作为记录,以便于自己下次能够找到模板

首先controller层:

[code]public static String url = "https://restapi.amap.com/v3/geocode/geo?key=332c42865845ba43bac0&address=";

@RequestMapping("/social")
@ResponseBody
public void Social(MultipartFile file) {
String result = null;
try {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Workbook book = Workbook.getWorkbook(file.getInputStream());
logger.info("book:" + book);
//  获得excel第一个的sheet文件
Sheet sheet = book.getSheet(0);
//  获取文件的总列数
int rowNum = sheet.getRows();
for (int i = 2; i < rowNum; i++) {
//  将数据从xls文件中解析出来,并存放到map中
Map<String, String> map = new HashMap<String, String>();

Cell cel0 = sheet.getCell(0, i);
//  单位名称
map.put("company", cel0.getContents().trim());

Cell cel1 = sheet.getCell(1, i);
//  应急值班电话
map.put("emergencyTelephone", cel1.getContents().trim());

Cell cel2 = sheet.getCell(2, i);
//  联系人姓名
map.put("name", cel2.getContents().trim());

Cell cel3 = sheet.getCell(3, i);
//  职务
map.put("post", cel3.getContents().trim());
Cell cel4 = sheet.getCell(4, i);
//  联系电话
map.put("phone", cel4.getContents().trim());
Cell cel5 = sheet.getCell(5, i);
//  应急装备
map.put("equipment", cel5.getContents().trim());
Cell cel6 = sheet.getCell(6, i);
//  应急物资
map.put("material", cel6.getContents().trim());
String urll = url + map.get("company");
//开始请求
URL url1 = new URL(urll.toString());
URLConnection open = url1.openConnection();
InputStream input = open.getInputStream();
result = org.apache.commons.io.IOUtils.toString(input, "utf-8");
//  将成功的结果进行解析
JSONObject jsonObject = JSONObject.parseObject(result);
String status = jsonObject.getString("status");
JSONArray geocodes = jsonObject.getJSONArray("geocodes");
String location = null;
String city = null;
if ("1".equals(status)) {
if (geocodes != null && geocodes.size() > 0) {
location = geocodes.getJSONObject(0).getString("location");
city = geocodes.getJSONObject(0).getString("city");
//  将得到的经纬度以","为标示,进行拆分
String[] str = location.split(",");
if (!str[0].equals("0") && !str[1].equals("0")) {
map.put("longitude", str[0]);
map.put("latitude", str[1]);
map.put("city", city);
list.add(map);
}
}
}
}
jointWorkService.insert(list);
} catch (BiffException e) {
logger.info(e.getMessage());
} catch (IOException e) {
logger.info("文件读取异常:" + e);
}
}
public static String toURLDecoded(String paramString) {
if (paramString == null || paramString.equals("")) {
return "";
}

try {
String str = new String(paramString.getBytes(), "UTF-8");
str = URLDecoder.decode(str, "UTF-8");
return str;
} catch (Exception localException) {
}

return "";
}

然后是serviceImpl层:

[code]@Override
public ZykjSocialJointWork insert(List<Map<String, String>> list) {

for(Map<String,String> map : list){
int id = dtAreaService.selectByCity(map.get("city"));
ZykjSocialJointWork jointWork = new ZykjSocialJointWork();
jointWork.setCompany(map.get("company"));
jointWork.setEmergencyTelephone(map.get("emergencyTelephone"));
jointWork.setName(map.get("name"));
jointWork.setPost(map.get("post"));
jointWork.setPhone(map.get("phone"));
jointWork.setEquipment(map.get("equipment"));
jointWork.setMaterial(map.get("material"));
jointWork.setLongitude(map.get("longitude"));
jointWork.setLatitude(map.get("latitude"));
jointWork.setCreateTime(new Date());
jointWork.setAreaId(id);
jointWorkDao.insert(jointWork);
}
return null;
}

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐