当前位置: 首页 > 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 sheet
        InputStream 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列之间的级联

相关文章:

  • RTA-OS Port Guide学习(三)-基于S32K324 OS
  • 一步一步丰富生成式语言模型系统
  • 计算机毕业设计Python+Tensorflow股票推荐系统 股票预测系统 股票可视化 股票数据分析 量化交易系统 股票爬虫 股票K线图 大数据毕业设计 AI
  • 安全点的应用场景及其原理详解
  • 828华为云征文|WordPress部署
  • jupyter安装与使用——Ubuntu服务器
  • 《工程科学与技术》
  • 上交所服务器崩溃:金融交易背后的技术隐患暴露杭州BGP高防服务器43.228.71.X
  • 设计模式之装饰模式(Decorator)
  • 数据结构-3.5.队列的顺序实现
  • 搭建高效知识库:教培机构数字教学的关键一步
  • 搭建本地AI聊天界面:Open WebUI与Ollama实战指南
  • 如何使用ssm实现北关村基本办公管理系统的设计与实现
  • 828华为云征文|华为云Flexus云服务器X实例Windows系统部署一键短视频生成AI工具moneyprinter
  • Xiaojie雷达之路---doa估计(dbf、capon、music算法)
  • Spring Security 是一个强大的和高度可定制的身份验证和访问控制框架。它是 Spring 项目家族的一员,用于构建安全的 Java 应用程序。
  • 代码随想录_刷题记录_第四次
  • [leetcode]216_组合总和III_给定数字范围且输出无重复
  • 怎么绕开华为纯净模式安装软件
  • 【球形空间产生器】
  • 泰特现代美术馆25年:那些瞬间,让艺术面向所有人
  • 会计江湖|年报披露关注什么:独董给出的“信号”
  • 保利42.41亿元竞得上海杨浦东外滩一地块,成交楼面单价超8万元
  • 异域拾异|大脚怪的形状:一项神秘社会学研究
  • 少年中国之少年的形塑
  • 加拿大总理访美与特朗普“礼貌交火”