使用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 颜色索引对照表

 

参考:

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

Java读写Excel之POI超入门

POI生成excel带下拉

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

 

Apache POI使用详解

 

转载于:https://www.cnblogs.com/huanghongbo/p/7694091.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值