java输出excel 异常处理_使用poi导出Excel,并设定单元格内容类型,抛出异常

这篇博客介绍了如何使用Apache POI库在Java中处理Excel,特别是设定单元格为数值类型并进行数据验证,包括创建大于0的数值约束。同时,也展示了设置下拉列表选项和单元格样式的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例子使用的是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;

}

参考:

java 使用poi导出Excel,设置单元格保护不可编辑

//sheet表加密:等效excel的审阅菜单下的保护工作表 sheet.protectSheet(new String("333"));//333是密码 更多设置请参考:http ...

C&num;导出Excel,某单元格内容长度超过255 的解决方法

public static void ToExcel(DataTable dtSource, string strPath, string strSheetName) { System.Data.Ol ...

poi 升级至4&period;x 的问题总结&lpar;POI Excel 单元格内容类型判断并取值&rpar;

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 统计单元格内容出现次数是工作中经常会涉及到的问题. 那么,如 ...

java poi操作excel 添加 锁定单元格保护

Excel的book保护是很常用的,主要是不想让别人修改Excel的时候用.这样能够避免恶意随便修改数据,提高数据的可信度. 下面介绍JAVA POI来实现设置book保护: 使用HSSFSheet类 ...

poi操作Excel并修改单元格背景色

废话不多说,直接来代码!!! 其中标红的才是重点!!! 代码中有时可以不用创建新文件, 如果报错的话可以通过创建新文件来进行操作(懒,没去找报错原因),不过原文件也会被修改. 操作之前做好备份!操作之 ...

&period;Net 导出Excel时设置单元格的格式为文本类型

" & Format(Val(rowTitle.Item( ...

随机推荐

【wikioi】1029 遍历问题

题目链接:http://www.wikioi.com/problem/1029/ 算法:数学 本题有个2小技巧. 一棵二叉树的前序遍历a1a2a3...ai和后序遍历b1b2b3...bi有一种关系: ...

【jmeter】参数化User Defined Variables与User Parameters

偶然发现JMeter中有两个元件(User Defined Variables与User Parameters)很相近,刚开始时我也没注意,两者有什么不同.使用时却发现两者使用场景有些不同,现在小结一 ...

atcoder它A Mountaineer

Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Problem Dave is a mountaineer. He is ...

p便签,去掉首行缩进

fdsfdsfs

使用P标签是,会自动的加上首行缩进,如果想去掉首行缩进,可以使用text-indent属性

AHOI(十二省联考)2019 退役记

我也想退役失败.jpg Day 0 我才知道联考原来是4.5h? 下午居然还有讲题,感觉变得正规多了. 试机敲了LCT,NTT,SA,加起来花了大概40min,基本1A,感觉海星.键盘似乎有点过于灵敏 ...

php 自定义 分页函数

正则表达式利用grep和sed处理日志内容,获取所需的内容

app.log文件内容: 2014-09-11 00:00:01,516 INFO [com.tt.bb.thread.Control] - Socket连接:/182.105.83.33:53217 ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值