博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POI设置单元格内容下拉框选择和单元格提示功能
阅读量:4698 次
发布时间:2019-06-09

本文共 2460 字,大约阅读时间需要 8 分钟。

import java.io.FileOutputStream;

import java.io.IOException;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddressList;
public class PoiTest2 {
    public static void main(String[] args) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();// excel文件对象
        HSSFSheet sheetlist = wb.createSheet("sheetlist");// 工作表对象
        FileOutputStream out = new FileOutputStream("d:\\success.xls");
        String[] textlist = { "列表1", "列表2", "列表3", "列表4", "列表5" };
        sheetlist = setHSSFValidation(sheetlist, textlist, 0, 500, 0, 0);// 第一列的前501行都设置为选择列表形式.
        // sheetlist = setHSSFPrompt(sheetlist, "promt Title", "prompt Content",
        // 0, 500, 1, 1);// 第二列的前501行都设置提示.
        wb.write(out);
        out.close();
    }
    /**
     * 设置某些列的值只能输入预制的数据,显示下拉框.
     *
     * @param sheet
     *            要设置的sheet.
     * @param textlist
     *            下拉框显示的内容
     * @param firstRow
     *            开始行
     * @param endRow
     *            结束行
     * @param firstCol
     *            开始列
     * @param endCol
     *            结束列
     * @return 设置好的sheet.
     */
    public static HSSFSheet setHSSFValidation(HSSFSheet sheet,
            String[] textlist, int firstRow, int endRow, int firstCol,
            int endCol) {
        // 加载下拉列表内容
        DVConstraint constraint = DVConstraint
                .createExplicitListConstraint(textlist);
        // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow,
                endRow, firstCol, endCol);
        // 数据有效性对象
        HSSFDataValidation data_validation_list = new HSSFDataValidation(
                regions, constraint);
        sheet.addValidationData(data_validation_list);
        return sheet;
    }
    /**
     * 设置单元格上提示
     *
     * @param sheet
     *            要设置的sheet.
     * @param promptTitle
     *            标题
     * @param promptContent
     *            内容
     * @param firstRow
     *            开始行
     * @param endRow
     *            结束行
     * @param firstCol
     *            开始列
     * @param endCol
     *            结束列
     * @return 设置好的sheet.
     */
    public static HSSFSheet setHSSFPrompt(HSSFSheet sheet, String promptTitle,
            String promptContent, int firstRow, int endRow, int firstCol,
            int endCol) {
        // 构造constraint对象
        DVConstraint constraint = DVConstraint
                .createCustomFormulaConstraint("BB1");
        // 四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow,
                endRow, firstCol, endCol);
        // 数据有效性对象
        HSSFDataValidation data_validation_view = new HSSFDataValidation(
                regions, constraint);
        data_validation_view.createPromptBox(promptTitle, promptContent);
        sheet.addValidationData(data_validation_view);
        return sheet;
    }
}

转载于:https://www.cnblogs.com/duguheilang/p/5282460.html

你可能感兴趣的文章
磁盘使用率达到100%
查看>>
linux跳过root密码登陆
查看>>
mini2440 U-boot 编译
查看>>
在UTF-8中,一个汉字为什么需要三个字节?
查看>>
学习ThreadLocal
查看>>
在 Visual Studio 调试器中指定符号 (.pdb) 和源文件
查看>>
直接量
查看>>
leetcode 115. 不同的子序列(Distinct Subsequences)
查看>>
三元表达式
查看>>
Go初接触之libjpeg-turbo
查看>>
UVa 11300 Spreading the Wealth 分金币
查看>>
[leetcode] Happy Number
查看>>
Java第五周学习总结
查看>>
j.c.Warnsdorff马踏棋盘算法
查看>>
the openning
查看>>
python 字符串 和 print
查看>>
MAC OS下安装Minizip
查看>>
Java_Certificates does not conform to algorithm constraints
查看>>
PAT 1027. Colors in Mars
查看>>
linux定时执行脚本
查看>>