java反射构建excel导出工具类
程序员文章站
2022-12-24 15:13:52
一、定义注解,用于vo类中标记使用到的属性 ......
一、定义注解,用于vo类中标记使用到的属性
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
/**
* @author fc
* @date 2018-09-18
*/
@target(value = {elementtype.field})
@retention(retentionpolicy.runtime)
public @interface allow {
string title() default "undefined";
}
二、反射工具类
import org.apache.poi.hssf.usermodel.hssfworkbook;
import org.apache.poi.ss.usermodel.row;
import org.apache.poi.ss.usermodel.sheet;
import org.apache.poi.ss.usermodel.workbook;
import org.apache.poi.xssf.streaming.sxssfworkbook;
import org.apache.poi.xssf.usermodel.xssfworkbook;
import java.lang.reflect.field;
import java.lang.reflect.method;
import java.util.list;
/**
* @author fc
* @date 2018-09-18
* @msg 反射工具类
* @tips 字段必须使用@allow注解 title属性定义标题
*/
public class excelcreateutil {
/**
*
* @param o object是vo类
* @param suf 返回的excel文件后缀
* @return
* @throws exception
*/
public static workbook createworkbook(list<object> o,returnsuf suf) throws exception {
//定义一个新的工作簿
workbook wb = suf.getvalue().equals(returnsuf.xlsx.getvalue())?new xssfworkbook():new hssfworkbook();
//创建sheet
sheet sheet = wb.createsheet(string.valueof(system.currenttimemillis()));
//创建行
row row = sheet.createrow(0);
//第一行以注解@allow title属性命名
class<?> aclass = o.get(0).getclass();
field[] fields1 = aclass.getdeclaredfields();
int bj2 = 0;
for (int j = 0; j < fields1.length; j++) {
allow annotation = fields1[j].getannotation(allow.class);
if (annotation != null) {
fields1[j].setaccessible(true);
row.createcell(bj2).setcellvalue(annotation.title());
bj2++;
}
}
int bj1 = 1;
for (int i = 0; i < o.size(); i++) {
//获取当前对象
object o1 = o.get(i);
//获取当前类类型
class<?> c = o.get(i).getclass();
//获取当前类类型的所有属性
field[] fields = c.getdeclaredfields();
//创建行-从第二行起
row row1 = sheet.createrow(bj1);
bj1++;
int bj = 0;
for (int j = 0; j < fields.length; j++) {
allow annotation = fields1[j].getannotation(allow.class);
if (annotation != null) {
fields[j].setaccessible(true);
//得到属性名
string name = fields[j].getname();
//获取属性对应的get方法
method method = c.getdeclaredmethod(toget(name));
object invoke = method.invoke(o1);
string value = invoke == null ? "" : invoke.tostring();
//赋值
row1.createcell(bj).setcellvalue(value);
bj++;
}
}
}
return wb;
}
//获取属性的set方法
public static string toset(string s) {
final string set = "set";
if (character.isuppercase(s.charat(0))) {
return set + s;
} else {
return set + (new stringbuilder()).append(character.touppercase(s.charat(0))).append(s.substring(1)).tostring();
}
}
//获取属性的get方法
public static string toget(string s) {
final string get = "get";
if (character.isuppercase(s.charat(0))) {
return get + s;
} else {
return get + (new stringbuilder()).append(character.touppercase(s.charat(0))).append(s.substring(1)).tostring();
}
}
public enum returnsuf {
xls(".xls后缀", ".xls"),
/**
*大数据量建议使用.xlsx
*/
xlsx(".xlsx后缀", ".xlsx"),;
private string key;
private string value;
returnsuf(string key, string value) {
this.key = key;
this.value = value;
}
public string getkey() {
return key;
}
public void setkey(string key) {
this.key = key;
}
public string getvalue() {
return value;
}
public void setvalue(string value) {
this.value = value;
}
}
}