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


    /*
      * 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);

  • 相关阅读:
    设计模式学习笔记之设计原则
    设计模式学习笔记之生成器模式
    设计模式学习笔记之适配器模式、外观模式
    java中的日期类型之间转换
    JS刷新当前页面的几种方法总结
    Java_枚举
    动态规划详解_2
    Java算法-动态规划详解
    Java经典算法题_2
    Java算法
  • 原文地址:https://www.cnblogs.com/lxaic/p/5016936.html
Copyright © 2020-2023  润新知