天天看點

poi 下拉框實作

需要導入jar包
      
import java.io.FileOutputStream;

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.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;      
public class ExcelTest {
    public static void main(String[] args) {
        try 
        {
            dropDownList42007("E:\\test.xlsx");
        } 
        catch (Exception e) {

            e.printStackTrace();
        }
    }


    /**
public static void dropDownList42007(String filePath)
            throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("test");
        String [] subjects = new String []{"JAVA","C++","JS"};
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createExplicitListConstraint(subjects);
        CellRangeAddressList addressList = null;
        DataValidation dataValidation = null;
        for (int i = 0; i < 100; i++) {
            addressList = new CellRangeAddressList(i, i, 0, 0);
            dataValidation = helper.createValidation(constraint, addressList);
            sheet.addValidationData(dataValidation);
        }
        FileOutputStream stream = new FileOutputStream(filePath);
        workbook.write(stream);
        stream.close();
        addressList = null;
        dataValidation = null;
    }
}      

轉載于:https://www.cnblogs.com/person008/p/9395531.html