使用POI进行Excel的导出,实现下载效果

//从jsp页面中获取多个Id
          String ids = request.getParameter("ids");
          System.out.println(ids);
          //从数据库中查询到要导出的字段信息
          List<News> news = n.inputExecl(ids);
          System.out.println(news.toString());
          //第一步:创建工作部对象
          HSSFWorkbook work=new HSSFWorkbook();
          //第二步:创建工作表对象
          HSSFSheet table=work.createSheet("新闻信息表");
          //第三步:设置行标题
          String[] title=new String[]{"ID","信息标题","发布时间","更新时间","是否推荐"};
          //遍历所查询到的信息
          HSSFRow smallRow = table.createRow(0);
          for(int i=0;i<title.length;i++){
          HSSFCell createCell = smallRow.createCell(i);
          createCell.setCellValue(title[i]);
          }
          //第四步:遍历集合对象
          for(int i=0;i<news.size();i++){
          News n=news.get(i);
              //创建行
          HSSFRow row = table.createRow(i+1);//因为java数数是从0开始的,每遍历一次要加1   
        // 开始创建单元格并赋值
  HSSFCell ns_idCell = row.createCell(0);
  ns_idCell.setCellValue(n.getNs_id());
  
  HSSFCell ns_titleCell = row.createCell(1);
  ns_titleCell.setCellValue(n.getNs_title());
  
  HSSFCell ns_showtimeCell = row.createCell(2);
  ns_showtimeCell.setCellValue(n.getNs_showtime());
  
  HSSFCell ns_updateCell = row.createCell(3);
  ns_updateCell.setCellValue(n.getNs_update());
  
  HSSFCell ns_stateCell = row.createCell(4);
  ns_stateCell.setCellValue(n.getNs_state());
          }
          
          //第五步:生产Excel文件名
          String ExcelName ="账单管理基础信息表" + ".xlsx";
          //下载功能
          response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(ExcelName, "utf-8"));
          ServletOutputStream out = response.getOutputStream();
          work.write(out);
  out.close();// 关闭流   
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值