Excel模板导出

//下面是实体类

package com.test.model;


import java.util.Date;


//模板数据实体

public class UserTmpl {

  private int id;

  private String name;

  private Date birthday;

  private String identity;

  private String phone;

  private String address;

  private String Email;



  public UserTmpl() {

    super();

  }

  public UserTmpl(int id, String name, Date birthday, String identity, String phone, String address, String email) {

    super();

    this.id = id;

    this.name = name;

    this.birthday = birthday;

    this.identity = identity;

    this.phone = phone;

    this.address = address;

    Email = email;

  }

  public int getId() {

    return id;

  }

  public void setId(int id) {

    this.id = id;

  }

  public String getName() {

    return name;

  }

  public void setName(String name) {

    this.name = name;

  }

  public Date getBirthday() {

    return birthday;

  }

  public void setBirthday(Date birthday) {

    this.birthday = birthday;

  }

  public String getIdentity() {

    return identity;

  }

  public void setIdentity(String identity) {

    this.identity = identity;

  }

  public String getPhone() {

    return phone;

  }

  public void setPhone(String phone) {

    this.phone = phone;

  }

  public String getAddress() {

    return address;

  }

  public void setAddress(String address) {

    this.address = address;

  }

  public String getEmail() {

    return Email;

  }

  public void setEmail(String email) {

    Email = email;

  }

}



//下面是导出execl的类

package com.test.utils;


import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.util.List;

import org.apache.log4j.Logger;

import net.sf.excelutils.ExcelException;

import net.sf.excelutils.ExcelUtils;

public class ExportUtil {

    static Logger log = Logger.getLogger(ExportUtil.class.getName());



    public int ExportExcel(String tmplPath,String tmplName,String objFilePath,String objFileName,List resourceList){

      //参数非空

      if(tmplPath.equals(null)||tmplName.equals(null)||objFilePath.equals(null)||objFileName.equals(null)||resourceList.equals("null")||resourceList.size()<1||resourceList==null){

        System.out.println("传入参数有误或有空值,中断执行");

        return 1;

      }

        //模板路径及名称

        tmplPath=tmplPath + "/" + tmplName + ".xls";

        log.info("模板文件存放目录:"+tmplPath);

        //目标路径及名称

        String mbFileName = objFilePath + "/" + objFileName+ ".xls";

        log.info("目标文件生成目录:"+tmplPath);

        //添加要生成的值

        ExcelUtils.addValue("reportList", resourceList);

      try {

        //检测模板文件是否存在

        File lcfile = new File(tmplPath); 

        if(!lcfile.exists()){

          log.error("模板文件不存在,请确认其位置");

          return -1;

        }

        //检测目标路径是否存在,不存在则创建

        File file =new File(objFilePath);

        if(!file.exists()  && !file.isDirectory()){

          file .mkdir();   

        }

        //导出Excel

         ExcelUtils.export(tmplPath, new FileOutputStream(mbFileName));

      }catch (FileNotFoundException e) {

          log.error("导出异常:",e.fillInStackTrace());

          return 2;
      }catch (ExcelException e) {
          log.error("导出异常:",e.fillInStackTrace());
          return 2;
      }
          log.info("导出成功,文件生成目录:"+mbFileName);
          return 0;
    }
}

//截下来是excel模板

编号 姓名 出生日期 身份证号 联系电话  现住址 联系邮箱

#foreach user in ${reportList}
${user.id}  ${user.name} ${user.birthday} ${user.identity} ${user.phone} ${user.address} ${user.email}
#end

//依赖包

commons-beanutils.jar

commons-logging.jar

excelutils-1.41.jar

log4j-1.2.13.jar

poi-2.5.1.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值