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

excel下拉框

java 复制代码
@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的复制

java 复制代码
@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列之间的级联

相关推荐
Python大数据分析@12 小时前
python操作CSV和excel,如何来做?
开发语言·python·excel
John.liu_Test12 小时前
js下载excel示例demo
前端·javascript·excel
ruleslol15 小时前
VBA02-初识宏——EXCEL录像机
excel·vba
IT铺子15 小时前
制定Excel使用规范和指导,提升数据处理的效率和准确性,减少错误和数据丢失的风险
excel
是萝卜干呀15 小时前
Backend - Python 爬取网页数据并保存在Excel文件中
python·excel·table·xlwt·爬取网页数据
Aloudata15 小时前
从Apache Atlas到Aloudata BIG,数据血缘解析有何改变?
大数据·apache·数据血缘·主动元数据·数据链路
神奇夜光杯20 小时前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
小c君tt21 小时前
MFC中Excel的导入以及使用步骤
c++·excel·mfc
小钱c71 天前
Mac下安装Apache JMeter并启动
jmeter·macos·apache
一名技术极客1 天前
Vue2 doc、excel、pdf、ppt、txt、图片以及视频等在线预览
pdf·powerpoint·excel·文件在线预览