技术汇总:第一章:使用poi实现表单下载成xls文件并打印

业务需求:
在这里插入图片描述

点击下载
在这里插入图片描述

第一种方式:

实现代码

      @RequestMapping("/ad/downExcel")
      public String downExcel(HttpSession session, HttpServletResponse response) {
          try {
              List<Ad> adlist = adService.getAdList();
              String fileName="广告管理表";
              List<Map<String,Object>> list=createExcelRecord(adlist);
              String columnNames[]={"标题","链接","权重"};//列名
              String keys[]    =     {"title","link","weight"};//map中的key
              ByteArrayOutputStream os = new ByteArrayOutputStream();
              try {
                  ExcelUtil.createWorkBook(list,keys,columnNames).write(os);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              byte[] content = os.toByteArray();
              InputStream is = new ByteArrayInputStream(content);
              // 设置response参数,可以打开下载页面
              response.reset();
              response.setContentType("application/vnd.ms-excel;charset=utf-8");
              response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
              ServletOutputStream out = response.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);
                  }

 

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120575483

posted @ 2023-02-02 11:01  忘川信使  阅读(97)  评论(0编辑  收藏  举报