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

5 apache poi实现excel的动态下拉框功能

excel下拉框

@RequestMapping("xiala")public void xiala(HttpServletResponse response){String fileName = "僵尸表";try{response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");response.setHeader("Content-Disposition","attachment;filename=" +URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20") + ".xlsx");Workbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet("Sheet1");String[] dropdownOptions = {"Option 1", "Option 2", "Option 3"};CellRangeAddressList addressList = new CellRangeAddressList(1, 100, 0, 0); // A1单元格。第二行到第100行记录数// 创建数据验证助手DataValidationHelper validationHelper = sheet.getDataValidationHelper();// 创建数据验证约束DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(dropdownOptions);// 创建数据验证对象DataValidation dataValidation = validationHelper.createValidation(constraint, addressList);// 将数据验证添加到表格中sheet.addValidationData(dataValidation);//处罚来源String[] publishType = {"个人", "企业", "33"};CellRangeAddressList publishTypeList = new CellRangeAddressList(1, 100, 1, 1);//A2单元格 处罚来源,第二行到第100行记录数DataValidationConstraint publishTypeConstraint = validationHelper.createExplicitListConstraint(publishType);DataValidation publishValidation = validationHelper.createValidation(publishTypeConstraint, publishTypeList);sheet.addValidationData(publishValidation);OutputStream os = new BufferedOutputStream(response.getOutputStream());workbook.write(os);os.flush();os.close();} catch (IOException e) {e.printStackTrace();} finally {}System.out.println("Excel文件创建成功,包含下拉框!");}

excel sheet的复制

@RequestMapping("list")public void list(HttpServletResponse response) throws Exception{System.out.println("------ 开始下载模板 ------");//获取要下载的模板名称String fileName = URLEncoder.encode("名单导入模板","UTF-8");response.setHeader("Content-Disposition","attachment;filename=" + fileName + ".xlsx");response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");//source sheetInputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/template.xlsx");Workbook sourceWorkbook = new XSSFWorkbook(inputStream);Workbook destWorkbook = new XSSFWorkbook();// 获取源工作簿中的第一个工作表Sheet sourceSheet = sourceWorkbook.getSheetAt(0);// 在目标工作簿中创建一个新的工作表Sheet destSheet = destWorkbook.createSheet(sourceSheet.getSheetName());// 复制每一行for (int rowIndex = 0; rowIndex <= sourceSheet.getLastRowNum(); rowIndex++) {Row sourceRow = sourceSheet.getRow(rowIndex);Row destRow = destSheet.createRow(rowIndex);if (sourceRow != null) {// 复制每一列for (int colIndex = 0; colIndex < sourceRow.getLastCellNum(); colIndex++) {Cell sourceCell = sourceRow.getCell(colIndex);Cell destCell = destRow.createCell(colIndex);if (sourceCell != null) {// 复制单元格的样式CellStyle newCellStyle = destWorkbook.createCellStyle();newCellStyle.cloneStyleFrom(sourceCell.getCellStyle());destCell.setCellStyle(newCellStyle);// 复制单元格的值switch (sourceCell.getCellType()) {case STRING:destCell.setCellValue(sourceCell.getStringCellValue());break;case NUMERIC:destCell.setCellValue(sourceCell.getNumericCellValue());break;case BOOLEAN:destCell.setCellValue(sourceCell.getBooleanCellValue());break;case FORMULA:destCell.setCellFormula(sourceCell.getCellFormula());break;case BLANK:destCell.setBlank();break;default:break;}}}}}OutputStream os = new BufferedOutputStream(response.getOutputStream());destWorkbook.write(os);os.flush();os.close();}

excel列之间的级联

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

相关文章:

  • 深度对比:etcd、Consul、Zookeeper 和 Nacos 作为注册中心和配置中心的优势与劣势
  • Android webview拦截H5的接口请求并返回处理好的数据
  • vue echarts tooltip使用动态模板
  • 網路本地連接沒有有效的IP配置:原因與解決方法
  • 如何使用ssm实现基于web的学生就业管理系统的设计与实现+vue
  • TCP三次握手四次挥手详解
  • 了解 如何使用同快充充电器给不同设备快速充电
  • AGI interior designer丨OPENAIGC开发者大赛高校组AI创作力奖
  • Centos安装docker(linux安装docker)——超详细小白可操作手把手教程,包好用!!!
  • QT day01
  • 如何从飞机、电报中提取数据
  • 【算法篇】二叉树类(2)(笔记)
  • Flask学习之项目搭建
  • **CentOS7安装Maven**
  • (undone) MIT6.824 Lecture1 笔记
  • 小白投资理财 - 开篇
  • 高中还来得及选择信息学奥赛赛道吗?
  • 01_OpenCV图片读取与展示
  • C语言中的输入控制重要基础
  • Vue 学习
  • Redis集群的两种方式
  • QT--基础
  • 一、前后端分离及drf的概念
  • AI垃圾溢出识别摄像机
  • 【抽代复习笔记】29-群(二十三):生成子群的两道例题及子群陪集的定义
  • 安全防护装备检测系统源码分享
  • easyexcel常见问题分析
  • 精通推荐算法31:行为序列建模之ETA — 基于SimHash实现检索索引在线化
  • Python知识点:如何使用Python进行卫星数据分析
  • Python实现Phong着色模型算法