easypoi导出excel带下拉框选项
废话不多说,直接上代码!!!!!
这里是easypoi的导出工具类,bean(Food)和查询list(arrayList)我直接省略了
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,Food.class,arrayList);
//这里是自己加的 带下拉框的代码
ExcelSelectListUtil.selectList(workbook, 4, 4, new String[]{"斤","两","个","袋","份"});
ExcelSelectListUtil.selectList(workbook, 0, 0, new String[]{"蔬菜","水果","主食","熟食","调料"});
downLoadExcel("菜品管理",workbook, request, response);
好,让咱们直接来我写的excel下拉框代码,我简洁了 单元格选项,因为格式固定了,所以从第二行写死开始的直到65535行
public final class ExcelSelectListUtil {
/**
* firstRow 開始行號 根据此项目,默认为2(下标0开始)
* lastRow 根据此项目,默认为最大65535
* firstCol 区域中第一个单元格的列号 (下标0开始)
* lastCol 区域中最后一个单元格的列号
* strings 下拉内容
* */
public static void selectList(Workbook workbook,int firstCol,int lastCol,String[] strings ){
Sheet sheet = workbook.getSheetAt(0);
// 生成下拉列表
// 只对(x,x)单元格有效
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 65535, firstCol, lastCol);
// 生成下拉框内容
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(strings);
HSSFDataValidation dataValidation = new HSSFDataValidation(cellRangeAddressList, dvConstraint);
// 对sheet页生效
sheet.addValidationData(dataValidation);
}
}
来看效果
下载excel的代码
// 下载excel
public static void downLoadExcel(String fileName, Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws IOException {
OutputStream output = null;
BufferedOutputStream bufferedOutPut = null;
try {
// 重置响应对象
response.reset();
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = fileName + "-" + sdf.format(new Date());
String UserAgent = request.getHeader("USER-AGENT").toLowerCase();
// 指定下载的文件名--设置响应头
if (UserAgent.indexOf("firefox") >= 0) {
response.setHeader("content-disposition", "attachment;filename=\"" + new String(dateStr.getBytes("UTF-8"), "iso-8859-1") +".xls\"");
}else {
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(dateStr, "UTF-8")+".xls");
}
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
// 编码
response.setCharacterEncoding("UTF-8");
output = response.getOutputStream();
bufferedOutPut = new BufferedOutputStream(output);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
} catch (Exception e) {
} finally {
if (bufferedOutPut != null) {
bufferedOutPut.close();
}
if (output != null) {
output.close();
}
}
}