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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM