hutool ExcelUtil 导出导入excel

引入依赖

xml 复制代码
  <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.8.15</version>
        </dependency>

文件导入

java 复制代码
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);
        }
    }

文件导出

java 复制代码
 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
        );
相关推荐
ajassi20002 小时前
开源 C++ QT Widget 开发(十五)多媒体--音频播放
linux·c++·qt·开源
能摆一天是一天2 小时前
JAVA stream().flatMap()
java·windows
JosieBook3 小时前
【远程运维】Linux 远程连接 Windows 好用的软件:MobaXterm 实战指南
linux·运维·windows
文档搬运工3 小时前
Linux MInt启动速度的优化
linux
Broken Arrows4 小时前
Linux学习——管理网络安全(二十一)
linux·学习·web安全
Light604 小时前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度
linux·pdf·可观测性·异步队列·plt转pdf·权限治理·进度查询
羚羊角uou5 小时前
【Linux】命名管道
linux·运维·服务器
IT 小阿姨(数据库)5 小时前
PgSQL监控死元组和自动清理状态的SQL语句执行报错ERROR: division by zero原因分析和解决方法
linux·运维·数据库·sql·postgresql·centos
THMAIL5 小时前
量化股票从贫穷到财务自由之路 - 零基础搭建Python量化环境:Anaconda、Jupyter实战指南
linux·人工智能·python·深度学习·机器学习·金融
让子弹飞025 小时前
36.2Linux单总线驱动DS18B20实验(详细讲解代码)_csdn
linux·ubuntu·驱动的分离和分层