Java实现下载excel模板,并实现自定义下拉框

java 复制代码
@GetMapping("excel/download")
	@ApiOperation(value = "模板下载")
	public void getUserRecordTemplate(HttpServletResponse response, HttpServletRequest request) throws IOException {

		

		OutputStream outputStream = response.getOutputStream();
		InputStream inputStream = new ClassPathResource("example/excel/download_template_user_record.xlsx").getInputStream();
		try {

			String fileName = "导入模板";
			String fileName3 = URLEncoder.encode(fileName, "utf-8");
			response.setHeader("Content-disposition", "attachment;filename*=utf-8''"+fileName3+".xlsx");
			response.setContentType("application/msexcel");// 定义输出类型
			response.setCharacterEncoding("UTF-8");

			//获取该类声明的所有字段
			Field[] fields = UserRecordExcelExportResult.class.getDeclaredFields();

			//响应字段对应的下拉集合
			Map<Integer, String[]> map = new HashMap<>();
			Field field = null;
//从数据库里获取下拉框选项
			List<XcUserLevel> list = xcUserLevelService.list();
//			List<SysDept> list1 = sysDeptService.list();
			List<String> collect = list.stream().map(XcUserLevel::getName).collect(Collectors.toList());
//			List<String> collect1 = list1.stream().map(SysDept::getName).collect(Collectors.toList());
//找到需要对应的下拉框位置
//			map.put(11,collect1.toArray(new String[0]));
			map.put(12,collect.toArray(new String[0]));
			// 循环判断哪些字段有下拉数据集,并获取
			/*for (int i = 0; i < fields.length; i++) {
				field = fields[i];
				// 解析注解信息
				DropDownSetField dropDownSetField = field.getAnnotation(DropDownSetField.class);
				if (null != dropDownSetField) {
					//调用下拉框数据源的方法
					String[] source = ResoveDropAnnotationUtil.dynamicListResove2(dropDownSetField, field.getName(),staffId);
					if (null != source && source.length > 0) {
						map.put(i, source);
					}
				}
			}*/

			ExcelWriter excelWriter = EasyExcel.write(outputStream)
//将保存下拉框的map放入excelWriter
					.registerWriteHandler(new CustomSheetWriteHandler(map))
					.withTemplate(inputStream)
					.build();

			WriteSheet writeSheet = EasyExcel.writerSheet(0,"Sheet1").build();
				excelWriter.write(new ArrayList<>(), writeSheet);
			excelWriter.finish();
			outputStream.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

从resoues包里获取excel文件

相关推荐
sduwcgg1 分钟前
python的numpy的MKL加速
开发语言·python·numpy
大模型真好玩3 分钟前
可视化神器WandB,大模型训练的必备工具!
人工智能·python·mcp
wkj0014 分钟前
QuaggaJS 配置参数详解
java·linux·服务器·javascript·quaggajs
东方佑4 分钟前
使用 Python 自动化 Word 文档样式复制与内容生成
python·自动化·word
钢铁男儿10 分钟前
Python 接口:从协议到抽象基 类(定义并使用一个抽象基类)
开发语言·python
databook18 分钟前
当机器学习遇见压缩感知:用少量数据重建完整世界
python·机器学习·scikit-learn
异常君40 分钟前
MyBatis 中 SqlSessionFactory 和 SqlSession 的线程安全性深度分析
java·面试·mybatis
crud1 小时前
Spring Boot 使用 spring-boot-starter-validation 实现优雅的参数校验,一文讲透!
java·spring boot
Dcs1 小时前
常见 GC 垃圾收集器对比分析
java
M1A11 小时前
Python数据结构操作:全面解析与实践
后端·python