文件下载公共方法 以及调用

/*
  * List<String[]>   传入
  * 公共方法
  */
 public static boolean exportCsv(File file, List dataList) {
  boolean isSuccess = false;
  
  if(file == null || !file.getName().toUpperCase().endsWith(格式)) {
   isSuccess = false;
  } else {
   FileOutputStream out = null;
   OutputStreamWriter osw = null;
   BufferedWriter bw = null;
   
   int i = 1;
   try {
    bw = new BufferedWriter(new FileWriter(file));
    for (int j = 0; j < dataList.size(); j++) {
     //第一行是标题
     String[] data = (String[])dataList.get(j);
     String dataStr = "";
     for (int k = 0; k < data.length; k++) {
      if(k == data.length - 1) {
       dataStr = dataStr + data[k];
      } else {
       dataStr = dataStr + data[k] + ",";
      }
     }
     bw.write(dataStr);
     bw.newLine();
    }
    bw.flush();
    isSuccess = true;
   } catch(IOException e) {
    e.printStackTrace();
    isSuccess = false;
   } finally {
    if(out != null) {
     try {
      out.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
   
  }
  
  return isSuccess;
 }

 

 

 

 

  调用

   File csvFile = null;
   try {
    //生成临时文件
    csvFile = File.createTempFile("模板名称", "模板格式");
   } catch (IOException e) {
    e.printStackTrace();
   }
   List<String[]> dataList = new ArrayList<String[]>();
   String[] data = new String[]{"姓名","年龄","编号"};  //文件列名称 
   dataList.add(data);
   
   CsvUtil.exportCsv(csvFile, dataList);

 

转载于:https://www.cnblogs.com/lxaic/p/5016936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值