码迷,mamicode.com
首页 > 其他好文 > 详细

使用poi导出Excel,并设定单元格内容类型,抛出异常

时间:2017-10-19 19:38:31      阅读:608      评论:0      收藏:0      [点我收藏+]

标签:ems   wrap   rri   util   round   fine   cee   url   list   

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

 

设置模板文件的输入项表格样式

        /**
         * 设置模板文件的输入项表格样式
     * 注意不要频繁的createCellStyle对象,声明一个CellStyle提供给相同的Cell即可,
* 否则容易造成 4000 styles异常
*
@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.BROWN.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BROWN.getIndex()); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BROWN.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BROWN.getIndex()); style.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); //设置背景颜色 style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置自动换行 style.setWrapText(true); return style; }

 

参考:

使用POI为Excel添加数据有效性验证

Java读写Excel之POI超入门

POI生成excel带下拉

使用POI3.8 设置EXCEL2007的数据有效性

使用poi导出Excel,并设定单元格内容类型,抛出异常

标签:ems   wrap   rri   util   round   fine   cee   url   list   

原文地址:http://www.cnblogs.com/huanghongbo/p/7694091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!