easyexcel导入导出+动态列+自定义样式

这篇具有很好参考价值的文章主要介绍了easyexcel导入导出+动态列+自定义样式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

1、引用maven依赖

2、模板文件template1.xlsx

3、导出效果

4、导入效果

5、导出用EasyWriteHandler

6、测试工具类 ExcelTest


1、引用maven依赖

<!-- easyexcel -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.2.1</version>
</dependency>
<!-- java 造数据用 -->
<dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>0.17.2</version>
</dependency>

2、模板文件template1.xlsx

easyexcel动态导入,java

3、导出效果

easyexcel动态导入,java

4、导入效果

easyexcel动态导入,java文章来源地址https://www.toymoban.com/news/detail-552667.html

5、导出用EasyWriteHandler


import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class EasyWriteHandler implements WorkbookWriteHandler, SheetWriteHandler, RowWriteHandler, CellWriteHandler {

    private Integer startRowIndex;
    private Integer lastColumnIndex;



    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
        if(startRowIndex == null){
            startRowIndex = row.getRowNum();
            lastColumnIndex = Integer.valueOf(row.getLastCellNum());
        }
    }

    public EasyWriteHandler(){}
    public Cell cell(Sheet sheet ,int rowIndex,int cellIndex){
        Row row = sheet.getRow(rowIndex);
        if (row == null) {
            row = WorkBookUtil.createRow(sheet, rowIndex);
        }
        Cell cell = row.getCell(cellIndex);
        if (cell == null) {
            cell = WorkBookUtil.createCell(row, cellIndex);
        }
        return cell;
    }

    public Integer getStartRowIndex() {
        return startRowIndex;
    }

    public Integer getLastColumnIndex() {
        return lastColumnIndex;
    }
}

6、测试工具类 ExcelTest


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.github.javafaker.Faker;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.Test;
import xiao.utils.FakerUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class ExcelTest {
    @Test
    public void testExport() throws Exception {
        String fileName = "D:/export.xlsx";
        String templateFile = "D:/template1.xlsx";

        List<List<String>> headList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<String> head = new ArrayList<>();
            head.add("code"+i);
            head.add("标题"+i);
            headList.add(head);
        }

        Faker faker = FakerUtil.FAKER;
        List<List<String>> dataList = Stream.generate(() -> {
            List<String> data=new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                data.add(faker.name().fullName());
            }
            return data;
        }).limit(50).collect(Collectors.toList());


        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)14);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short)10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);


        WriteCellStyle headBlueWriteCellStyle=new WriteCellStyle();
        headBlueWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());


        //添加自定义样式和修改固定单元格内容时需要加上 .inMemory(true)
        EasyExcel.write(fileName)
                .withTemplate(templateFile).head(headList).inMemory(true)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(new EasyWriteHandler() {
                    @Override
                    public void afterCellDispose(CellWriteHandlerContext context) {
                        if (context.getHead()) {
                            Cell cell = context.getCell();
                            if(cell.getColumnIndex()%2 == 0){
                                WriteCellStyle.merge(headBlueWriteCellStyle,
                                        context.getFirstCellData().getOrCreateStyle());
                            }
                        }
                    }

                    @Override
                    public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
                        Sheet sheet = context.getWriteContext().writeSheetHolder().getSheet();
                        for (int columnIndex = 0; columnIndex < getLastColumnIndex() ; columnIndex++) {
                            //设置自适应列宽
                            sheet.autoSizeColumn(columnIndex);
                        }
                        //设置部门名称
                        cell(sheet,1,1).setCellValue("开发部门");

                    }
                }).sheet().doWrite(dataList);
    }
    @Test
    public void testUpload() throws Exception {
        String fileName = "D:/export.xlsx";
        List<Map<Integer, String>> resultList = EasyExcel.read(fileName).sheet().headRowNumber(2).doReadSync();
        System.out.println(JSON.toJSON(resultList));
    }
}

到了这里,关于easyexcel导入导出+动态列+自定义样式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java 实现 excel 自定义样式和字段导出

     java 功能中,有一个功能是大家经常做的,就是excel导出,简单的excel导出 可以直接用阿里的easyExcel添加注解自动导出来某些固定字段就行了,这个是比较简单的导出,本文就不作过多赘述  这篇文章主要是针对,某些页面的导出,比如说按照页面上的表格的样式导出数据

    2024年02月09日
    浏览(29)
  • EasyExcel导入和导出数据

    1.cmtroller 调用service方法,完成导出 2.service 调用工具类的方法完成导出 传入response,标题控制类(标题名称,合并的列数),员工列表,文件名称,excel的标题名称,要导出的数据类 3.工具类中的方法 4.控制标题类 这个类控制数据之前的显示内容 效果 2022-10-28 乱码解决 因为文件

    2023年04月09日
    浏览(24)
  • 使用EasyExcel实现导入和导出

    简单实现导入和导出 POM FileUtil controller Service ServiceImpl ExceptionPersonListener ExceptionPersonImportExcel

    2024年02月13日
    浏览(25)
  • EasyExcel 三分钟搞定导入导出

    前言:本文章教你从零开始,三分钟搞定excel单sheet导出、导入、多sheet导出、导入、excel模板导入单个sheet、多个sheet,废话不多说,直接上代码 1.引入依赖 2.工具类-ExcelHandler 3.工具类-ExcelTemplateEnum 4.工具类-UploadDataListener 5.实体类-ExcelVO 6.业务层-ExcelService 7.实现层-ExcelService

    2024年02月15日
    浏览(22)
  • EasyExcel实现execl导入导出

    在实际开发中,处理 Excel 文件是一个常见的需求。EasyExcel 是一个基于 Java 的开源库,提供了简单易用的 API,可以方便地读取和写入 Excel 文件。本文将介绍如何使用 EasyExcel 实现 Excel 导入功能,以及一些相关的技巧和注意事项。 在开始之前,我们需要准备好 EasyExcel 的环境。

    2024年02月09日
    浏览(28)
  • 数据导入导出(POI以及easyExcel)

            将一些数据库信息导出为Excel表格         将Excel表格数据导入数据库         大量数据的导入导出操作 常⽤的解决⽅案为: Apache POI 与阿⾥巴巴 easyExcel Apache POI 是基于 Office Open XML 标准( OOXML )和 Microsoft 的 OLE 2 复合⽂档 格式( OLE2 )处理各种⽂件格式的

    2024年02月13日
    浏览(29)
  • 基于EasyExcel的数据导入导出(复制可用)

    目录   前言: 新建SpringBoot项目,引入下面的依赖 数据导入导出执行原理和思路: 用户端逻辑: 后台开发逻辑: 代码实现 下拉框策略 批注策略 数据读取监听 Excel工具类 创建导入数据模板类 创建数据导出模板 Web接口 结果展示 模板下载 数据导入 数据导出   代码复制粘贴

    2024年02月05日
    浏览(33)
  • EasyExcel实现Excel文件导入导出功能

    Java领域解析、生成Excel比较有名的框架有Apache poi、jxl等。但他们都存在一个严重的问题就是非常的耗内存。如果你的系统并发量不大的话可能还行,但是一旦并发上来后一定会OOM或者JVM频繁的full gc。 EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。

    2024年02月02日
    浏览(54)
  • 使用EasyExcel实现Excel的导入导出

    在真实的开发者场景中,经常会使用excel作为数据的载体,进行数据导入和导出的操作,使用excel的导入和导出有很多种解决方案,本篇记录一下EasyExcel的使用。 EasyExcel是一个开源的项目,是阿里开发的。EasyExcel可以简化Excel表格的导入和导出操作,使用起来简单快捷,易上手

    2023年04月15日
    浏览(36)
  • 使用EasyExcel实现Excel表格的导入导出

    Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。 easyexcel重

    2024年02月12日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包