生成csv文件的工具类(CSVWriter使用示例)详解编程语言

生成csv文件的工具类(CSVWriter使用示例)

 //按照省份poi类型保存csv 
    private static final String GD_FILE_BASE = "D://amap//gaode//"; 
    //按照省份poi类型保存的图片路径csv 
    private static final String GD_FILE_BASE_IMG = "D://amap//gaode//imgs//"; 
     
    /** 
     * 按照省份poi类型生成csv文件 
     * @author gaozhen 
     * @time 2018年1月11日上午9:31:16 
     * @param arr 
     * @param city 
     * @throws Exception 
     */ 
    public static void writeAmapCSV(JSONArray arr, String area,String poiTypeName,String poiType) throws Exception { 
    	 
    	//高德poi数据命名 
        File infoFile = new File(GD_FILE_BASE + "amap_" + area +"_"+poiTypeName+ ".csv"); 
        File imgFile = new File(GD_FILE_BASE_IMG + "amap_img_" + area +"_"+poiTypeName+ ".csv"); 
         
        boolean infoFlag = infoFile.exists(); 
        boolean imgFlag = imgFile.exists(); 
 
        // 创建CSV写对象 例如:CsvWriter(文件路径,分隔符,编码格式); 
        CSVWriter infoWriter = new CSVWriter(new FileWriter(infoFile)); 
        // 创建CSV写对象 例如:CsvWriter(文件路径,分隔符,编码格式); 
        CSVWriter imgWriter = new CSVWriter(new FileWriter(imgFile)); 
         
        // 写表头 
        if (!infoFlag) { 
            String[] InfoHeaders = { "POI_NAME", "X_AXIS", "Y_AXIS", "POI_STATE", "POI_DESC", "POI_ADDRESS", 
                    "THUMBNAIL", "AREA", "CITY", "POI_TYPE", "SALES_VOLUME", "POI_LABELS", "POI_ATTRIBUTES", "ASCRIPTION","TN_POIID"}; 
            infoWriter.writeNext(InfoHeaders); 
        } 
        if (!imgFlag) { 
            String[] imgHeaders = { "IMG_NAME", "URL"}; 
            imgWriter.writeNext(imgHeaders); 
        } 
         
		for (int i=0;i<arr.size();i++) { 
			JSONObject poiJson = arr.getJSONObject(i); 
			 
			//处理坐标 
			String location = poiJson.getString("location"); 
			String[] locations = location.split(","); 
			 
			//处理空地址 
			String address=poiJson.getString("address"); 
			if("[]".equals(address)){ 
				address=""; 
			} 
			 
			//处理图片 
			JSONArray photos = poiJson.getJSONArray("photos"); 
			String thumbnail=""; 
			if(photos.size()>0){ 
				thumbnail=photos.getJSONObject(0).getString("url"); 
				 
				//写入图片的csv 
				for(int j=0;j<photos.size();j++){ 
					String[] imgContent={ 
							poiJson.getString("id"), 
							photos.getJSONObject(j).getString("url") 
					}; 
					imgWriter.writeNext(imgContent); 
				}; 
			} 
			 
			//拼接内容 
			String[] infoContent={ 
					 poiJson.getString("name"),  //POI_NAME 
					 locations[0],               //X_AXIS 
					 locations[1],               //Y_AXIS 
					 "1",                        //POI_STATE 
					 "",                         //POI_DESC 
					 address,                    //POI_ADDRESS 
					 thumbnail,                  //THUMBNAIL 
					 poiJson.getString("pname"), //AREA 
					 poiJson.getString("cityname"),//CITY 
					 poiType,                     //POI_TYPE 
					 "",                          //SALES_VOLUME 
					 poiJson.getString("type"),  //POI_LABELS 
					 "",                         //POI_ATTRIBUTES 
					 "4",                        //ASCRIPTION 
					 poiJson.getString("id")    //TN_POIID(高德的poiId) 
			 }; 
			 infoWriter.writeNext(infoContent); 
		} 
        infoWriter.close(); 
        imgWriter.close(); 
    }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13893.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论