io.github.mengwang0211:excel-utils

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

GroupId

GroupId

io.github.mengwang0211
ArtifactId

ArtifactId

excel-utils
Last Version

Last Version

3.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

https://github.com/mengwang0211/excel-utils.git

Download excel-utils

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.mengwang0211/excel-utils/ -->
<dependency>
    <groupId>io.github.mengwang0211</groupId>
    <artifactId>excel-utils</artifactId>
    <version>3.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.mengwang0211/excel-utils/
implementation 'io.github.mengwang0211:excel-utils:3.0.0'
// https://jarcasting.com/artifacts/io.github.mengwang0211/excel-utils/
implementation ("io.github.mengwang0211:excel-utils:3.0.0")
'io.github.mengwang0211:excel-utils:jar:3.0.0'
<dependency org="io.github.mengwang0211" name="excel-utils" rev="3.0.0">
  <artifact name="excel-utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.mengwang0211', module='excel-utils', version='3.0.0')
)
libraryDependencies += "io.github.mengwang0211" % "excel-utils" % "3.0.0"
[io.github.mengwang0211/excel-utils "3.0.0"]

Dependencies

compile (6)

Group / Artifact Type Version
com.alibaba : fastjson jar 1.2.58
org.apache.poi : poi jar 3.17
org.apache.poi : poi-ooxml jar 3.17
commons-collections : commons-collections jar 3.2.1
commons-beanutils : commons-beanutils jar 1.8.3
org.slf4j : slf4j-api jar 1.7.5

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.18

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
ch.qos.logback : logback-classic jar 1.2.3

Project Modules

There are no modules declared in this project.

Maven Central License

excel-utils

使用方法

    <groupId>io.github.mengwang0211</groupId>
    <artifactId>excel-utils</artifactId>
    <version>${latest.version}</version>
通常可以使用你的数据表bean,当然如果需要导入/导出的字段与数据表差异很大,可以新增bean,当然也可以跳过bean,直接使用Map,下面是示例Bean:
public class Model {
    @ExcelCell(index = 0)
    private String a;
    @ExcelCell(index = 1)
    private String b;
    @ExcelCell(index = 2)
    private String c;
    @ExcelCell(index = 3)
    private Date d;

    public Date getD() {
        return d;
    }

    public void setD(Date d) {
        this.d = d;
    }

    public Model(String a, String b, String c,Date d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    //setter and getter 省略...
}
    // 导出-Bean方式
    //用排序的Map且Map的键应与ExcelCell注解的index对应
    Map<String,String> map = new LinkedHashMap<>();
    map.put("a","姓名");
    map.put("b","年龄");
    map.put("c","性别");
    map.put("d","出生日期");
    Collection<Object> dataset=new ArrayList<Object>();
    dataset.add(new Model("", "", "",null));
    dataset.add(new Model(null, null, null,null));
    dataset.add(new Model("王五", "34", "",new Date()));
    File f=new File("test.xls");
    OutputStream out =new FileOutputStream(f);

    ExcelUtil.exportExcel(map, dataset, out);
    out.close();
   
    // 导出-Map方式
    List<Map<String,Object>> list = new ArrayList<>();
    Map<String,Object> map =new LinkedHashMap<>();
    map.put("name", "");
    map.put("age", "");
    map.put("birthday","");
    map.put("sex","");
    Map<String,Object> map2 =new LinkedHashMap<String, Object>();
    map2.put("name", "测试是否是中文长度不能自动宽度.测试是否是中文长度不能自动宽度.");
    map2.put("age", null);
    map2.put("sex", null);
    map.put("birthday",null);
    Map<String,Object> map3 =new LinkedHashMap<String, Object>();
    map3.put("name", "张三");
    map3.put("age", 12);
    map3.put("sex", "");
    map3.put("birthday",new Date());
    list.add(map);
    list.add(map2);
    list.add(map3);
    Map<String,String> map1 = new LinkedHashMap<>();
    map1.put("name","姓名");
    map1.put("age","年龄");
    map1.put("birthday","出生日期");
    map1.put("sex","性别");
    File f= new File("test.xls");
    OutputStream out = new FileOutputStream(f);
    ExcelUtil.exportExcel(map1,list, out );
    out.close();
     
    // 导入
    ExcelUtil.importExcel 方法的第一个参数,是个Class,如果你有Bean,就写BeanClass,反之,直接Map.class

 // XLS 文件
File f=new File("src/test/resources/test.xls");
InputStream inputStream= new FileInputStream(f);

ExcelLogs logs =new ExcelLogs();
Collection<Map> importExcel = ExcelUtil.importExcel(Map.class, inputStream, "yyyy/MM/dd HH:mm:ss", logs , 0);

for(Map m : importExcel){
  System.out.println(m);
}
 // XLSX 文件
File f=new File("src/test/resources/test.xlsx");
InputStream inputStream= new FileInputStream(f);

ExcelLogs logs =new ExcelLogs();
Collection<Map> importExcel = ExcelUtil.importExcel(Map.class, inputStream, "yyyy/MM/dd HH:mm:ss", logs , 0);

for(Map m : importExcel){
  System.out.println(m);
}

Versions

Version
3.0.0
2.2.0
2.1.0
2.0.1
2.0.0
1.0.0