java生成execl下拉列表

来源:互联网 发布:apache运行php文件 编辑:程序博客网 时间:2024/05/29 09:35


java生成Execl下拉列表模板文件


package cn.px.irs.pl.web;

import java.io.FileOutputStream;


import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddressList;


public class ExeclDown {


  public static void main(String[] args) {  
    HSSFWorkbook wb = new HSSFWorkbook();  
    HSSFSheet sheet = wb.createSheet("new sheet");  
    // 在第一行第一个单元格,插入下拉框  
    HSSFRow row = sheet.createRow(0);  
    HSSFCell cell = row.createCell(5);  
    HSSFCell cell2 = row.createCell(7);  
    HSSFCell cell3 = row.createCell(8);  
    // 普通写入操作  
    cell.setCellValue("请选择");// 这是实验  
    cell2.setCellValue("请选择2");// 这是实验  
    cell3.setCellValue("请选择3");// 这是实验  
    // 生成下拉列表  
    // 只对(0,0)单元格有效  
    
 // 四个参数分别是:起始行、终止行、起始列、终止列  
    CellRangeAddressList regions = new CellRangeAddressList(0, 65535, 5, 5);  
    CellRangeAddressList regions2 = new CellRangeAddressList(0, 65535, 7, 8);  
    // 生成下拉框内容  
    DVConstraint constraint = DVConstraint.createExplicitListConstraint(new String[] { "积分卡", "优惠卡","VIP卡","金卡","银卡"});  
    DVConstraint constraint2 = DVConstraint.createExplicitListConstraint(new String[] { "积分卡2", "优惠卡2","VIP卡2","金卡2","银卡2"});  
    // 绑定下拉框和作用区域  
    HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);  
    HSSFDataValidation data_validation2 = new HSSFDataValidation(regions2,constraint2);  
    // 对sheet页生效  
    sheet.addValidationData(data_validation);  
    sheet.addValidationData(data_validation2);  
    // 写入文件  
    FileOutputStream fileOut;  
    try {  
        fileOut = new FileOutputStream("workbook.xls");  
        wb.write(fileOut);  
        fileOut.close();  
    } catch (Exception e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  


    }  
    // 结束  
    System.out.println("Over");  


}  
}

0 0
原创粉丝点击