导出文件csv格式

2019-07-14 12:57发布

public class JsonToExcel { // 服务器文件路径 public static String saveUrl ="D:\hnqp\GMManager\excal\"; /** * 导出玩家基本信息 * @param data * @param name */ public static void excelToLoad_player(JSONArray data,String name) { String textName =name+".csv"; FileWriter fw = null; try { fw = new FileWriter(saveUrl+textName); fw.write("玩家id"); fw.write(","); fw.write("openId"); fw.write(","); fw.write("昵称"); fw.write(","); fw.write("平台"); fw.write(","); fw.write("注册时间"); fw.write(","); fw.write("是否禁用"); fw.write(","); fw.write("是否机器人"); fw.write(" "); fw.flush(); Iterator it = data.iterator(); while (it.hasNext()) { JSONObject ob = (JSONObject) it.next(); if (ob.getString("playerId")!=null && ob.getString("playerId")!="" && !"".equals(ob.getString("playerId"))) { fw.write(ob.getString("playerId")); } else { fw.write("null"); } fw.write(","); if (ob.getString("openId")!=null && ob.getString("openId")!="" &&!"".equals(ob.getString("openId"))) { fw.write(ob.getString("openId")); } else { fw.write("null"); } fw.write(","); if (ob.getString("name")!=null && ob.getString("name")!="" && !"".equals(ob.getString("name"))) { fw.write(ob.getString("name")); } else { fw.write("null"); } fw.write(","); if (ob.getString("phoneOS")!=null && ob.getString("phoneOS")!="" && !"".equals(ob.getString("phoneOS"))) { fw.write(ob.getString("phoneOS")); } else { fw.write("null"); } fw.write(","); if (ob.getString("createTime")!=null && ob.getString("createTime")!="" && !"".equals(ob.getString("createTime"))) { fw.write(ob.getString("createTime")); } else { fw.write("null"); } fw.write(","); if (ob.getString("forbidden")!=null && ob.getString("forbidden")!="" && !"".equals(ob.getString("forbidden"))) { fw.write(ob.getString("forbidden")); } else { fw.write("null"); } fw.write(","); if (ob.getString("iRobotFlag")!=null && ob.getString("iRobotFlag")!="" && !"".equals(ob.getString("iRobotFlag"))) { fw.write(ob.getString("iRobotFlag")); } else { fw.write("null"); } fw.write(","); fw.write(" "); fw.flush(); } } catch (Exception e) { e.printStackTrace(); } } }