使用poi导出Excel,并设定单元格内容类型,抛出异常
本例子使用的是HSSF,为Excel2003提供处理方案。
设定为输入类型为数值
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFDataValidationHelper;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.CellRangeAddressList;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType; /**
* 设定为输入类型为数值
* @param firstRow
* @param endRow
* @param firstCol
* @param endCol
* 注意:如果是一个单元格,需要firstRow = endRow, firstCol = endCol
* @return
*/
public static HSSFDataValidation setDataValidation(int firstRow,int endRow,int firstCol,int endCol)
{ CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
//数值型,大于0
DVConstraint constraint = DVConstraint.createNumericConstraint(ValidationType.DECIMAL, OperatorType.GREATER_THAN, "0", null);
//整数 1到100之间
// DVConstraint constraint = DVConstraint.createNumericConstraint(ValidationType.INTEGER, OperatorType.BETWEEN, "1", “100"); HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint);//add dataValidation.createErrorBox("输入值类型或大小有误", "数值型,请输入不小于0的数值");
dataValidation.createPromptBox("", null);
dataValidation.setShowErrorBox(true); return dataValidation;
}
设置为下拉列表选项
/**
* 添加数据有效性检查.
* @param sheet 要添加此检查的Sheet
* @param firstRow 开始行
* @param lastRow 结束行
* @param firstCol 开始列
* @param lastCol 结束列
* @param explicitListValues 有效性检查的下拉列表
* @throws IllegalArgumentException 如果传入的行或者列小于0(< 0)或者结束行/列比开始行/列小
* 注意: 如果是一个单元格,需要 firstRow = lastRow , firstCol= lastCol
*/
public static void setValidationData(Sheet sheet, int firstRow, int lastRow,
int firstCol, int lastCol,String[] explicitListValues) throws IllegalArgumentException{
if (firstRow < 0 || lastRow < 0 || firstCol < 0 || lastCol < 0 || lastRow < firstRow || lastCol < firstCol) {
throw new IllegalArgumentException("Wrong Row or Column index : " + firstRow+":"+lastRow+":"+firstCol+":" +lastCol);
}
if (sheet instanceof XSSFSheet) {
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet)sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
.createExplicitListConstraint(explicitListValues);
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
} else if(sheet instanceof HSSFSheet){
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(explicitListValues);
DataValidation validation = new HSSFDataValidation(addressList, dvConstraint);
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
}
}
设置模板文件的输入项表格样式
/**
* 设置模板文件的输入项表格样式
* @param wb
* @return
*/
public static CellStyle setValueStyle(Workbook wb) {
//The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook
CellStyle style = wb.createCellStyle();
//对齐方式设置
style.setAlignment(CellStyle.ALIGN_LEFT);
//边框颜色和宽度设置
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // style.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//设置背景颜色
// style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
style.setFillBackgroundColor(HSSFColor.LIGHT_TURQUOISE.index);
style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); //设置背景色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//设置自动换行
style.setWrapText(true);
return style;
}
POI HSSFColor 颜色索引对照表
参考:
Java读写Excel之POI超入门
POI生成excel带下拉
使用POI3.8 设置EXCEL2007的数据有效性
Apache POI使用详解
使用poi导出Excel,并设定单元格内容类型,抛出异常的更多相关文章
- java 使用poi导出Excel,设置单元格保护不可编辑
//sheet表加密:等效excel的审阅菜单下的保护工作表 sheet.protectSheet(new String("333"));//333是密码 更多设置请参考:http ...
- C#导出Excel,某单元格内容长度超过255 的解决方法
public static void ToExcel(DataTable dtSource, string strPath, string strSheetName) { System.Data.Ol ...
- poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)
POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...
- WPF 导出Excel(合并单元格)
WPF 导出Excel(合并单元格) DataTable 导出Excel(导出想要的列,不想要的去掉) ,B1,B2,B3,B4,B5} MisroSoft.Office.Interop.Excel. ...
- 转:jxl导出excel(合并单元格)
Demo 代码如下: import java.io.*; import jxl.*; import jxl.format.UnderlineStyle; import jxl.write.*; pub ...
- EXCEL中统计单元格内容出现次数
参考网站: https://jingyan.baidu.com/article/7c6fb428dfcc9580642c90ae.html 统计单元格内容出现次数是工作中经常会涉及到的问题. 那么,如 ...
- 导出excel带合并单元格方法的Demo
package com.test.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import ...
- java poi操作excel 添加 锁定单元格保护
Excel的book保护是很常用的,主要是不想让别人修改Excel的时候用.这样能够避免恶意随便修改数据,提高数据的可信度. 下面介绍JAVA POI来实现设置book保护: 使用HSSFSheet类 ...
- poi操作Excel并修改单元格背景色
废话不多说,直接来代码!!! 其中标红的才是重点!!! 代码中有时可以不用创建新文件, 如果报错的话可以通过创建新文件来进行操作(懒,没去找报错原因),不过原文件也会被修改. 操作之前做好备份!操作之 ...
随机推荐
- cocoapod 快速更新,加载
pod install --verbose --no-repo-update pod update --verbose --no-repo-update
- SNMP学习笔记之SNMPv3报文认证和加密
下面主要的内容就是SNMPv3的加密和认证过程! USM的定义为实现以下功能: 鉴别 数据加密 密钥管理 时钟同步化 避免延时和重播攻击 1.UsmSecurityParameters(安全参数) 安 ...
- P4009 汽车加油行驶问题
P4009 汽车加油行驶问题 最短路 清一色的spfa....送上一个堆优化Dijkstra吧(貌似代码还挺短) 顺便说一句,堆优化Dj跑分层图灰常好写 #include<iostream> ...
- Tomcat上发布webservices的war工程,访问异常404
Tomcat上发布webservices的war工程,访问异常404 Tomcat部署正常.war导出工程正常.Tomcat自带的工程可以正常访问: 问题: webservices工程访问异常404 ...
- 20145336 张子扬 《网络对抗技术》web基础
20145336张子扬<网络对抗>Exp8 Web基础 实践内容 1.简单的web前端页面(HTML.CSS等) 2.简单的web后台数据处理(PHP) 3.Mysql数据库 4.一个简单 ...
- 什么是BFC?
转载自知乎:https://zhuanlan.zhihu.com/p/25321647 一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: ...
- visual studio扩展插件Visual Assist x给代码插入注释模板(转载)
转载:http://www.cnblogs.com/xiongmao-cpp/p/5196555.html Visual Assist 是由Whole Tomato公司为Microsoft Visua ...
- centos7 + mysql5.7 tar包解压安装
#卸载系统自带的Mariadb [root@hdp265dnsnfs ~]# rpm -qa|grep mariadb mariadb-libs--.el7.centos.x86_64 [root@h ...
- system.data.sqlite的源代码下载
帮助文档 http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki 历史版本https://system.data.sqlit ...
- 【附3】springboot源码解析 - 构建SpringApplication
package com.microservice.framework; import org.springframework.boot.SpringApplication; import org.sp ...