在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);
}