当前位置: 首页 > news >正文

【JAVA实战】JAVA实现Excel模板下载并填充模板下拉选项数据

背景

有这样一个场景:前端下载Excel模板,进行数据导入,这个下载模板过程需要经过后端接口去数据库查询数据进行某些列的下拉数据填充,下拉填充的数据过程中会出现错误String literals in formulas can’t be bigger than 255 characters ASCII,超过字符限制。
那么,如何解决?

解决方案

引入隐藏区域方式,比如 可以创建一个隐藏Sheet专门存储该下拉填充数据,需要使用到的地方进行引用该Sheet区域范围内容。

实现过程

下载接口
@ApiOperation(value = "下载EXCEL文件模板", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)@GetMapping("/download/{bizType}")public void downloadExcel(HttpServletResponse response, @PathVariable("bizType") String bizType) throws IOException {if (Arrays.stream(AliYunOssBizTypeEnum.values()).noneMatch(x -> x.getCode().equals(bizType))) {throw new RuntimeException("类型有误!");}String fileName = bizType + ".xls";InputStream inputStream = aliYunOssService.download("template/" + fileName);if (inputStream == null) {throw new RuntimeException("获取阿里云文件模板失败,请检查是否上传到阿里云OSS");}HSSFWorkbook wb = new HSSFWorkbook(inputStream);Sheet sheet = wb.getSheetAt(0);ExcelAbstractHandler handler = ExcelHandlerFactory.getHandler(bizType);if (Objects.nonNull(handler)) {// 3. 创建隐藏工作表存储选项Sheet hiddenSheet = wb.createSheet("HiddenSheet");wb.setSheetHidden(wb.getSheetIndex(hiddenSheet), true); // 隐藏工作表handler.handle(hiddenSheet, sheet, bizType);}ServletOutputStream output = response.getOutputStream();response.setContentType("application/vnd.ms-excel");String desc = Arrays.stream(AliYunOssBizTypeEnum.values()).filter(x -> x.getCode().equals(bizType)).findFirst().get().getDesc();fileName = URLEncoder.encode(desc, "UTF-8");response.setHeader("Content-Disposition", "attachment;filename=" + fileName);wb.write(output);output.flush();output.close();wb.close();}
抽象处理器
public abstract class ExcelAbstractHandler {public abstract void handle(Sheet hiddenSheet, Sheet sheet, String bizType);protected void buildDropdownData(Sheet mainSheet, List<String> list, int col, String referenceRange) {if (CollectionUtil.isEmpty(list)) {return;}DataValidationHelper dvHelper = mainSheet.getDataValidationHelper();DataValidationConstraint dvConstraint = dvHelper.createFormulaListConstraint(referenceRange);CellRangeAddressList addressList = new CellRangeAddressList(1, 2000, col, col);DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);mainSheet.addValidationData(validation);}
}
处理工厂类

public class ExcelHandlerFactory {private static final Map<String, ExcelAbstractHandler> handlerMap = new ConcurrentHashMap<>();public static ExcelAbstractHandler getHandler(String type) {return handlerMap.get(type);}public static void register(String type, ExcelAbstractHandler handler) {Assert.notNull(type, "type can't be null");handlerMap.put(type, handler);}
}
产品标准价格模板
@Component
@Slf4j
@RequiredArgsConstructor
public class ExcelProductPlatformPriceHandler extends ExcelAbstractHandler implements InitializingBean {private final BusinessBaseCountryMapper businessBaseCountryMapper;private final BusinessBaseShopPlatformMapper businessBaseShopPlatformMapper;@Overridepublic void afterPropertiesSet() throws Exception {ExcelHandlerFactory.register(AliYunOssBizTypeEnum.PRODUCT_PLATFORM_PRICE.getCode(), this);}@Overridepublic void handle(Sheet hiddenSheet, Sheet sheet, String bizType) {// 1.平台数据List<BusinessBaseShopPlatform> platforms = businessBaseShopPlatformMapper.selectList(Wrappers.lambdaQuery());List<String> platformList = platforms.stream().map(BusinessBaseShopPlatform::getPlatformName).collect(Collectors.toList());//   写入选项到隐藏工作表(逐行填充)for (int i = 0; i < platformList.size(); i++) {Row row = hiddenSheet.createRow(i);Cell cell = row.createCell(0);cell.setCellValue(platformList.get(i));}//   定义引用区域(例如:Hidden!A1:A100)String referenceRange = hiddenSheet.getSheetName() + "!$A$1:$A$" + platformList.size();buildDropdownData( sheet, platformList, 1, referenceRange);// 2.国家数据List<BusinessBaseCountry> countries = businessBaseCountryMapper.selectList(Wrappers.lambdaQuery());List<String> countryList = countries.stream().map(BusinessBaseCountry::getCountryName).collect(Collectors.toList());//  写入选项到隐藏工作表(逐行填充)int preCount = platformList.size();for (int i = preCount; i < (preCount + countryList.size()); i++) {Row row = hiddenSheet.createRow(i);Cell cell = row.createCell(0);cell.setCellValue(countryList.get(i - preCount));}//  定义引用区域(例如:Hidden!A1:A100)referenceRange = hiddenSheet.getSheetName() + "!$A$" + (preCount + 1) + ":$A$" + (preCount + countryList.size());buildDropdownData(sheet, countryList, 2 , referenceRange);}
}

最后

以上是一个简单操作下载导出模板并填充数据后下载模板接口,经供参考!

http://www.lryc.cn/news/539346.html

相关文章:

  • java面试笔记(一)
  • 【C++】36.C++IO流
  • Qt5开发入门指南:从零开始掌握跨平台开发
  • Rook-ceph(1.92最新版)
  • 深度学习在蛋白质-蛋白质相互作用(PPI)领域的研究进展(2022-2025)
  • 网络安全学习架构 网络安全架构内容
  • 硕成C语言24
  • 《Stable Diffusion绘画完全指南:从入门到精通的Prompt设计艺术》-配套代码示例
  • Linux下为Intel核显安装OpenCL
  • 用deepseek学大模型04-机器学习建模过程
  • 【ClickHouse】Ubuntu下离线安装ClickHouse数据库并使用DBeaver连接
  • Unity3D实现接入DeepSeek对话
  • 【ISO 14229-1:2023 UDS诊断(会话控制0x10服务)测试用例CAPL代码全解析②】
  • 前端新手必看:10 大 UI 组件库全面解析,快速搭建高质量 Web 应用」 「从零开始:Vue 和 React 最受欢迎的 UI 组件库入门指南」 「超实用!PC 端和移动端 UI 组件库推荐与实战
  • 【MySQL高级】17 - MySQL中常用工具
  • 【Linux】Linux 文件系统——有关 inode 不足的案例
  • 计算机视觉:卷积神经网络(CNN)基本概念(二)
  • 【第7章:注意力机制与Transformer模型—7.4 NLP领域的BERT、GPT系列模型】
  • [代码调试]安装Text2Image(stable diffusion)模型环境的踩坑记录
  • 大数据SQL调优专题——Flink执行原理
  • Oracle 12c中在同一组列上创建多个索引
  • 线程安全的集合类
  • 【如何实现 JavaScript 的防抖和节流?】
  • C#中File类的Copy()方法或FileInfo类的CopyTo()方法的参数overwrite取false和true的区别
  • 力扣 买卖股票的最佳时机
  • 蚁剑(AutSword)的下载安装与报错解决
  • 【全栈开发】----Mysql基本配置与使用
  • Spring Boot项目的基本设计步骤和相关要点介绍
  • 【Spring快速入门】不断更新...
  • nodejs版本管理,使用 nvm 删除node版本,要删除 Node.js 的某个版本详细操作