使用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并修改单元格背景色
废话不多说,直接来代码!!! 其中标红的才是重点!!! 代码中有时可以不用创建新文件, 如果报错的话可以通过创建新文件来进行操作(懒,没去找报错原因),不过原文件也会被修改. 操作之前做好备份!操作之 ...
随机推荐
- web前端----JavaScript的DOM(一)
一.什么是HTML DOM HTML Document Object Model(文档对象模型) HTML DOM 定义了访问和操作HTML文档的标准方法 HTML DOM 把 HTML 文档呈现 ...
- php随笔10-thinkphp 3.1.3 模板继承 布局
8.25 模板继承 模 板继承是3.1.2版本添加的一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类 的继承一样,模板也可以定义一个 ...
- 负载均衡之-haproxy
老规矩,先介绍?复制一段? 1)HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. 2)HAProxy特别适用于那些负载特大的 ...
- Python3.x:抓取百事糗科段子
Python3.x:抓取百事糗科段子 实现代码: #Python3.6 获取糗事百科的段子 import urllib.request #导入各类要用到的包 import urllib import ...
- 20145101《Java程序设计》第8周学习总结
20145101<Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了clear(),rew ...
- 20145127《java程序设计》第二次实验
一.实验内容及其步骤 1.要想对某个程序进行单元测试,我们先是在eclipse中建立了一个新的项目,项目的名字是TDDDmeo.并在这个新的项目里右键单击创建一个source floder.并将flo ...
- Codeforces Round #429 (Div. 2)
A. Generous Kefa One day Kefa found n baloons. For convenience, we denote color of i-th baloon as ...
- BZOJ1632: [Usaco2007 Feb]Lilypad Pond SPFA+最短路计数
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- ElasticSearch 5.4 安装
1. 前期准备 环境准备 IP地址 操作系统 内存 192.168.1.10 centos 7 16 192.168.1.11 centos 7 16 192.168.1.12 centos ...
- Network Simulator for P4(NSP4) src内容介绍
Structure What's NSP4? src source code introduction What's NSP4? NSP4是一个用于P4的网络仿真工具,旨在简化P4的环境部署和运行,将 ...