当前位置: 移动技术网 > IT编程>开发语言>Java > JavaWeb导出Excel文件并弹出下载框

JavaWeb导出Excel文件并弹出下载框

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、引言

在java web开发中经常涉及到报表,最近做的项目中需要实现将数据库中的数据显示为表格,并且实现导出为excel文件的功能。

二、相关jar包

使用poi可以很好的解决excel的导入和导出的问题,poi下载地址:
poi-3.6-20091214.jar

三、关键代码

首先导入上述jar包。
在生成excel时一般数据源形式为一个list,下面把生成excel格式的代码贴出来:

/**
       * 以下为生成excel操作
       */
      // 1.创建一个workbook,对应一个excel文件
      hssfworkbook wb = new hssfworkbook();
      // 2.在workbook中添加一个sheet,对应excel中的一个sheet
      hssfsheet sheet = wb.createsheet("xxx表");
      // 3.在sheet中添加表头第0行,老版本poi对excel行数列数有限制short
      hssfrow row = sheet.createrow((int) 0);
      // 4.创建单元格,设置值表头,设置表头居中
      hssfcellstyle style = wb.createcellstyle();
      // 居中格式
      style.setalignment(hssfcellstyle.align_center);

      // 设置表头
      hssfcell cell = row.createcell(0);
      cell.setcellvalue("表头1");
      cell.setcellstyle(style);

      cell = row.createcell(1);
      cell.setcellvalue("表头2");
      cell.setcellstyle(style);

      cell = row.createcell(2);
      cell.setcellvalue("表头3");
      cell.setcellstyle(style);

      cell = row.createcell(3);
      cell.setcellvalue("表头4");
      cell.setcellstyle(style);

      cell = row.createcell(4);
      cell.setcellvalue("表头5");
      cell.setcellstyle(style);

生成excel格式后要将数据写入excel:

// 循环将数据写入excel
      for (int i = 0; i < lists.size(); i++) {
        row = sheet.createrow((int) i + 1);
        list list= lists.get(i);
        // 创建单元格,设置值
        row.createcell(0).setcellvalue(list.getxxx());
        row.createcell(1).setcellvalue(list.getxxx());
        row.createcell(2).setcellvalue(list.getxxx());
        row.createcell(3).setcellvalue(list.getxxx());
        row.createcell(4).setcellvalue(list.getxxx());
      }

之后将生成的excel以流输出。
*不弹出下载框

fileoutputstream out =new fileoutputstream("e:/xxx.xls");
wb.write(out); 
out.close();

*弹出下载框

string filename = "xxx表";
      bytearrayoutputstream os = new bytearrayoutputstream();
      wb.write(os);
      byte[] content = os.tobytearray();
      inputstream is = new bytearrayinputstream(content);
      // 设置response参数,可以打开下载页面
      res.reset();
      res.setcontenttype("application/vnd.ms-excel;charset=utf-8");
      res.setheader("content-disposition", "attachment;filename="
          + new string((filename + ".xls").getbytes(), "iso-8859-1"));
      servletoutputstream out = res.getoutputstream();
      bufferedinputstream bis = null;
      bufferedoutputstream bos = null;

      try {
        bis = new bufferedinputstream(is);
        bos = new bufferedoutputstream(out);
        byte[] buff = new byte[2048];
        int bytesread;
        // simple read/write loop.
        while (-1 != (bytesread = bis.read(buff, 0, buff.length))) {
          bos.write(buff, 0, bytesread);
        }
      } catch (exception e) {
        // todo: handle exception
        e.printstacktrace();
      } finally {
        if (bis != null)
          bis.close();
        if (bos != null)
          bos.close();
      }

完成以上操作之后即可跳转到其他页面。
同时poi还可以将excel上传解析显示在网页中,这个另一篇文章总结,敬请期待!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网