前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POI 导出excel文件下拉框问题

POI 导出excel文件下拉框问题

作者头像
周杰伦本人
发布2023-10-12 14:14:21
4070
发布2023-10-12 14:14:21
举报
文章被收录于专栏:同步文章同步文章

今天捣鼓那个下拉框呢 这个Workbook有三个实现类

这里写图片描述
这里写图片描述

首先po一个xssf的下拉框实现工具类:

代码语言:javascript
复制
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;

/**
 * describe:
 *
 * @author xiepanpan
 * @date 2018/07/11
 */
public class Test {

    public static void main(String[] args) {
        try
        {
            dropDownList42007("D:\\test.xlsx");
        }
        catch (Exception e) {

            e.printStackTrace();
        }
    }


    public static void dropDownList42007(String filePath)
            throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("下拉列表测试");
        String[] datas = new String[] {"维持","恢复","调整"};
        XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
        XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
                .createExplicitListConstraint(datas);
        CellRangeAddressList addressList = null;
        XSSFDataValidation validation = null;
        for (int i = 0; i < 100; i++) {
            addressList = new CellRangeAddressList(i, i, 0, 0);
            validation = (XSSFDataValidation) dvHelper.createValidation(
                    dvConstraint, addressList);
            // 07默认setSuppressDropDownArrow(true);
            // validation.setSuppressDropDownArrow(true);
            // validation.setShowErrorBox(true);
            sheet.addValidationData(validation);
        }
        FileOutputStream stream = new FileOutputStream(filePath);
        workbook.write(stream);
        stream.close();
        addressList = null;
        validation = null;
    }
}

HSSFWorkbook 的下拉框设置:

代码语言:javascript
复制
//加载下拉列表内容
        DVConstraint constraint = DVConstraint.createExplicitListConstraint(formulaString);
        //设置数据有效性加载在哪个单元格上。
        //四个参数分别是:起始行、终止行、起始列、终止列
        int firstRow = naturalRowIndex-1;
        int lastRow = naturalRowIndex-1;
        int firstCol = naturalColumnIndex-1;
        int lastCol = naturalColumnIndex-1;
        CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);
        //数据有效性对象
        DataValidation dataValidation = new HSSFDataValidation(regions,constraint);
        sheet.addValidationData(dataValidation);

SXSSFWorkbook的下拉框设置方法:

代码语言:javascript
复制
/**
     * 设置下拉框
     * @param formulaString 下拉框数组
     * @param firstRow 起始行 从0开始
     * @param lastRow 终止行
     * @param firstCol 起始列
     * @param lastCol 终止列
     * @param <E>
     * @return
     */
    public <E> MyExportExcel setDropDownBox(String[] formulaString,int firstRow, int lastRow,int firstCol,int lastCol){
        //加载下拉列表内容
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createExplicitListConstraint(formulaString);
        //设置下拉框位置
        CellRangeAddressList addressList = null;
        addressList = new CellRangeAddressList(firstRow, 500, 1, 1);
        DataValidation dataValidation = helper.createValidation(constraint, addressList);
        //处理Excel兼容性问题
        if(dataValidation instanceof XSSFDataValidation){
            //数据校验
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        }else{
            dataValidation.setSuppressDropDownArrow(false);
        }
        sheet.addValidationData(dataValidation);
        return this;
    }

效果:

这里写图片描述
这里写图片描述

至于表头为啥整这么炫,是因为我用了jeeplus框架 参考博客: https://blog.csdn.net/u014792342/article/details/47973087?locationNum=13 http://www.it610.com/article/3597562.htm http://wuhaidong.iteye.com/blog/2039848 https://my.oschina.net/sxxachao/blog/607322

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档