1 jsp中:
<a href="${basePath}/manage/bulletinAction.do?method=exportMainProduct&is18th=1">导出公司主营产品</a>
2 action中:
/*** 导出主营产品*/public void exportMainProduct(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {BulletinForm bulletinForm = (BulletinForm) form;bulletinForm.setRows(5000);List<Bulletin> list = bulletinService.find(bulletinForm);List<MainproductcnVo> mainproductcnVos = new ArrayList<MainproductcnVo>();Iterator<Bulletin> it = list.iterator();while (it.hasNext()) {Bulletin bulletin = (Bulletin) it.next();MainproductcnVo mainproductcnVo = new MainproductcnVo();BeanUtils.copyProperties(bulletin, mainproductcnVo);mainproductcnVos.add(mainproductcnVo);}SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("CIOE2017主营产品", "导出时间:" + format.format(new Date()),"CIOE2017主营产品"), MainproductcnVo.class, mainproductcnVos);//主要!!用这一句话确定导出内容OutputStream ops = null;try {ops = response.getOutputStream();response.reset();response.setContentType("application/octet-stream");response.setHeader("Content-Disposition","attachment; filename=mainproduct.xls");// ops=new FileOutputStream("D:/AudienceRecord.xls");workbook.write(ops);ops.flush();System.out.println("ok");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();} finally {if (ops != null) {try {ops.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
3 vo中,注释:
public class MainproductcnVo {@Excel(name = "公司中文名称", orderNum = "1")private String companynameCn;@Excel(name = "公司英文名称", orderNum = "2")private String companynameEn;@Excel(name="主营产品",orderNum = "3")private String mainproductcn;@Excel(name="电话",orderNum = "4")private String tel;@Excel(name="邮箱",orderNum = "5")private String email;//省略set/get方法
}