poi 3.2 动态设定excle单元格格式

例如:selet

import java.io.FileOutputStream;
import java.util.Date;

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 TestPOI
{
    public static void main(String[] args)
    {
        String[] list = {"a", "b", "c"};
        createListBox(list);
        return;
    }
    
   public static void createListBox(String[] list)
    {
        //文件初始化
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("test");
        String dateformat = "yyyy-MM-dd";
        String s="yyyy-MM-dd HH:mm:ss";

        //在第一行第一个单元格,插入下拉框
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        //在第一行第二个单元格,插入日期
       
       
        //生成下拉列表
        //四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(0, 1001, 0, 0);
        // CellRangeAddressList regions = new CellRangeAddressList();
        //只对(0,1)单元格有效
        CellRangeAddressList regions1 = new CellRangeAddressList(1, 1001, 1, 1);
       
       
       
        //生成下拉框内容
        DVConstraint constraint = DVConstraint.createExplicitListConstraint(list);
       
        //生成日期
        DVConstraint constraint1 = DVConstraint.createDateConstraint(DVConstraint.OperatorType.BETWEEN,
                "2009-01-01",
                "2009-11-01",
                dateformat);
       
        //绑定下拉框和作用区域 .并添加输入内容提示
        HSSFDataValidation data_validation = new HSSFDataValidation(regions,
                constraint);
        data_validation.createPromptBox("tip","select");  
     
        //绑定日期和作用区域.并添加输入内容提示
        HSSFDataValidation date_validation = new HSSFDataValidation(regions1,
                constraint1);
        date_validation.createPromptBox("tip","date between  2009-01-01 and 2009-11-01");  
       
        //对sheet页生效
        sheet.addValidationData(data_validation);
        sheet.addValidationData(date_validation);
      
        //写入文件
        FileOutputStream fileOut;
       
        try
        {
            fileOut = new FileOutputStream("c:\\testworkbook.xls");
            wb.write(fileOut);
            fileOut.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
      
    }
}

你可能感兴趣的:(apache,C++,c,C#)