【Java学习笔记】文件和Excel操作工具类

相比较原始的文件读写及Excel读写方式,commons-io及Excel4j无疑更加简单方便。本文将针对这两个工具类的使用进行简单介绍。

一、commons-io

首先需要导入commons-io工具包

<dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.7</version>
</dependency>

基本使用

// 定义path为工程路径下的data的1.txt
path=System.getProperty("user.dir")+File.separator+"data"+File.separator+"1.txt";
// 创建file对象
File f = new File(path);
// 如果file不存在则创建
if(!f.exists()) {
    try {
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}    
try {
    //写入文件
    FileUtils.write(f, "厉害了我的国\n","utf8",false);
    // 读取文件
    System.out.println(FileUtils.readFileToString(f,"utf8"));
} catch (IOException e) {
    e.printStackTrace();
}

二、Excel4j

第一步导入工具包

<dependency>
    <groupId>com.github.crab2died</groupId>
    <artifactId>Excel4J</artifactId>
    <version>2.1.4-Final2</version>
</dependency>

使用Excel4J简化excel的读写,主要是通过注解来实现,需要创建model类来实现

通过在model类的对应属性上添加title定义读取或写入的列名称,也可以通过writeConverter和readConverter针对写入或读取的数据做中间数据处理。

Student model

package com.lxs.db2excel;

import com.github.crab2died.annotation.ExcelField;

public class Student {
    
    @ExcelField(title = "id")
    private String id;
    
    @ExcelField(title = "name")
    private String name;
    
    @ExcelField(title = "sex")
    private String sex;
    
    @ExcelField(title = "age", writeConverter=Data2Excel.class,readConverter=Data2Excel.class)
    private int age;
    
    @ExcelField(title = "score", writeConverter=Data2Excel.class,readConverter=Data2Excel.class)
    private int score;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", score=" + score + "]";
    }
    
    
}

数据处理显示类

package com.lxs.db2excel;

import com.github.crab2died.converter.DefaultConvertible;

public class Data2Excel extends DefaultConvertible{
 
    @Override
    public Object execRead(String object) {
        
        float x = Float.valueOf(object) * 100;
        String xx = String.valueOf(x);
        return xx;
    }

    @Override
    public Object execWrite(Object object) {
        float x = Float.valueOf(object.toString()) * 200 ;
        String xx = String.valueOf(x);
        return xx;
    }

}

Excel4J的读写

// 定义读取路径
String path=System.getProperty("user.dir")+File.separator+"data"+File.separator+"student.xlsx";

// 读取的数据结果是Student对象组成的List
// 数据读取,需要注意Student类中的title注意必须要和excel列标题对应才可以读取,遇到readConverter需要将对应属性执行转化后读取
List<Student> list = ExcelUtils.getInstance().readExcel2Objects(path, Student.class);

// 数据导出,导入时候的列标题即为Student类中定义中的title名称,遇到writeConverter需要将对应属性执行转化后写入
ExcelUtils.getInstance().exportObjects2Excel(list, Student_export.class, path);

 

posted @ 2021-10-02 19:38  时念  阅读(193)  评论(0编辑  收藏  举报
Live2D