使用poi导出Excel,并设定单元格内容类型,抛出异常
2017-10-19 18:23
531 查看
本例子使用的是HSSF,为Excel2003提供处理方案。
设定为输入类型为数值
设置为下拉列表选项
设置模板文件的输入项表格样式
使用POI为Excel添加数据有效性验证
设定为输入类型为数值
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 颜色索引对照表
参考:使用POI为Excel添加数据有效性验证
Java读写Excel之POI超入门
POI生成excel带下拉
使用POI3.8 设置EXCEL2007的数据有效性
Apache POI使用详解
相关文章推荐
- POI导出Excel时设置单元格类型为数值类型,消除左上小三角
- Struts2使用Displaytag导出Excel,抛出nullException异常的处理
- java使用poi导出excel的内容,同时可以利用反射进行动态获取信息
- poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出
- JAVA使用poi进行EXCEL模板导入导出,XSSFCell数据类型
- 使用POI设置导出的EXCEL锁定指定的单元格
- 【POI框架实战】——POI导出Excel时设置单元格类型为数值类型
- java 使用poi导出Excel,设置单元格保护不可编辑,设置下拉框
- 使用poi导入、导出Excel内容
- POI获取Excel中单元格内容时,如果为数字格式获取类型为double的问题
- Excel在vc中怎么操作单元格的字体、颜色、加线框(和异常的抛出)
- 如何使用POI处理Excel中的日期数据类型
- c#使用CsGL时一个小错误:“CsGL.OSLib”的类型初始值设定项引发异常 产生的原因
- jxls--使用模版导出excel,单元格无法自动计算问题解决
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- jsp导出excel文件并设定单元格格式
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- 使用BindingSource发生错误:Microsoft.VisualStudio.DataDesign.SyncDesigner.SyncFacade.SyncTableConfigManager 的类型初始值设定项引发异常
- [jxcell]使用jxcell导入excel中重命名的单元格内容
- 关于导出数据到Excel时,单元格数据类型的控制