使用vo注释做一个poi导出功能

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 block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            if (ops != null) {
                try {
                    ops.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.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方法
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己封装的excel导出/导入,可以根据注解来导出excel.本项目一共有13个类,里面还包含了一个反射工具,一个编码工具,10分值了。下面是测试代码 public class Test { public static void main(String[] arg) throws FileNotFoundException, IOException{ testBean(); testMap(); } public static void testBean() throws FileNotFoundException, IOException{ List l = new ArrayList(); for(int i=0;i<100;i++){ l.add(new MyBean()); } //很轻松,只需要二句话就能导出excel BeanExport be = ExportExcel.BeanExport(MyBean.class); be.createBeanSheet("1月份", "1月份人员信息").addData(l); be.createBeanSheet("2月份","2月份人员信息").addData(l); be.writeFile("E:/test/bean人员信息8.xlsx"); } //如果不想用注解,还能根据MAP导出. public static void testMap () throws FileNotFoundException, IOException{ List l = new ArrayList(); l.add(new MapHeader("姓名","name",5000)); l.add(new MapHeader("年龄","age",4000)); l.add(new MapHeader("生日","birthdate",3000)); l.add(new MapHeader("地址","address",5000)); l.add(new MapHeader("双精度","d",4000)); l.add(new MapHeader("float","f",6000)); List<Map> lm = new ArrayList<Map>(); for(int i=0;i<100;i++){ Map map = new HashMap(); map.put("name","闪电球"); map.put("age",100); map.put("birthdate",new Date()); map.put("address","北京市广东省AAA号123楼!"); map.put("d",22.222d); map.put("f",295.22f); lm.add(map); } MapExport me = ExportExcel.mapExport(l); me.createMapSheel("1月份","广东省人员信息").addData(lm); me.createMapSheel("2月份", "北京市人员信息").addData(lm); me.writeFile("E:/test/map人员信息9.xlsx"); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值