package com.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import net.sf.json.JSONObject;
/**
* @ClassName: EntCoordSyncJob
* @Description: TODO(这里用一句话描述这个类的作用)
*
*/
public class EntCoordSyncJob {
static String AK = "ZaDxCDzTjeoKNflCy0qUxlkmm2WoRmnS"; // 百度地图密钥如果次数没了 请自己注册个
public static void main(String[] args) {
ArrayList<String> columnList = new ArrayList<String>();
File file = new File("F:\\test.xls");
try {
FileInputStream in = new FileInputStream(file);
//HSSFWorkbook wb = new HSSFWorkbook(in); //xls读取
XSSFWorkbook wb = new XSSFWorkbook(in); //xlsx读取
Sheet sheet = wb.getSheetAt(0);//Excel 数量
int firstRowNum = sheet.getFirstRowNum();//初始行 0
int lastRowNum = sheet.getLastRowNum(); //总数行
Row row = null; //行
Cell cell_a = null; //列
String [] sum;
FileOutputStream out=new FileOutputStream(file);
for (int i = 1; i <= lastRowNum; i++) {
row = sheet.getRow(i); //取得第i行
cell_a = row.getCell(7); //取得i行的第一列
String cellValue = cell_a.getStringCellValue().trim();
//System.out.println(cellValue);
columnList.add(cellValue);
String dom = "杭州市"+cellValue;
String coordinate = getCoordinate(dom);
System.out.println("'" + dom + "'的经纬度为:" + coordinate);
String a;
String b;
if(coordinate==null){
a="0000000000";
b="0000000000";
}else{
sum = coordinate.split(",");
a=sum[0];
b=sum[1];
}
row=sheet.createRow((short)(i));
row.createCell(4).setCellValue(a);
row.createCell(5).setCellValue(b);
}
out.flush();
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//String dom = "万景村";
//String coordinate = getCoordinate(dom);
//System.out.println("'" + dom + "'的经纬度为:" + coordinate);
// System.err.println("######同步坐标已达到日配额6000限制,请明天再试!#####");
}
// 调用百度地图API根据地址,获取坐标
public static String getCoordinate(String address) {
if (address != null && !"".equals(address)) {
address = address.replaceAll("\\s*", "").replace("#", "栋");
String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + AK;
String json = loadJSON(url);
if (json != null && !"".equals(json)) {
JSONObject obj = JSONObject.fromObject(json);
if ("0".equals(obj.getString("status"))) {
double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); // 经度
double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat"); // 纬度
DecimalFormat df = new DecimalFormat("#.######");
return df.format(lng) + "," + df.format(lat);
}
}
}
return null;
}
public static String loadJSON(String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {} catch (IOException e) {}
return json.toString();
}
// 来自stackoverflow的MD5计算方法,调用了MessageDigest库函数,并把byte数组结果转换成16进制
/*
* public String MD5(String md5) { try { java.security.MessageDigest md = java.security.MessageDigest .getInstance("MD5"); byte[] array = md.digest(md5.getBytes()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; ++i) { sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100) .substring(1, 3)); } return sb.toString(); } catch (java.security.NoSuchAlgorithmException e) {
* } return null; }
*/
}