poi建立下拉框
package com.cloud.poi.utils;import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.DVConstraint;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDataValidation;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.CellRangeAddressList;import org.apache.poi.ss.SpreadsheetVersion;/** * poi建立下拉框 * @author Henry */public class PoiCheckBox97{public static void main(String[] args){String[] list = { "JAVA", "C", "易語言", "GO" };new PoiCheckBox97().createListBox(list);return;}public void createListBox(String[] list){// 檔案初始化HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");// 在第一行第一個儲存格,插入下拉框HSSFRow row = sheet.createRow(0);HSSFCell cell = row.createCell(0);// 普通寫入操作cell.setCellValue("請選擇");// 這是實驗// 產生下拉式清單// 對(row0,rowMax,col0,colMax)所有儲存格有效int maxRowNum = SpreadsheetVersion.EXCEL97.getLastRowIndex();//maxRowNum=65535System.out.println("maxRowNum:"+maxRowNum);int maxColNum = SpreadsheetVersion.EXCEL97.getLastColumnIndex();System.out.println("maxColNum:"+maxColNum);//maxColNum=255CellRangeAddressList regions = new CellRangeAddressList(0, maxRowNum, 0, maxColNum);// 產生下拉框內容DVConstraint constraint = DVConstraint.createExplicitListConstraint(list);// 綁定下拉框和作用地區HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);// 對sheet頁生效sheet.addValidationData(data_validation);// 寫入檔案FileOutputStream fileOut;try {fileOut = new FileOutputStream("d:/workbook.xls");wb.write(fileOut);fileOut.close();} catch (Exception e) {e.printStackTrace();}// 結束System.out.println("Over");}}