基本的过程就是设置一个下拉选择框的校验,然后绑定到sheet的指定格子上。XSS和HSS的方式有些许不同,但是原理是一样的。代码示例如下:
//性别
String[] datas = new String[]{"男","女"};
if(excelType.equals(ExcelTypeEnum.XLSX)){
//数据验证帮助程序
XSSFDataValidationHelper dvHelper2 = new XSSFDataValidationHelper((XSSFSheet) sheet);
XSSFDataValidationConstraint dvConstraint2 = (XSSFDataValidationConstraint) dvHelper2.createExplicitListConstraint(datas);
CellRangeAddressList regions2 =new CellRangeAddressList(1, 5000, 2, 2);
XSSFDataValidation dataValidation2 = (XSSFDataValidation) dvHelper2.createValidation( dvConstraint2, regions2);
sheet.addValidationData(dataValidation2);
}else{
// 设置第5列的2-5000行为下拉列表
CellRangeAddressList regions2 = new CellRangeAddressList(1, 5000, 2, 2);
// 创建下拉列表数据
DVConstraint constraint2 = DVConstraint.createExplicitListConstraint(datas);
// 绑定
HSSFDataValidation dataValidation2 = new HSSFDataValidation(regions2, constraint2);
sheet.addValidationData(dataValidation2);
}
------------
完整的一个示例代码如下。导入包后可以直接运行实验。
package com.insigma;
import java.io.FileOutputStream;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelTest {
public static void main(String[] args) {
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 = new CellRangeAddressList(0, 100, 0, 0);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation( dvConstraint, addressList);
sheet.addValidationData(validation);
FileOutputStream stream = new FileOutputStream(filePath);
workbook.write(stream);
stream.close();
}
-----------------
学如逆水行舟,不进则退
java queue 数组实现 java数组实例
数组排序及元素查找数组添加元素获取数组长度数组反转数组输出数组获取最大和最小值数组合并数组填充数组扩容查找数组中的重复元素删除数组元素数组差集数组交集数组并集在数组中查找指定元素判断数组是否相等import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Has
1.7-16| *8 directory index of "E:\wwwroot/" is forbidden, client: 127.0.0.1, server: 192.168.1.240, re
2.Mysql进阶篇(二)之索引
3.m基于FPGA的带相位偏差64QAM调制信号相位估计和补偿算法verilog实现,包含testbench
4.文心一言 VS 讯飞星火 VS chatgpt (60)-- 算法导论6.4 4题
5.从0到1:跑团小程序开发心得笔记