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文件

相关推荐
海兰1 分钟前
【第3篇】使用LangGraph构建工作流
人工智能·windows
滴滴答答哒2 分钟前
c#将平铺列表转换为树形结构(支持孤儿节点作为独立根节点)
java·前端·c#
不知名XL3 分钟前
day02 mcp开发以及skill开发规范
python
李少兄4 分钟前
Windows系统JDK安装与环境配置指南(2026年版)
java·开发语言·windows
好家伙VCC2 小时前
**神经编码新视角:用Python实现生物启发的神经信号压缩与解码算法**在人工智能飞速发展的今天
java·人工智能·python·算法
一灯架构8 小时前
90%的人答错!一文带你彻底搞懂ArrayList
java·后端
踏着七彩祥云的小丑8 小时前
pytest——Mark标记
开发语言·python·pytest
xiaoshuaishuai89 小时前
C# 接入 OpenClaw
windows·visualstudio·c#
Y4090019 小时前
【多线程】线程安全(1)
java·开发语言·jvm
不爱吃炸鸡柳10 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python