采用commons-csv数据转CSV文件

采用commons-csv数据转CSV文件

  • 导入pom.xml依赖

        
            org.apache.commons
            commons-csv
            1.5
        
    
        
            org.apache.poi
            poi
            3.17
        
    
        
            org.apache.poi
            poi-ooxml
            3.17
        
    
  • 创建工具类CSVUtils

    public class CSVUtils {
    
        //CSV文件分隔符
        private static final String NEW_LINE_SEPARATOR = "\n";
    
        //CSV文件头 可以不用写死
        private static final Object [] FILE_HEADER = {"天气","气温","降水","风速","数据更新时间","对象日期时间"};
    
        /**
         * 写CSV文件
         * @param fileName 文件名
         * @param weather  天气
         */
        public void writeCsvFile(String fileName, Weather weather){
            //获取文件夹名
            String dir = fileName.substring(0,11);
            File dirFile = new File(dir);
            //判断文件是否存在
            if(!dirFile.exists()) {
                dirFile.mkdirs();
            }
            File file=new File(fileName);
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileWriter fileWriter = null;
            CSVPrinter csvFilePrinter = null;
    
            //创建 CSVFormat
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            try{
                //初始化FileWriter
                fileWriter = new FileWriter(fileName);
    
                //初始化 CSVPrinter
                csvFilePrinter = new CSVPrinter(fileWriter,csvFileFormat);
    
                 //创建CSV文件头
                csvFilePrinter.printRecord(FILE_HEADER);
                
                //遍历写进CSV
                List  weatherDataRecord = new ArrayList<>();
                weatherDataRecord.add(weather.getWeather());
                weatherDataRecord.add(weather.getTemp());
                weatherDataRecord.add(weather.getWater());
                weatherDataRecord.add(weather.getWind());
                weatherDataRecord.add(sdf.format(weather.getForcastDate()));
    
                //设置整点时间
                calendar.setTime(weather.getForcastDate());
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                Date time = calendar.getTime();
                weatherDataRecord.add(sdf.format(time));
    
                csvFilePrinter.printRecord(weatherDataRecord);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try{
                    fileWriter.flush();
                    fileWriter.close();
                    csvFilePrinter.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    
    }
    

你可能感兴趣的:(采用commons-csv数据转CSV文件)