导出excel 自定义模板

CustomSheetWriteHandler
package org.springblade.common.utils;


import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.Map;

public class CustomSheetWriteHandler implements SheetWriteHandler {

	private Map<Integer, String[]> mapDropDown;

	public CustomSheetWriteHandler(Map<Integer, String[]> mapDropDown) {
		this.mapDropDown = mapDropDown;
	}

	@Override
	public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

	}

	@Override
	public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

		//获取工作簿
		Sheet sheet = writeSheetHolder.getSheet();
		///开始设置下拉框
		DataValidationHelper helper = sheet.getDataValidationHelper();
		//设置下拉框
		for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
			/*起始行、终止行、起始列、终止列  起始行为1即表示表头不设置**/
			//这里设置65535可能又问题,因为这个是excel的最大行数,如果数据量超过这个数,就会报错
			CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, entry.getKey(), entry.getKey());
			/*设置下拉框数据**/
			DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
			DataValidation dataValidation = helper.createValidation(constraint, addressList);

			//阻止输入非下拉选项的值
			dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
			dataValidation.setShowErrorBox(true);
			dataValidation.setSuppressDropDownArrow(true);
			dataValidation.createErrorBox("提示", "输入值与单元格定义格式不一致");
			dataValidation.createPromptBox("填写说明", "填写内容只能为下拉数据集中的类型");

			sheet.addValidationData(dataValidation);
		}
	}
}


controller

	/**
	 * 下载导入模板
	 *
	 * @param response
	 * @throws IOException
	 */
	@GetMapping("/downloadExcelTemplate")
	public void downloadExcelTempplate(HttpServletResponse response) throws IOException {
		List<RegionAnalysisParkAttractExcel> list = new ArrayList<>();

		//定义一个map key是需要添加下拉框的列的index value是下拉框数据
		Map<Integer, String[]> mapDropDown = new HashMap<>();
		List<String> list1=regionAnalysisParkAttractService.getAllProject();
		String[] belongOwner =  list1.toArray(new String[list.size()]);
		String[] year =  {"2018","2019","2020","2021","2022","2023","2024"};
		String[] month =  {"01","02","03","04","05","06","07","08","09","10","11","12"};
		//下拉选在Excel中对应的列
		mapDropDown.put(4,belongOwner);
		mapDropDown.put(0,year);
		mapDropDown.put(1,month);
		// 这里URLEncoder.encode可以防止中文乱码 easyexcel没有关系
		String fileName = URLEncoder.encode("园区招商引资到位资金模板", "UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		//需要下拉框处理,关键是registerWriteHandler
		EasyExcel.write(response.getOutputStream(), RegionAnalysisParkAttractExcel.class).sheet("园区招商引资到位资金模板").registerWriteHandler(new CustomSheetWriteHandler(mapDropDown)).doWrite(list);
	}

实体类

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.ks.regionAnalysisParkAttract.excel;


import com.alibaba.excel.annotation.ExcelIgnore;
import lombok.Data;

import java.util.Date;
import java.math.BigDecimal;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import java.io.Serializable;


/**
 * 区域分析_园区招商引资到位资金 Excel实体类
 *
 * @author BladeX
 * @since 2023-10-25
 */
@Data
@ColumnWidth(25)
@HeadRowHeight(20)
@ContentRowHeight(18)
public class RegionAnalysisParkAttractExcel implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 年份
	 */
	@ColumnWidth(20)
	@ExcelProperty("年份")
	private String year;
	/**
	 * 月份
	 */
	@ColumnWidth(20)
	@ExcelProperty("月份")
	private String month;

	/**
	 * 金额
	 */
	@ColumnWidth(20)
	@ExcelProperty("到位资金(亿元)")
	private BigDecimal value;

	@ColumnWidth(20)
	@ExcelProperty("目标资金(亿元)")
	private BigDecimal inMoney;

	/**
	 * 园区id
	 */
	@ColumnWidth(20)
	@ExcelProperty("区域")
	private Long regionId;

	/**
	 * 环比
	 */
	@ColumnWidth(20)
	@ExcelProperty("完成情况(%)")
	private String percent;

	/**
	 * 园区名字
	 */
	@ColumnWidth(20)
	@ExcelProperty("园区名字")
	@ExcelIgnore
	private String regionName;
	/**
	 * 是否已删除
	 */
	@ColumnWidth(20)
	@ExcelProperty("是否已删除")
	@ExcelIgnore
	private Integer isDeleted;

}

参考链接:导出带下拉选项的Excel基于EasyExcel实现_easyexcel 下拉框-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值