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;

  /**
   * 思路:
   * 1.工作簿
   * 2.下拉框位置
   * 3.下拉框数据
   * @param filePath
   * @throws Exception
   */

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; } }

 

posted on 2018-07-31 14:07  大山008  阅读(3487)  评论(0编辑  收藏  举报