All Projects → HowieYuan → Easyexcel Encapsulation

HowieYuan / Easyexcel Encapsulation

easyexcel 的方法封装,快速、简单地处理 Excel

Programming Languages

java
68154 projects - #9 most used programming language

Labels

Projects that are alternatives of or similar to Easyexcel Encapsulation

Closedxml
ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.
Stars: ✭ 2,799 (+1051.85%)
Mutual labels:  excel
Portphp
Data import/export framework for PHP
Stars: ✭ 225 (-7.41%)
Mutual labels:  excel
Psexcel
A simple Excel PowerShell module
Stars: ✭ 234 (-3.7%)
Mutual labels:  excel
Excel to code
Roughly translate some Excel spreadsheets to Ruby or C.
Stars: ✭ 214 (-11.93%)
Mutual labels:  excel
Spark Excel
A Spark plugin for reading Excel files via Apache POI
Stars: ✭ 216 (-11.11%)
Mutual labels:  excel
Gotenberg
A Docker-powered stateless API for PDF files.
Stars: ✭ 3,272 (+1246.5%)
Mutual labels:  excel
Vue Easytable
🍉 Table Component/ Data Grid / Data Table.Support Virtual Scroll,Column Fixed,Header Fixed,Header Grouping,Filter,Sort,Cell Ellipsis,Row Expand,Row Checkbox ...
Stars: ✭ 2,501 (+929.22%)
Mutual labels:  excel
Androiddocumentviewer
Android 文档查看: word、excel、ppt、pdf,使用mupdf及tbs
Stars: ✭ 235 (-3.29%)
Mutual labels:  excel
Docto
Simple command line utility for converting .doc & .xls files to any supported format such as Text, RTF, CSV or PDF
Stars: ✭ 220 (-9.47%)
Mutual labels:  excel
Seatable
SeaTable: easy like a spreadsheet, powerful like a database
Stars: ✭ 231 (-4.94%)
Mutual labels:  excel
Autopoi
AutoPOI 功能如同名字auto,追求的就是自动化,让一个没接触过poi的人员,可以傻瓜化的快速实现Excel导入导出、Word模板导出、可以仅仅5行代码就可以完成Excel的导入导出。
Stars: ✭ 213 (-12.35%)
Mutual labels:  excel
Yarg
Yet Another Report Generator - CUBA Platform reporting engine
Stars: ✭ 215 (-11.52%)
Mutual labels:  excel
Django Data Wizard
🧙⚙️ Import structured data (e.g. Excel, CSV, XML, JSON) into one or more Django models via an interactive web-based wizard
Stars: ✭ 227 (-6.58%)
Mutual labels:  excel
Flutter sheet localization
Generate Flutter localization from a simple online Google Sheets.
Stars: ✭ 212 (-12.76%)
Mutual labels:  excel
Relaxtools Addin
RelaxTools Addin for Microsoft Excel 2010/2013/2016
Stars: ✭ 234 (-3.7%)
Mutual labels:  excel
Vue Blog
🎉 基于vue全家桶 + element-ui 构建的一个后台管理集成解决方案
Stars: ✭ 208 (-14.4%)
Mutual labels:  excel
Flask Excel
A flask extension using pyexcel to read, manipulate and write data in different excel formats: csv, ods, xls, xlsx and xlsm.
Stars: ✭ 227 (-6.58%)
Mutual labels:  excel
Layui Excel
简单快捷的导出插件,导出仅需一句话
Stars: ✭ 239 (-1.65%)
Mutual labels:  excel
Kkfileviewofficeedit
文件在线预览及OFFICE(word,excel,ppt)的在线编辑
Stars: ✭ 234 (-3.7%)
Mutual labels:  excel
Closedxml.report
ClosedXML.Report is a tool for report generation with which you can easily export any data from your .NET classes to Excel using a XLSX-template.
Stars: ✭ 230 (-5.35%)
Mutual labels:  excel

easyexcel-encapsulation

easyexcel 项目地址 :https://github.com/alibaba/easyexcel

对 easyexcel 进行了方法的封装,可以做到一个函数完成简单的读取和导出

目前 easyexcel 版本已经更新至 1.1.2-beta4


一. 依赖

首先是添加该项目的依赖,目前的版本是 1.1.2-beta4

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>1.1.2-beta4</version>
</dependency>

二. 需要的类

1. ExcelUtil

工具类,可以直接调用该工具类的方法完成 Excel 的读或者写

2. ExcelListener

监听类,可以根据需要,自定义处理获取到的数据

public class ExcelListener extends AnalysisEventListener {

    //自定义用于暂时存储data。
    //可以通过实例获取该值
    private List<Object> datas = new ArrayList<>();

    /**
     * 通过 AnalysisContext 对象还可以获取当前 sheet,当前行等数据
     */
    @Override
    public void invoke(Object object, AnalysisContext context) {
        //数据存储到list,供批量处理,或后续自己业务逻辑处理。
        datas.add(object);
        //根据自己业务做处理
        doSomething(object);
    }

    private void doSomething(Object object) {
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        /*
            datas.clear();
            解析结束销毁不用的资源
         */
    }

    public List<Object> getDatas() {
        return datas;
    }

    public void setDatas(List<Object> datas) {
        this.datas = datas;
    }
}

3. ExcelWriterFactroy

用于导出多个 sheet 的 Excel,通过多次调用 write 方法写入多个 sheet

4. ExcelException

捕获相关 Exception

三. 读取 Excel

读取 Excel 时只需要调用 ExcelUtil.readExcel() 方法

@RequestMapping(value = "readExcel", method = RequestMethod.POST)
public Object readExcel(MultipartFile excel) {
    return ExcelUtil.readExcel(excel, new ImportInfo());
}

其中 excel 是 MultipartFile 类型的文件对象,而 new ImportInfo() 是该 Excel 所映射的实体对象,需要继承 BaseRowModel 类,如:

public class ImportInfo extends BaseRowModel {
    @ExcelProperty(index = 0)
    private String name;

    @ExcelProperty(index = 1)
    private String age;

    @ExcelProperty(index = 2)
    private String email;

    /*
        作为 excel 的模型映射,需要 setter 方法
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

作为映射实体类,通过 @ExcelProperty 注解与 index 变量可以标注成员变量所映射的列,同时不可缺少 setter 方法

四. 导出 Excel

1. 导出的 Excel 只拥有一个 sheet

只需要调用 ExcelUtil.writeExcelWithSheets() 方法:

@RequestMapping(value = "writeExcel", method = RequestMethod.GET)
public void writeExcel(HttpServletResponse response) throws IOException {
    List<ExportInfo> list = getList();
    String fileName = "一个 Excel 文件";
    String sheetName = "第一个 sheet";

    ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName, new ExportInfo());
    }

fileName,sheetName 分别是导出文件的文件名和 sheet 名,new ExportInfo() 为导出数据的映射实体对象,list 为导出数据。

对于映射实体类,可以根据需要通过 @ExcelProperty 注解自定义表头,当然同样需要继承 BaseRowModel 类,如:

public class ExportInfo extends BaseRowModel {
    @ExcelProperty(value = "姓名" ,index = 0)
    private String name;

    @ExcelProperty(value = "年龄",index = 1)
    private String age;

    @ExcelProperty(value = "邮箱",index = 2)
    private String email;

    @ExcelProperty(value = "地址",index = 3)
    private String address;
}

value 为列名,index 为列的序号

如果需要复杂一点,可以实现如下图的效果:

对应的实体类写法如下:

public class MultiLineHeadExcelModel extends BaseRowModel {

    @ExcelProperty(value = {"表头1","表头1","表头31"},index = 0)
    private String p1;

    @ExcelProperty(value = {"表头1","表头1","表头32"},index = 1)
    private String p2;

    @ExcelProperty(value = {"表头3","表头3","表头3"},index = 2)
    private int p3;

    @ExcelProperty(value = {"表头4","表头4","表头4"},index = 3)
    private long p4;

    @ExcelProperty(value = {"表头5","表头51","表头52"},index = 4)
    private String p5;

    @ExcelProperty(value = {"表头6","表头61","表头611"},index = 5)
    private String p6;

    @ExcelProperty(value = {"表头6","表头61","表头612"},index = 6)
    private String p7;

    @ExcelProperty(value = {"表头6","表头62","表头621"},index = 7)
    private String p8;

    @ExcelProperty(value = {"表头6","表头62","表头622"},index = 8)
    private String p9;
}

2. 导出的 Excel 拥有多个 sheet

调用 ExcelUtil.writeExcelWithSheets() 处理第一个 sheet,之后调用 write() 方法依次处理之后的 sheet,最后使用 finish() 方法结束

public void writeExcelWithSheets(HttpServletResponse response) throws IOException {
    List<ExportInfo> list = getList();
    String fileName = "一个 Excel 文件";
    String sheetName1 = "第一个 sheet";
    String sheetName2 = "第二个 sheet";
    String sheetName3 = "第三个 sheet";

    ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName1, new ExportInfo())
                .write(list, sheetName2, new ExportInfo())
                .write(list, sheetName3, new ExportInfo())
                .finish();
}

write 方法的参数为当前 sheet 的 list 数据,当前 sheet 名以及对应的映射类

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].