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

相关推荐
躺平大鹅6 小时前
Java面向对象入门(类与对象,新手秒懂)
java
码路飞7 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
初次攀爬者7 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺7 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart9 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
曲幽10 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
NE_STOP10 小时前
MyBatis-mybatis入门与增删改查
java
孟陬13 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌13 小时前
一站式了解四种限流算法
java·后端·go
华仔啊13 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java