首页 > 其他分享 >技术汇总:第一章:使用poi实现表单下载成xls文件并打印

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

时间:2023-02-02 11:13:56浏览次数:57  
标签:并打印 String bis try poi new xls response buff

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

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

第一种方式:

实现代码

      @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

标签:并打印,String,bis,try,poi,new,xls,response,buff
From: https://www.cnblogs.com/wangchuanxinshi/p/17085342.html

相关文章

  • python 将 csv转excel (.xls和.xlsx)的几种方式
    前言excel后缀有2种格式,.xls是从Excel97到Excel2003的默认文件格式,而.xlsx是Excel2007及更高版本的默认文件格式。.xlsx和.xls格式的主要区别在于,.xls格式......
  • springboot导入和导出.xlsx
    以这个为例:这个主要是用到了这个https://www.hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter加入依赖<!--导入导出依赖--><dependency>......
  • ENTRYPOINT
        Dockerfile中RUN,CMD和ENTRYPOINT都能够用于执行命令,下面是三者的主要用途:RUN命令:执行命令并创建新的镜像层,通常用于安装软件包CMD命令:设置容器启动后默认执......
  • 百度离线地图地点搜索 离线地图poi搜索
    1.场景和需求:在局域网开发的web项目,不能连接公网1需要使用离线地图展示设备点位;2需要实现地图的城市范围内的离线搜索,可以检索到百度地图上的点位,类似与百度地图首......
  • PHPMyWind编辑器支持PowerPoint上传
    ​ 1.编辑器修改(可选)1.1在 ueditor/config.json 中添加代码块    /* 上传word配置 */    "wordActionName":"wordupload",/* 执行上传视频的action......
  • java.lang.NumberFormatException: multiple points报错
    记录一下今天出现的问题,前端页面点击本周或本月时,会报一个multiplepoints的错误,起初我以为是我后端接收与前端传的类型不符,后面查了一下才发现,是因为SimpleDateFormat......
  • 根据地图point生成随机坐标
    在此记录一下,不是本人开发github地址:https://github.com/zxbit2011/RandomLngLat......
  • C++ const pointer
    在C++中const限定的指针类型常常令人困惑,现整理如下,以整型为例,主要区分如下三个例子constint*p;int*constp;constint*constp;其实就是2种情况,const在int前......
  • [LeetCode] 1828. Queries on Number of Points Inside a Circle
    Youaregivenanarray points where points[i]=[xi,yi] isthecoordinatesofthe ith pointona2Dplane.Multiplepointscanhavethe same coordinat......
  • .net NPOI导出Excel,自定义单元格背景颜色,office2007及以上,及office2003使用方法
    目录:NPOI相关功能目录开发环境:VS2015.Net版本:.NetFramework4.5.2NPOI版本:2.4.1.0本以为NPOI使用颜色值会非常方便,以为RGB或16进制赋值就行了没想到NPOI不这样给我们用,......