- package com.test;
- import java.io.FileOutputStream;
- import org.apache.poi2.hssf.usermodel.DVConstraint;
- import org.apache.poi2.hssf.usermodel.HSSFCell;
- import org.apache.poi2.hssf.usermodel.HSSFDataValidation;
- import org.apache.poi2.hssf.usermodel.HSSFRow;
- import org.apache.poi2.hssf.usermodel.HSSFSheet;
- import org.apache.poi2.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi2.hssf.util.CellRangeAddressList;
- public class JExcel {
- 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(0);
- // 普通写入操作
- cell.setCellValue("请选择");// 这是实验
- // 生成下拉列表
- // 只对(0,0)单元格有效
- CellRangeAddressList regions = new CellRangeAddressList(0, 0, 0, 0);
- // 生成下拉框内容
- DVConstraint constraint = DVConstraint.createExplicitListConstraint(new String[] { "2", "3" });
- // 绑定下拉框和作用区域
- HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);
- // 对sheet页生效
- sheet.addValidationData(data_validation);
- try {
-
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(); - } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // 结束
- System.out.println("Over");
- }
- }
easypoi生成带下拉框的excel
最新推荐文章于 2024-04-30 16:28:58 发布