上篇博客是Excel导入数据库,这篇是mysql导出到Excel,环境都一样。操作的是xlsx文件。
上代码:
模型类
User.java
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
数据库操作类
ListUser.java
public static List<User> list(){
List<User> users = new ArrayList<User>();
User user = null;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String sql = "select * from t_user";
try {
conn = DButil.getConnection();
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DButil.freeSource(conn, pst, rs);
}
return users;
}
Excel操作类
ExceportExcel.java
public void Export(List<T> objs,T t) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException{
String outputFile="e:\\"+t.getClass().getSimpleName()+".xlsx";
XSSFWorkbook workbook = new XSSFWorkbook(); //操作xlsx文件用这个
XSSFSheet sheet = workbook.createSheet("sheet1");
XSSFRow row = sheet.createRow((short)0); //创建一行,这行是表头
XSSFCell cell=null; //单元格
/**----------------给表头添加字段*/ //对应你类中的属性名称
Class c = t.getClass();
Field[] fields = c.getDeclaredFields();//获取类中所有的属性,包括private
int i = 0;
for (Field field : fields) {
String fieldName = field.getName(); //属性名称
Class fieldType = field.getType(); //类型
cell = row.createCell((short)i);
cell.setCellValue(fieldName); //设置到表头中
i++;
}
/**----------------给表头添加字段完毕*/
/**
* 下面的代码 就不写注释了,上篇博里有
*/
int j = 1;
for (Object obj : objs) {
row = sheet.createRow(j);//每一次都创建一个新的行
int m = 0;
for(Field field : fields){
String fieldName = field.getName();
Class fieldType = field.getType();
String getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
Method getMethod = c.getDeclaredMethod(getMethodName, new Class[]{});
cell=row.createCell(m);
if(fieldType == int.class){
int fieldValue = (Integer) getMethod.invoke(obj, new Integer[]{});
cell.setCellValue(fieldValue);
}
if(fieldType == String.class){
String fieldValue = (String) getMethod.invoke(obj, new String[]{});
cell.setCellValue(fieldValue);
}
//这里还可以添加不同的属性类型的处理逻辑,具体的自己看着稍微改改
m++;
}
j++;
}
FileOutputStream out = new FileOutputStream(outputFile);
workbook.write(out);
out.flush();
out.close();
System.out.println("完成!!!");
}
主方法
doMain.java
/***
* 导出Excel的反射实现
* @author yx
*
*/
public class DoMain {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
List<User> users = ListUser.list();
//上面这两行是我自己的dao层里查询出来的user集合,你可以换成自己的。
//换成自己的,把下面的 model改了就好了。
User user = new User();
ExceportExcel<User> e = new ExceportExcel<User>();
e.Export(users, user);
}
}
| | |部分字段乱码,大家有解决办法回复我 。