jxls 生成Excel的整套代码

//这是jxls 生成Excel的整套代码,希望对大家有帮助阿 

public String creatExcelBylist( String relativelyPath, String templateFileName, List guestMembers, String makerName) {

        if(relativelyPath == null || relativelyPath==""){
            return null;
        }
        if (!(relativelyPath.endsWith("//") || relativelyPath.endsWith("/"))) {
            relativelyPath += "/";
        }
        String fileName = createFileName();//生成随机文件
        String destFileName = relativelyPath+fileName;
        try {
            createFileCatalog(relativelyPath);//如果路径不存在则创建一个文件目录
            delFile(relativelyPath);//删除指定目录下今天(24小时)之前的文件             //TODO
            Map beans = new HashMap();
            beans.put("maker",  makerName);
            beans.put("time", DateUtils.currentTime());
            beans.put("members", guestMembers);
            XLSTransformer transformer = new XLSTransformer();
      //      String n = getClass().getResource("/").getFile();
            String m = this.getClass().getClassLoader().getResource("").getPath();
            long startTime = System.currentTimeMillis();
            transformer.transformXLS(m+templateFileName, beans, destFileName);
            long endTime = System.currentTimeMillis();
            System.out.println((endTime-startTime)/1000);
          *//*  File file = ResourceUtils.getFile("classpath:excel/roomorder_crs.xls");
            InputStream is = new BufferedInputStream(new FileInputStream(file));
            transformer.transformXLS(is, beans);*//*
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;

}

 

 

/*
  *@Function      : delFile

  *@Description   :  删除文件存储目录下x小时之前的文件
  *@Input         : Excel文件保存地址等
  *@Calls by      : createExcel(...)
  */
    private void delFile(String filePath){
        File file = new File(filePath);
        if(file.exists()){
            for(int i=0;i<file.list().length;i++){
               File[] files = file.listFiles();
               String[] fileN = file.list();
               String fileStr = fileN[i];
               String[] hourFile = fileStr.split("_");

               if(hourFile.length>2 && DateUtils.isTimePeriodExceed24Hours(hourFile[1],DateUtils.mineTime())){
                   files[i].delete();
               }                                                                               
            }
        }
    }

    /*
  *@Function      : createFileName

  *@Description   : 随机生成文件名
  *@Calls by      : createExcel(...)
  */
    private String createFileName() {
        return "member_"+DateUtils.mineTime() + "_" + System.currentTimeMillis() + "_" + Math.round(Math.random()*10000)+".xls";       //TODO
    }

  /*
  *@Function      : createFileCatalog

  *@Description   : 创建文件目录
  *@Input         : Excel文件保存地址等
  *@Calls by      : createExcel(...)
  */
    private void createFileCatalog(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值