EasyPoi导出Excel,完整代码+案例(100%能导出——导不出来砍我)

java解决,EasyPoi导出Excel,废话不多说,直接上代码

第一步:导入依赖 

        
		
			cn.afterturn
			easypoi-base
			3.2.0
		
		
			cn.afterturn
			easypoi-web
			3.2.0
		
		
			cn.afterturn
			easypoi-annotation
			3.2.0
		

第二部:写好 导出的实体类(和Excel对应的)

package com.space.web.dto.finance.resp;

import java.math.BigDecimal;
import java.util.List;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import lombok.Data;

/**
 * @Description: 功能描述:财务模块-导出Excel
 * @ClassName: AcceptedProjectsDto.java
 * @author: 贺志辉
 * @date: 2019年6月14日
 */
@Data
@ExcelTarget("teacherEntity")
public class ExportDto {

	/**
	 * 项目id
	 */
	private Long projectId;

	/**
	 * 项目名称
	 */
	@Excel(name = "项目名称", orderNum = "0",needMerge=true)
	private String projectName;

	/**
	 * 项目金额
	 */
	@Excel(name = "项目金额", orderNum = "1",needMerge=true)
	private BigDecimal budget;

	/**
	 * 毛利
	 */
	@Excel(name = "毛利率(%)", orderNum = "2",needMerge=true)
	private BigDecimal grossProfit;

	/**
	 * 发放状态
	 */
	private int status;
	@Excel(name = "发放状态", orderNum = "3",needMerge=true)
	private String statusGrant;

	/**
	 * 奖金
	 */
	@Excel(name = "奖金(元)", orderNum = "4",needMerge=true)
	private BigDecimal bonus;

	/**
	 * 用户id
	 */

	private long userId;

	public void setUserId(Long userId) {
		this.userId = userId;
//		this.userName = UserUtil.lastName(userId);
	}

	/**
	 * 姓名
	 */
	@ExcelCollection(name="",orderNum = "5")
	private List listExportUser;
	
	/**
	 * 税额
	 */
    private BigDecimal paymentTax;



}

第三部:写好,需要导出的 值(就是从数据库查询,需要导入的值)--这是重点

//要导出的值
List exportExcel = bonusDistributionService.exportExcel(status);

第四部:写导出控制类
 

	/**
	 * @Description: 财务模块-导出要发Excel的表格
	 * @author: 贺志辉
	 * @return: void
	 */
	@GetMapping("exportExcel")
	public void searchMemberList(HttpServletResponse response, Integer status) {

		Workbook workbook = null;
		// 传真实地址
		try {
			// String time =
			// DateUtil.getTime(startTime)+"至"+DateUtil.getTime(endTime)+".xls";
			String fileName = "奖金明细列表" + LocalDate.now();
			//要导出的值
			List exportExcel = bonusDistributionService.exportExcel(status);
			// list = flowService.excelExport(projectStateReqDto);
			workbook = ExcelExportUtil.exportExcel(new ExportParams(), ExportDto.class, exportExcel);
			response.reset();//清除首部的空白行。
			response.setContentType("application/xls;charset=UTF-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=\"" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xls" + "\"");
			OutputStream out = response.getOutputStream();
			workbook.write(out);
			out.flush();
			out.close();
		} catch (IOException ex) {
			throw new BizException("导出失败");
		}
	}

第五步:测试导出(用浏览器测试的,浏览器直接下载一个,Exel文件)如下所示

EasyPoi导出Excel,完整代码+案例(100%能导出——导不出来砍我)_第1张图片

OK-->导入完成啦!
注意:其实导出不难,重点在第三部,写你需要导出的数据,其实也是根据你们的业务逻辑而定的,业务简单,就简单了

有问题,欢迎联系交流!

你可能感兴趣的:(java)