poi XSSFWorkbook实现excel中的下拉框

package …
import java.io.FileOutputStream;

import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.huawei.bme.commons.util.debug.DebugLog;
import com.huawei.bme.commons.util.debug.LogFactory;

public class Test {

public static void main(String[] args) {
    try 
    {
        dropDownList42007("D:\\test.xlsx");
    } 
    catch (Exception e) {

        e.printStackTrace();
    }
}


public static void dropDownList42007(String filePath)
        throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("下拉列表测试");
    String[] datas = new String[] {"维持","恢复","调整"};
    XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
    XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
            .createExplicitListConstraint(datas);
    CellRangeAddressList addressList = null;
    XSSFDataValidation validation = null;
    for (int i = 0; i < 100; i++) {
        addressList = new CellRangeAddressList(i, i, 0, 0);
        validation = (XSSFDataValidation) dvHelper.createValidation(
                dvConstraint, addressList);
        // 07默认setSuppressDropDownArrow(true);
        // validation.setSuppressDropDownArrow(true);
        // validation.setShowErrorBox(true);
        sheet.addValidationData(validation);
    }
    FileOutputStream stream = new FileOutputStream(filePath);
    workbook.write(stream);
    stream.close();
    addressList = null;
    validation = null;
}

}

poi XSSFWorkbook实现excel中的下拉框_第1张图片

你可能感兴趣的:(项目开发)