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

相关推荐
Dxy123931021624 分钟前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
我上去就是一套QWER然后等待复活27 分钟前
Windows 清理 CC Switch 并恢复官方 Codex CLI 登录流程(完整记录)
windows
bamb001 小时前
一个项目带你入门AI应用开发01
python
0566461 小时前
Python康复训练——常用标准库
开发语言·python·学习
极光代码工作室1 小时前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
吾儿良辰1 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#
哎呦喂我去去去1 小时前
C#实现屏幕墙:同时监控多个电脑桌面(支持Windows、信创Linux、银河麒麟、统信UOS)
linux·windows·c#
昆曲之源_娄江河畔1 小时前
Python如何安装flask, pymssql
开发语言·python·flask·pymssql
Leighteen2 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
0566462 小时前
Python康复训练——控制流与函数
开发语言·python·学习