hutool ExcelUtil 导出导入excel

引入依赖

  <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.8.15</version>
        </dependency>

文件导入

public void savelist(
            String filepath,
            String keyname
    ){
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(filepath), 0);
        List<Map<String, Object>> sheetRowsMap = reader.readAll();

        String host = "192.168.1.80";
        int port = 6379;
        Jedis jedis = new  Jedis(host, port);
        for (Map<String, Object> stringObjectMap : sheetRowsMap) {
            TemplateVo vo =  JwfsStdPopRealPopNewTemplateVo.builder()
                    .addressDetail(String.valueOf(stringObjectMap.get("户籍地址(校验后)")))
                    .popName(String.valueOf(stringObjectMap.get("姓名")))
                    .popPhone(String.valueOf(stringObjectMap.get("电话")))
                    .build();
                    log.info(vo);
        }
    }

文件导出

 public static void export(List<JwfsStdPopRealPopNewTemplateVo> data, String path, String fileName, JwfsStdPopRealPopNewTemplateVo excelData) throws IOException {
        log.info("开始导出");
        ExcelWriter writer = ExcelUtil.getWriter();
        List<Map> fieldAnnotation = getFieldAnnotation(excelData);
        for(Map<String,String> map:fieldAnnotation){
            for(String key:map.keySet()){
                String value = map.get(key);
                writer.addHeaderAlias(key,value);
            }
        }

        //第1列40px宽
        //writer.setColumnWidth(0, 40);
        //第2列15px 宽
        //writer.setColumnWidth(1, 15);



        //对齐方式,水平左对齐,垂直中间对齐
        writer.getStyleSet().setAlign(HorizontalAlignment.LEFT, VerticalAlignment.CENTER);

        //标题样式
        CellStyle headCellStyle = writer.getHeadCellStyle();
        //设置背景色
        headCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        //必须设置 否则背景色不生效
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //创建标题字体
        Font headFont = writer.createFont();
        headFont.setFontName("宋体");
        //大小
        headFont.setFontHeightInPoints((short) 11);
        //加粗
        headFont.setBold(true);
        headCellStyle.setFont(headFont);

        writer.setColumnWidth(-1, 30);
        writer.setRowHeight(-1,20);


        //保存路径
        FileOutputStream output = new FileOutputStream(path+fileName+".xls");
        // 一次性写出内容
        writer.write(data);

        //自适应列宽
        int columnCount = writer.getColumnCount();
        for (int i = 0; i < columnCount; ++i) {
            double width = SheetUtil.getColumnWidth(writer.getSheet(), i, false);
            if (width != -1.0D) {
                width *= 256.0D;
                //此处可以适当调整,调整列空白处宽度
                width += 220D;
                writer.setColumnWidth(i, Math.toIntExact(Math.round(width / 256D)));
            }
        }

        writer.flush(output,true);
        // 关闭writer,释放内存
        writer.close();
    }


   JwfsStdPopRealPopNewTemplateVo jwfsStdPopRealPopNewTemplateVo = JwfsStdPopRealPopNewTemplateVo.builder().build();
        export(
                list,
                "C:\\Users\\Administrator\\Desktop\\1\\",
                filename,
                jwfsStdPopRealPopNewTemplateVo
        );

相关推荐

  1. excel导入导出

    2024-07-10 05:14:03       32 阅读
  2. Excel 导入导出的封装

    2024-07-10 05:14:03       33 阅读
  3. hutool ExcelUtil 导出导入excel

    2024-07-10 05:14:03       23 阅读
  4. Excel 导入

    2024-07-10 05:14:03       22 阅读
  5. 导出excel

    2024-07-10 05:14:03       18 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 05:14:03       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 05:14:03       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 05:14:03       42 阅读
  4. Python语言-面向对象

    2024-07-10 05:14:03       53 阅读

热门阅读

  1. 模型计算量 MAC/FLOPs 的手动统计方法

    2024-07-10 05:14:03       19 阅读
  2. 构建自定义Tensorflow镜像时用到的链接地址整理

    2024-07-10 05:14:03       25 阅读
  3. 凸包——G - Highest Ratio

    2024-07-10 05:14:03       18 阅读
  4. 力扣第226题“翻转二叉树”

    2024-07-10 05:14:03       22 阅读
  5. QUdpSocket 的bind函数详解

    2024-07-10 05:14:03       21 阅读
  6. 代码随想录算法训练营第7天

    2024-07-10 05:14:03       25 阅读
  7. 关于美国服务器IP的几个常见问题

    2024-07-10 05:14:03       25 阅读
  8. html&css基础

    2024-07-10 05:14:03       22 阅读
  9. 【云原生】Kubernetes部署高可用平台手册

    2024-07-10 05:14:03       20 阅读
  10. Ubuntu 20.04.6 安装 docker

    2024-07-10 05:14:03       26 阅读
  11. 数据结构第07节:队列

    2024-07-10 05:14:03       20 阅读