1. 程式人生 > >使用poi導出Excel,並設定單元格內容類型,拋出異常

使用poi導出Excel,並設定單元格內容類型,拋出異常

sdro spa ppr 相同 值類型 setfill dropdown 整數 csdn

本例子使用的是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,並設定單元格內容類型,拋出異常