技术汇总:第一章:使用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 @   忘川信使  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示