easypoi生成带下拉框的excel

  1. package com.test;  
  2.   
  3. import java.io.FileOutputStream;  
  4.   
  5. import org.apache.poi2.hssf.usermodel.DVConstraint;  
  6. import org.apache.poi2.hssf.usermodel.HSSFCell;  
  7. import org.apache.poi2.hssf.usermodel.HSSFDataValidation;  
  8. import org.apache.poi2.hssf.usermodel.HSSFRow;  
  9. import org.apache.poi2.hssf.usermodel.HSSFSheet;  
  10. import org.apache.poi2.hssf.usermodel.HSSFWorkbook;  
  11. import org.apache.poi2.hssf.util.CellRangeAddressList;  
  12.   
  13. public class JExcel {  
  14.   
  15.     public static void main(String[] args) {  
  16.         HSSFWorkbook wb = new HSSFWorkbook();  
  17.         HSSFSheet sheet = wb.createSheet("new sheet");  
  18.         // 在第一行第一个单元格,插入下拉框  
  19.         HSSFRow row = sheet.createRow(0);  
  20.         HSSFCell cell = row.createCell(0);  
  21.         // 普通写入操作  
  22.         cell.setCellValue("请选择");// 这是实验  
  23.         // 生成下拉列表  
  24.         // 只对(0,0)单元格有效  
  25.         CellRangeAddressList regions = new CellRangeAddressList(0, 0, 0, 0);  
  26.         // 生成下拉框内容  
  27.         DVConstraint constraint = DVConstraint.createExplicitListConstraint(new String[] { "2", "3" });  
  28.         // 绑定下拉框和作用区域  
  29.         HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);  
  30.         // 对sheet页生效  
  31.         sheet.addValidationData(data_validation);  
  32.        
  33.         try {  
  34.             

       response.setContentType("application/vnd.ms-excel");
        String fileName = URLEncoder.encode("模板下载","UTF-8");
        response.setHeader("Content-Disposition","attachment; filename="+ fileName+".xls");
        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();

  35.         } catch (Exception e) {  
  36.             // TODO Auto-generated catch block  
  37.             e.printStackTrace();  
  38.   
  39.         }  
  40.         // 结束  
  41.         System.out.println("Over");  
  42.   
  43.     }  
  44. }  

你可能感兴趣的:(easypoi生成带下拉框的excel)