读书人

poi数据有效性上拉列表

发布时间: 2012-10-11 10:16:10 作者: rapoo

poi数据有效性下拉列表

定义函数

?

public HSSFDataValidation SetDataValidation(String[] textList, int firstRow, int endRow, int firstCol, int endCol) {// 设置下拉列表的内容log.info("起始行:" + firstRow + "___起始列:" + firstCol + "___终止行:" + endRow + "___终止列:" + endCol);// 加载下拉列表内容DVConstraint constraint = DVConstraint.createExplicitListConstraint(textList);// DVConstraint constraint = new DVConstraint();constraint.setExplicitListValues(textList);// 设置数据有效性加载在哪个单元格上。// 四个参数分别是:起始行、终止行、起始列、终止列CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol);// 数据有效性对象HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);return data_validation;}

?调用类

import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class Test {public static void main(String[] args) throws IOException {HSSFWorkbook wb = new HSSFWorkbook();// excel文件对象HSSFSheet sheetlist = wb.createSheet("sheetlist");// 工作表对象FileOutputStream out = new FileOutputStream("success.xls");String[] textList = { "空闲1", "已用2" };sheetlist.addValidationData((new OracleToExcel()).SetDataValidation(textList, 1,2, 3, 4));wb.write(out);out.close();}}
?

?

读书人网 >编程

热点推荐