/** * 生成csv文件 * @param data 要写入文件的内容 * @param path 文件要保存的路径 */ public static void CreateCSV(ArrayList<ArrayList<String>> data, String path) { BufferedWriter out = null; long start = System.currentTimeMillis(); try { out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"GBK")); //表头信息写入 ArrayList<String> titleHeader = new ArrayList<>(Arrays.asList("会员号", "会员昵称", "推客等级","直推","引导消费者","消费额", "推广订单额", "佣金")); for(int i = 0; i < titleHeader.size(); i++){ out.write(titleHeader.get(i)); out.write(","); } out.newLine(); //换行后写入文件内容 for (int i = 0; i < data.size(); i++) { ArrayList<String> onerow = data.get(i); for (int j = 0; j < onerow.size(); j++) { out.write(onerow.get(j)); out.write(","); } out.newLine(); } out.flush(); long end = System.currentTimeMillis(); logger.info("报表生成完毕,用时: " + (end - start) + " ms"); } catch (Exception e) { logger.error("写文件异常"); logger.error(e.getMessage(), e); }finally { try { if(out != null){ out.close(); } } catch (IOException e) { logger.error("关闭数据写出流异常"); logger.error(e.getMessage(), e); } } } public static String DelQuota(String str) { String result = str; String[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?" }; for (int i = 0; i < strQuota.length; i++) { if (result.indexOf(strQuota[i]) > -1) result = result.replace(strQuota[i], ""); } return result; }
csv文件生成
最新推荐文章于 2024-08-06 10:22:43 发布