溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

配置簡單功能強(qiáng)大的excel工具類搞定excel導(dǎo)入導(dǎo)出工具類(一)

發(fā)布時(shí)間:2020-06-23 13:35:38 來源:網(wǎng)絡(luò) 閱讀:757 作者:wuyi0706 欄目:大數(shù)據(jù)

? 對(duì)于J2EE項(xiàng)目導(dǎo)入導(dǎo)出Excel是最普通和實(shí)用功能,本工具類使用步驟簡單,功能強(qiáng)大,只需要對(duì)實(shí)體類進(jìn)行簡單的注解就能實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能,導(dǎo)入導(dǎo)出操作的都是實(shí)體對(duì)象.

請(qǐng)看一下這個(gè)類都有哪些功能:
????? 1.實(shí)體屬性配置了注解就能導(dǎo)出到excel中,每個(gè)屬性都對(duì)應(yīng)一列.
?????
2.列名稱可以通過注解配置.
????? 3.導(dǎo)出到哪一列可以通過注解配置.
?????
4.鼠標(biāo)移動(dòng)到該列時(shí)提示信息可以通過注解配置.
????? 5.用注解設(shè)置只能下拉選擇不能隨意填寫功能.
???? ?
6.用注解設(shè)置是否只導(dǎo)出標(biāo)題而不導(dǎo)出內(nèi)容,這在導(dǎo)出內(nèi)容作為模板以供用戶填寫時(shí)比較實(shí)用.

請(qǐng)看一下效果圖:

?

?

請(qǐng)看一下使用步驟:

1.寫一個(gè)實(shí)體類,并設(shè)置注解配置.
2.實(shí)例化一個(gè)ExcelUtil<T>對(duì)象,調(diào)用exportExcel或importExcel方法.

請(qǐng)看一個(gè)demo.

1.寫一個(gè)實(shí)體類,并設(shè)置注解配置.

package com.tgb.lk.test03;

import com.tgb.lk.util.ExcelVOAttribute;

public class StudentVO {
@ExcelVOAttribute(name = "序號(hào)", column = "A")
private int id;

@ExcelVOAttribute(name = "姓名", column = "B", isExport = true)
private String name;

@ExcelVOAttribute(name = "年齡", column = "C", prompt = "年齡保密哦!", isExport = false)
private int age;

@ExcelVOAttribute(name = "班級(jí)", column = "D", combo = { "五期提高班", "六期提高班",
        "七期提高班" })
private String clazz;

@ExcelVOAttribute(name = "公司", column = "F")
private String company;

//get和set方法(略)...

@Override
public String toString() {
    return "StudentVO [id=" + id + ", name=" + name + ", company="
            + company + ", age=" + age + ", clazz=" + clazz + "]";
}

}
2.實(shí)例化一個(gè)ExcelUtil<T>對(duì)象,調(diào)用exportExcel或importExcel方法.
(1)導(dǎo)出

package com.tgb.lk.test03;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.tgb.lk.util.ExcelUtil;

/*

  • 使用步驟:
  • 1.新建一個(gè)類,例如StudentVO.
  • 2.設(shè)置哪些屬性需要導(dǎo)出,哪些需要設(shè)置提示.
  • 3.設(shè)置實(shí)體數(shù)據(jù)
  • 4.調(diào)用exportExcel方法.
    */
    public class ExportTest03 {
    public static void main(String[] args) {
    // 初始化數(shù)據(jù)
    List<StudentVO> list = new ArrayList<StudentVO>();

    StudentVO vo = new StudentVO();
    vo.setId(1);
    vo.setName("李坤");
    vo.setAge(26);
    vo.setClazz("五期提高班");
    vo.setCompany("天融信");
    list.add(vo);
    
    StudentVO vo2 = new StudentVO();
    vo2.setId(2);
    vo2.setName("曹貴生");
    vo2.setClazz("五期提高班");
    vo2.setCompany("中銀");
    list.add(vo2);
    
    StudentVO vo3 = new StudentVO();
    vo3.setId(3);
    vo3.setName("柳波");
    vo3.setClazz("五期提高班");
    list.add(vo3);
    
    FileOutputStream out = null;
    try {
        out = new FileOutputStream("d:\\success3.xls");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    ExcelUtil<StudentVO> util = new ExcelUtil<StudentVO>(StudentVO.class);// 創(chuàng)建工具類.
    util.exportExcel(list, "學(xué)生信息", 65536, out);// 導(dǎo)出
    System.out.println("----執(zhí)行完畢----------");

    }

}
(2)導(dǎo)入

package com.tgb.lk.test03;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import com.tgb.lk.util.ExcelUtil;

public class ImportTest03 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("d:\success3.xls");
ExcelUtil<StudentVO> util = new ExcelUtil<StudentVO>(
StudentVO.class);// 創(chuàng)建excel工具類
List<StudentVO> list = util.importExcel("學(xué)生信息0", fis);// 導(dǎo)入
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

}
?

看完使用步驟一定對(duì)封裝的類迫不及待了吧,請(qǐng)繼續(xù)往下看:

(1)注解實(shí)現(xiàn)類:

package com.tgb.lk.util;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target( { java.lang.annotation.ElementType.FIELD })
public @interface ExcelVOAttribute {

/**
 * 導(dǎo)出到Excel中的名字.
 */
public abstract String name();

/**
 * 配置列的名稱,對(duì)應(yīng)A,B,C,D....
 */
public abstract String column();

/**
 * 提示信息
 */
public abstract String prompt() default "";

/**
 * 設(shè)置只能選擇不能輸入的列內(nèi)容.
 */
public abstract String[] combo() default {};

/**
 * 是否導(dǎo)出數(shù)據(jù),應(yīng)對(duì)需求:有時(shí)我們需要導(dǎo)出一份模板,這是標(biāo)題需要但內(nèi)容需要用戶手工填寫.
 */
public abstract boolean isExport() default true;
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI