java csv处理工具

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * CSV工具类
 **/

public class CSVUtils {
    private static Logger logger = LoggerFactory.getLogger(CSVUtils.class);
    //行尾分隔符定义
    private final static String NEW_LINE_SEPARATOR = ",";
    //上传文件的存储位置
    private final static URL PATH = Thread.currentThread().getContextClassLoader().getResource("");

    /**
     * 创建CSV文件
     * @return File
     * @param fileName 文件名
     * @param head 表头
     * @param values 表体
     * @throws IOException 抛出io异常
     **/
    public static File makeTempCSV(String fileName, String[] head, List<String[]> values) throws IOException {
//        创建文件
        File file = File.createTempFile(fileName, ".csv");
        //CSVFormat formator = CSVFormat.DEFAULT.withHeader().withRecordSeparator(NEW_LINE_SEPARATOR);
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(head).withSkipHeaderRecord();

        BufferedWriter bufferedWriter =
                new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        CSVPrinter printer = new CSVPrinter(bufferedWriter, formator);

//        写入表头
        printer.printRecord(head);

//        写入内容
        for (String[] value : values) {
            printer.printRecord(value);
        }

        printer.close();
        bufferedWriter.close();
        return file;
    }

    /**
     * 下载文件
     * @return boolean
     * @param response response
     * @param file file
     **/
    public static boolean downloadFile(HttpServletResponse response, File file) {
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream os = null;
        try {
            fileInputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            os = response.getOutputStream();
            //MS产本头部需要插入BOM
            //如果不写入这几个字节,会导致用Excel打开时,中文显示乱码
            os.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
            byte[] buffer = new byte[1024];
            int i = bufferedInputStream.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bufferedInputStream.read(buffer);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            //关闭流
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            file.delete();
        }
        return false;
    }

    /**
     * 上传文件
     * @return File
     * @param multipartFile multipartFile
     **/
    public static File uploadFile(MultipartFile multipartFile) {
        String path = PATH.getPath() + multipartFile.getOriginalFilename();
        try {
            File file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            multipartFile.transferTo(file);
            logger.info("上传文件成功,文件名===>" + multipartFile.getOriginalFilename() + ", 路径===>" + file.getPath());
            return file;
        } catch (IOException e) {
            logger.error("上传文件失败" + e.getMessage(), e);
            return null;
        }
    }

    /**
     * 读取CSV文件的内容(不含表头)
     * @return values 内容集
     * @param filePath 文件存储路径
     * @param colNum 列数
     **/
    public static List<List<String>> readCSV(String filePath, int colNum) {

        BufferedReader bufferedReader = null;
        InputStreamReader inputStreamReader = null;
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(filePath);
            inputStreamReader = new InputStreamReader(fileInputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            CSVParser parser = CSVFormat.DEFAULT.parse(bufferedReader);
//          表内容集合,外层List为行的集合,内层List为字段集合
            List<List<String>> values = new ArrayList<>();
            int rowIndex = 0;

            for (CSVRecord record : parser.getRecords()) {
//              跳过表头
                if (rowIndex == 0) {
                    rowIndex++;
                    continue;
                }
//              每行的内容
                List<String> value = new ArrayList<>(colNum + 1);
                for (int i = 0; i < colNum; i++) {
                    value.add(record.get(i));
                }
                values.add(value);
                rowIndex++;
            }
            return values;
        } catch (IOException e) {
            logger.error("解析CSV内容失败" + e.getMessage(), e);
        }finally {
            //关闭流
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
	 
	public static List<Map<String, String>> readCSVDatas(String path) {
        File file = new File(path);
        List<List<String>> reader = Reader(file);
        List<String> head = reader.get(0);
        List<Map<String, String>> datas = new ArrayList<>();
        for (int i = 1; i < reader.size(); i++) {
            List<String> dataList = reader.get(i);
            HashMap<String, String> data = new LinkedHashMap<>();
            for (int j = 0; j < head.size(); j++) {
                String key = head.get(j);
                if(j <= dataList.size()-1){
                    String value = dataList.get(j);
                    data.put(key, value);
                }
            }
            datas.add(data);
        }
        return datas;
    }

    /**
     * 生成csv并通过response返回
     * @param fileName
     * @param response
     * @param datas
     */
    public static void returnFile(String fileName, HttpServletResponse response, List<Map<String, Object>> datas) {
        //将数据变成csv文件返回
        List<String> head = new ArrayList<>();
        for (Map<String, Object> data : datas) {
            for (String key : data.keySet()) {
                if(!head.contains(key)){
                    head.add(key);
                }
            }
        }

        ArrayList<String[]> records = new ArrayList<>();
        for (Map<String, Object> data : datas) {
            ArrayList<String> record = new ArrayList<>();
            head.forEach(key ->{
                if(data.containsKey(key)){
                    record.add(String.valueOf(data.get(key)));
                }else{
                    record.add("");
                }
            });
            records.add(record.toArray(new String[data.size()]));
        }

        try {
            File file = CSVUtils.makeTempCSV(fileName, head.toArray(new String[head.size()]), records);
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName +".csv");
            CSVUtils.downloadFile(response, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     *  生成本地的静态Csv文件
     * @param destPath
     * @param fileName
     * @param datas
     */
    public static void makeFile(String destPath, String fileName, List<Map<String, Object>> datas) {
        //将数据变成csv文件返回
        List<String> head = new ArrayList<>();
        for (Map<String, Object> data : datas) {
            for (String key : data.keySet()) {
                if(!head.contains(key)){
                    head.add(key);
                }
            }
        }

        ArrayList<String[]> records = new ArrayList<>();
        for (Map<String, Object> data : datas) {
            ArrayList<String> record = new ArrayList<>();
            head.forEach(key ->{
                if(data.containsKey(key)){
                    record.add(String.valueOf(data.get(key)));
                }else{
                    record.add("");
                }
            });
            records.add(record.toArray(new String[data.size()]));
        }

        try {
            File file = CSVUtils.makeTempCSV(fileName, head.toArray(new String[head.size()]), records);
            FileInputStream input = new FileInputStream(file);
            //String destPath = "/home/sundial/BETA/test-exportData/data";
            File folder = new File(destPath);
            if(!folder.exists() && !folder.isDirectory()){
                folder.mkdirs();
            }
            OutputStream os = new FileOutputStream(new File(destPath + "/" + fileName + ".csv"));
            int len;
            byte[] arr = new byte[1024];
            while ((len = input.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值