以下内容仅为记录:
第一步:解析csv文件
@Testpublic void testcsv() throws Exception {File csv = new File("C:\\Users\\hjy\\Desktop\\attendace164_20180312.csv"); // CSV文件路径BufferedReader br = null;try{br = new BufferedReader(new FileReader(csv));} catch (FileNotFoundException e){e.printStackTrace();}String line = "";String everyLine = "";try {List<String> allString = new ArrayList<>();while ((line = br.readLine()) != null) //读取到的内容给line变量{everyLine = line;System.out.println(everyLine);allString.add(everyLine);}System.out.println("csv表格中所有行数:"+allString.size());} catch (IOException e){e.printStackTrace();}}
csv文件样式
解析的内容:
第二步:将数据写入csv
@Testpublic void testcsv1(){//自定义数组ArrayList<ArrayList<String>> alldata=new ArrayList<ArrayList<String>>();alldata.add(new ArrayList<String>(Arrays.asList("1","11","111"))); //添加一行alldata.add(new ArrayList<String>(Arrays.asList("2","22","222"))); //添加一行alldata.add(new ArrayList<String>(Arrays.asList("3","33","333"))); //添加一行//保存成csv文件Array2CSV(alldata,"test.csv");
// //读取csv文件
// ArrayList<ArrayList<String>> alldata1=CSV2Array("test.csv");
// //遍历数组
// for (ArrayList<String> arrayList : alldata1) {
// for (String string : arrayList) {
// System.out.println(string);
// }
// }}//导出到csv文件public static void Array2CSV(ArrayList<ArrayList<String>> data, String path){try {BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"UTF-8"));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();out.close();} catch (Exception e) {e.printStackTrace();}}
List<Checkins> list = checkinsQuery.asList();if (CollectionUtils.isNotEmpty(list)) {try {FileWriter fw = new FileWriter("data_"+ea+"_"+date+".csv");String header = "员工姓名,签到时间,签到地点\r\n";fw.write(header);StringBuffer str = new StringBuffer();list.forEach(o -> {String name = o.getOwnerName();String checkTime = DateUtils.getStringFromTime(o.getCheckinsTimeStamp(), DateUtils.DateTimeFormat);String address = o.getCheckinsAddressDesc();str.append(name).append(",").append(checkTime).append(",").append(address).append(",").append("\r\n");});fw.write(str.toString());fw.flush();fw.close();} catch (Exception e) {LOG.error("getCheckinsData error ", e);}}LOG.info("getCheckinsData end ea:{},date:{}", ea, date);
最终写出的结果: