poi生成excel下拉菜单

在web开发中常用到生成excel模板,在用excel poi生成excel模板时,常用到生成excel下拉菜单。下面是生成excel下拉菜单的代码:

package poiexcel;

  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 POI

  {

  public static void main(String [] args)

  {

  String [] list={"东软","华信","SAP","海辉"};

  new POI().createListBox(list);

  return;

  }

  public void createListBox (String [] list)

  {

  //文件初始化

  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(list);

  //绑定下拉框和作用区域

  HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);

  //对sheet页生效

  sheet.addValidationData(data_validation);

  //写入文件

  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");

  }

  }
 上面的代码中,下拉菜单的内容是在list中写死的,在开发的过程中往往是会用到“动态”生成下拉菜单的内容,在这里记录代码,一来有助于自己积累,二来将经验与大家分享:
public void downLoadModle() throws IOException, RowsExceededException, WriteException{


 String jgId = this.getParameter("jgId");
 List<BaseTrainType> childrenList = (List<BaseTrainType>) pxlbManager.findByHql("from BaseTrainType b where b.parentId="+pxlb+"and b.id in (select trainTypeId from OrgBaseTrainType where orgBaseId="+jgId+")");



 String path ="/excel模板.xls";
 String templetDire = this.getRequest().getSession().getServletContext().getRealPath("/common");
 FileInputStream finput = new FileInputStream(path); //从工程目录下下载模板
 POIFSFileSystem pss = new POIFSFileSystem(finput);
 HSSFWorkbook workbook = new HSSFWorkbook(pss);
//读取Sheet
 HSSFSheet sheet = workbook.getSheetAt(0);
 CellRangeAddressList regions = new CellRangeAddressList(1, 65535, 12, 12); //excel下拉菜单作用域,例子中是第12列(从0开始)
 String [] list= new String[childrenList.size()];;
 for (int i=0;i<childrenList.size();i++) {

list[i] = childrenList.get(i).getTypeNum()+"-"+childrenList.get(i).getTypeName(); //加载下拉菜单中的内容
}
 DVConstraint constraint = DVConstraint.createExplicitListConstraint(list); //将list中的内容写入到excel中
 HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint); //绑定下拉菜单和作用域
 sheet.addValidationData(data_validation); //对sheet生效




this.getResponse().setContentType("application/vnd.ms-excel");
 String fileName = URLEncoder.encode(name,"UTF-8");
 this.getResponse().setHeader("Content-Disposition","attachment; filename="+ fileName+pxbjNum+".xls");
 ServletOutputStream out = this.getResponse().getOutputStream(); 
workbook.write(out);
}


  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值