使用hutool进行excel导入导出

  1. maven依赖
    复制代码
      <!--hutool-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.7.20</version>
            </dependency>
            <!--hutool-->
            <!--poi-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.2</version>
            </dependency>
            <!--poi-->
    复制代码
  2. 导入
    复制代码
    /**
         * 导出
         *
         * @param response
         */
        @RequestMapping("/doExport")
        public void doExport(HttpServletRequest request, HttpServletResponse response) throws IOException {

       //对应数据库操作 List
    <xxxx> list = xxUsersService.list(); //在内存操作,写到浏览器 ExcelWriter writer = ExcelUtil.getWriter(true); //自定义标题别名(将对应的字段转化成自己需要的名称) writer.addHeaderAlias("orgCode", "组织编号"); writer.addHeaderAlias("orgName", "组织名称"); writer.addHeaderAlias("orgType", "组织类型"); writer.addHeaderAlias("locationType", "定位类型"); writer.addHeaderAlias("orgLevel", "组织层级"); writer.addHeaderAlias("tips", "备注");

    // 默认的,未添加alias的属性也会写出,如果想只写出加了别名的字段,可以调用此方法排除之 writer.setOnlyAlias(true); //默认配置 writer.write(list, true); //设置content—type response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8"); //设置标题 String fileName = URLEncoder.encode("组织信息", "UTF-8");
    //Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。 response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); ServletOutputStream outputStream = response.getOutputStream(); //将Writer刷新到OutPut writer.flush(outputStream, true); outputStream.close(); writer.close(); }
    复制代码
  3. 导出
    复制代码
    public String importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {//文件处理成io流
                InputStream in = file.getInputStream();//读取excel中的内容
                List<List<Object>> list = excelReader.read(1);
                for (List<Object> row : list) {
                   //职位编号
                    String positionCode = row.get(0).toString();//职位名称
                    String positionName = row.get(1).toString();//关联上级职位
                    model.setPositionPcode(row.get(2).toString());
                    //上级职位名称
                    model.setPositionPname(row.get(3).toString());
    //-------------后面类似一一对应--------------
    } }
    复制代码
  4. 直接根据所在路径导出excel
    复制代码
      @RequestMapping("/download")
        public void template(HttpServletResponse response) {
            //获取路径信息(换成自己的实际路径)
            String value = global.getValue("global.org.template");
            try {
                File file = new File(value);
                //通过流把文件内容写入到客户端
                InputStream fis = new BufferedInputStream(new FileInputStream(value));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                // 清空response
                response.reset();
                //设置标题
                String fileName = URLEncoder.encode("组织模板信息", "UTF-8");
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
                response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    复制代码

     

posted @   fu!!!  阅读(3286)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示