java使用easypoi导出并下载excel文件(maven)

weblog 2776 0 0

java使用easypoi导出并下载excel文件

简单导出测试如图:

一、pom文件
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>
二、实体类
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("stu")
public class Stu {
	@Excel(name = "id")
	private Integer id;
	@Excel(name = "姓名",width = 20)
	private String name;
	public Stu(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
三、浏览器类型判断工具类
import javax.servlet.http.HttpServletRequest;
/**
 * 	浏览器类型判断
 * @author 硅谷探秘者(jia)
 *
 */
public class CheckUtil {
	/**
	 * 	判断是不是ie浏览器
	 * @param request
	 * @return
	 */
	public static boolean isMSBrowser(HttpServletRequest request) {
        String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"};
        String userAgent = request.getHeader("User-Agent");
        for (String signal : IEBrowserSignals) {
        	if (userAgent.contains(signal)){
                return true;
            }
        }
        return false;
    }
}
四、excel导出下载工具类
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

/***
 * 	excel导出
 * @author 硅谷探秘者(jia)
 */
public class ExcelExportDownloadUtil {
	/**
	 * 	泛型 t 规定统一类型
	 */
	public static <T> void exportAndDownload(HttpServletResponse response,HttpServletRequest request,String title,String fileName,List<T> list,Class<T> c)throws IOException {
		Workbook workbook = ExcelExportUtil.exportExcel( new ExportParams(title,title),c,list);
        response.setContentType(new MimetypesFileTypeMap().getContentType("application/octet-stream"));// 设置response内容的类型
        if (CheckUtil.isMSBrowser(request)) {
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} else {
			fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
		}
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + "." +"xls");// 设置头部信息
        workbook.write(response.getOutputStream());
        workbook.close();
	}
}
五、测试controller
	/**
	 * 	导出
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(value = "/exports", method = RequestMethod.GET)
	public void exports(HttpServletResponse response,HttpServletRequest request) throws IOException {
		List<Stu> stus=new ArrayList<Stu>();
		stus.add(new Stu(1,"张三1"));
		stus.add(new Stu(2,"张三2"));
		stus.add(new Stu(3,"张三3"));
		stus.add(new Stu(4,"张三4"));
		stus.add(new Stu(5,"张三5"));
		ExcelExportDownloadUtil.exportAndDownload(response, request, "测试","测试导出", stus,Stu.class);
	}

浏览器访问测试即可

动态设置a标签的href

      $(document).on('click','.e',function(){
          var message=$('#name').val();
    	  $(this).prop("href","/teamManage/export?message="+message);
      })

 


猜你喜欢
weblog 6102 java使easypoiexcel时进行值替换如性别:数据库中储存的是数字,1代表男,2代表女。的时候需要是汉字。在实体类中使@Excel注解。 @Excel(name="性别
weblog 1272 java使easypoi批量excel实体类:importcn.afterturn.easypoi.excel.annotation.Excel
算法基础 2268 什么是jxlsjxls是一个简单的、轻量级的excel库,使特定的标记在excel模板中来定义输格式和布局。其实java中成熟的excel工具有pol、jxl,但他们都是使java
spring/springmvc 2776 1.easypoi的pom依赖dependency groupIdcn.afterturn/groupId artifactIdeasypoi-base/artifactId
框架 1836 maven项目使tomcat7插1.pombuild finalNamedz/finalName plugins plugin
算法基础 1324 安装node第一步依然是安装node,node.js安装安装:https://nodejs.org/en/全局安装clean-cssnpminstall-gclean-css局部安装
算法基础 3736 Pom依赖dependency groupIdcn.afterturn/groupId artifactIdeasypoi-base/artifactId version4.0.0/version /dependency dependency groupIdcn.afterturn/groupId artifactIdeasypoi-web/artifactId version4.0.0/ver
minio 2656 创建buckets 创建serviceaccounts java上传 maven依赖: dependency groupIdio.minio/groupId
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。