阅读背景:

将数据导出csv文件并下载

来源:互联网 

将数据导出csv文件并下载


1.导出csv文件方法

package com.demo.main;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;


public class CsvUtil {
    // 文件位置(可使用参数传递的方式动态指定)
    private final static String path = "C:/member.csv";

    /**
     * 导出
     *
     * @param dataList 数据
     * @param headdataList 标题数据数据
     * @return
     */

    public static boolean exportCsv(List<String> dataList,List<String> headdataList){
        File file = new File(path);
        boolean isSucess=false;

        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            if(!file.exists()){
                file.createNewFile();
            }
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out);
            bw =new BufferedWriter(osw);
            if(headdataList!=null && !headdataList.isEmpty()){
                for(String data : headdataList){
                    bw.append(data).append("\r");
                }
            }
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

   
}
package com



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: