002-poi-excel-导出设置单元格数据校验规则、筛选功能
一、数据验证概述
推荐以下操作在2007之后操作
1.1、查看excel的数据验证
1、进入

2、设置规则

通过验证条件允许,可以看到是每个单元格默认只成立一种条件
1.2、POI代码开发-数据验证
1.2.1、两个数之间
public void excelRuleNumberBetween(Sheet sheet, int min, int max, int firstRow, int lastRow, int firstCol, int lastCol){
DataValidationHelper helper = sheet.getDataValidationHelper();
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);//设置行列范围
//设置数据
DataValidationConstraint constraint = helper.createIntegerConstraint(DataValidationConstraint.OperatorType.BETWEEN,
String.valueOf(min),String.valueOf(max));
DataValidation dataValidation = helper.createValidation(constraint, addressList);
dataValidation.createErrorBox("输入值类型或大小有误", String.format("请输入%s~%s之间的数值",min,max));
//处理Excel兼容性问题
if(dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
}else {
dataValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(dataValidation);
}
1.2.2、选择【序列】
public void excelRuleSelect(Sheet sheet, String[] rule, int firstRow, int lastRow, int firstCol, int lastCol) {
DataValidationHelper helper = sheet.getDataValidationHelper();
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
DataValidationConstraint constraint = helper.createExplicitListConstraint(rule);
DataValidation dataValidation = helper.createValidation(constraint, addressList);
dataValidation.createErrorBox("输入有误", "请选择下拉参数");
if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
} else {
dataValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(dataValidation);
}
1.2.3、列唯一
使用excel设置

POI设置
public void excelRuleUniqueue(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
Row row = sheet.getRow(0);
Cell cell = row.getCell(firstCol);
String r = ((XSSFCell) cell).getCTCell().getR();
r = r.substring(0, 1);
DataValidationHelper helper = sheet.getDataValidationHelper();
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
//唯一
DataValidationConstraint constraint = helper.createCustomConstraint(MessageFormat.format("COUNTIF({0}:{0},{0}2)=1",r));
DataValidation dataValidation = helper.createValidation(constraint, addressList);
dataValidation.createErrorBox("错误:", "赋值属性列不允许重复");
dataValidation.setShowErrorBox(true);
dataValidation.setEmptyCellAllowed(true);
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowPromptBox(true);
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
sheet.addValidationData(dataValidation);
}
二、其他
2.1、列筛选
查看excel实现

POI代码
Sheet sheetCreat = wbCreat.createSheet(sheet.getSheetName());
CellRangeAddress c = CellRangeAddress.valueOf(CELL_RANGE_ADDRESS);
sheetCreat.setAutoFilter(c);
002-poi-excel-导出设置单元格数据校验规则、筛选功能的更多相关文章
- [转载]Java读取Excel中的单元格数据
目前网上能找到的读取Excel表格中数据的两种比较好的方案:PageOffice好用开发效率高:POI免费.供大家参考,针对具体情况选择具体方案. 1. PageOffice读取excel impor ...
- jqgrid设置单元格数据
$("#gridid").jqGrid('setCell',rowid,icol,data); rowid为行ID,jqgrid内置的那个,从1开始 icol为列索引,从0开始, ...
- .Net 导出Excel时设置单元格的格式为文本类型
<td style= 'vnd.ms-excel.numberformat:@ ' align='right'>" & Format(Val(rowTitle.Item( ...
- 【手记】解决excel无法设置单元格颜色且界面怪异+桌面图标文字老有色块等问题
注:问题是在XP上遇到的,不知道是否适用其它系统 问题现象 excel 2010成这样了: 关键是设置不了单元格颜色,无论是文字颜色还是背景色都设置不了,设了没变化.同时会发现桌面图标的文字总有底色: ...
- Excel公式设置单元格颜色
Excel2010 “条件格式"-"新建规则"-"使用公式确定要设置格式的单元格" 公式如下: =OR(H2<=-20%,H2>=20%, ...
- java 使用poi导出Excel,设置单元格保护不可编辑
//sheet表加密:等效excel的审阅菜单下的保护工作表 sheet.protectSheet(new String("333"));//333是密码 更多设置请参考:http ...
- thinkphp3.2.3集成phpexcel1.8导出设置单元格合并
1 到这里下载classes里面的文件 https://github.com/PHPOffice/PHPExcel 2 然后放到 thinkphp的vendor 新建一个文件夹 Phpexcel 然 ...
- asp.net+nopi生成Excel遇到设置单元格值null问题
Npoi 生成excel报表功能很不错,功能也不用给大家介绍了.首先看遇到的问题吧! FileStream file = new FileStream(Server.MapPath("Tem ...
- C#导出Excel按照指定格式设置单元格属性值
最近项目中一直在写XML.Table.Excel之间的转化.之前一直都是不考虑格式的导出,今天给出一个格式,让按照格式导出,还真把我这新手为难了一翻,网上给出的资料基本一样.为了一个单元格文字变色纠结 ...
随机推荐
- CUDA中确定你显卡的thread和block数
CUDA中确定你显卡的thread和block数 在进行并行计算时, 你的显卡所支持创建的thread数与block数是有限制的, 因此, 需要自己提前确定够用, 再进行计算, 否则, 你需要改进你的 ...
- Codeforces #369 (Div. 2) C. Coloring Trees (3维dp
http://codeforces.com/group/1EzrFFyOc0/contest/711/problem/C https://blog.csdn.net/qq_36368339/artic ...
- linux网络编程之socket编程(九)
转眼又快到十一月份了,北京已经是完全进入冬天的节奏,外面冷风嗖嗖的,不过在夜深人静之时,学习永远成了我最快乐的时光,只有此时会觉得自己是如此踏实,虽说白天工作也是编一天程,但是此时的编程,是一种业余爱 ...
- Java中ClassLoader浅析.
一.问题 请在Eclipse中新建如下类,并运行它: 1 package java.lang; 2 3 public class Long { 4 public static void main(St ...
- 如何在vscode中用standard style 风格去验证 vue文件
1 JavaScript Standard Style简介 本工具通过以下三种方式为你(及你的团队)节省大量时间: 无须配置. 史上最便捷的统一代码风格的方式,轻松拥有. 自动代码格式化. 只需运行 ...
- jQuery模拟键盘打字逐字逐句显示文本
jQuery模拟键盘打字逐字逐句显示文本 html代码 <!doctype html> <html lang="zh"> <head> < ...
- java.sql.SQLException: connection holder is null;
一.问题来源分析 出现的错误 : Cause: java.sql.SQLException: connection holder is null; uncategorized SQLException ...
- Spring security invalid-session-url 的坑(配了permitAll仍然跳转到登录页)
Spring security session配置中如果配了如下的invalid-session-url,配置了permitAll链接首次链接系统时会跳转到登录页,将该配置删除即可解决此问题. < ...
- 005_Python3 运算符
什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算 ...
- learning scala type alise
How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, ...