java 导出 Excel

工具类:

package com.hcoa.util;

import java.io.File;

import java.lang.reflect.Method;

import java.util.List;
import java.util.Map;
import java.util.Set;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
* @author 陆仁聪 E-mail:cmzx3444@163.com
* @file ExcelUtil.java
* @version 创建时间:Sep 4, 2009 3:09:08 PM
* @desc (excel操作工具类)
*/
public class ExcelUtil<E> {
/**
*
* @param list要导入到excel的数据集合
* @param file
* excel文件
* @param type数据的类型
* @param attrubutes要导出的属性
* @param head表格头部信息
* 实例如下 String[] attrubutes = { "users.number", "users.name",
* "averageScore", "coursePoint", "awardScore", "sportPoint" };
* String[] head = { "序号", "学号", "姓名", "绩点", "学习成绩分", "学习成绩加分",
* "体育成绩分" }; ExcelUtil<PointSumScore> excelUtil = new ExcelUtil<PointSumScore>();
* excelUtil.createExcel(pointSumScoreList, file,
* PointSumScore.class, attrubutes, head);
*
*/
@SuppressWarnings("deprecation")
public void createExcel(List<E> list, File file, Class<E> type,
String[] attrubutes, String[] head,
Map<String, Map<Integer, String>>... keyvalueMap) {
WritableWorkbook wwb = null;
try {
file.createNewFile();
wwb = Workbook.createWorkbook(file);
WritableSheet ws = wwb.createSheet("sheet1", 0);
// 下面开始添加单元格
Label labelC = null;
for (int j = 0; j < head.length; j++) {
labelC = new Label(j, 0, head[j]);
ws.addCell(labelC);
}
for (int i = 0; i < list.size(); i++) {
labelC = new Label(0, i + 1, Integer.toString(i + 1));// 序号
ws.addCell(labelC);
for (int j = 0; j < attrubutes.length; j++) {
Object returnvalue = returnVelue(
returnGetMethod(attrubutes[j]), list.get(i), 0,
keyvalueMap);
if (returnvalue == null) {
labelC = new Label(j + 1, i + 1, "0");
} else
labelC = new Label(j + 1, i + 1, returnvalue.toString());
ws.addCell(labelC);
}
}
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private Object returnVelue(String[] getMethod, Object o, int i,
Map<String, Map<Integer, String>>... keyvalueMap) {
Object returnValue = null;
if (o != null) {
Method[] methods = o.getClass().getMethods();
try {
for (Method m : methods) {
if (m.getName().equals(getMethod[i])) {
returnValue = m.invoke(o);
i += 1;
if (i < getMethod.length) {
returnValue = returnVelue(getMethod, returnValue, i);
} else {
if (keyvalueMap != null) {
for (Map<String, Map<Integer, String>> map : keyvalueMap) {
Set<String> attr = map.keySet();
Map<Integer, String> kv;
for (String a : attr) {
if (a.equals(m.getName())) {
kv = map.get(a);
if (returnValue == null
|| returnValue.equals(""))
returnValue = "未知";
else
returnValue = kv
.get(returnValue);
}
}
}
}
return returnValue;
}
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return returnValue;
}

private String[] returnGetMethod(String attrubutes) {
String[] attrubutesArray = attrubutes.split("\\.");
String[] returns = new String[attrubutesArray.length];
for (int i = 0; i < attrubutesArray.length; i++) {
Character f = attrubutesArray[i].charAt(0);
String a = f.toString().toUpperCase();
returns[i] = "get" + a + attrubutesArray[i].substring(1);
}
return returns;
}

}


使用列子:

ExcelUtil<PointSumScore> excelUtil = new ExcelUtil<PointSumScore>();
Map<String, Map<Integer, String>> map = new HashMap<String, Map<Integer, String>>();
Map<Integer, String> value = new HashMap<Integer, String>();
value.put(0, "未发布");
value.put(1, "等待确认");
value.put(2, "已确认");
map.put("getSumPointStatus", value);
try {
file.createNewFile();
String[] attrubutes = { "users.number", "users.name", "coursePoint", "users.classes.classRate",
"qitaPoint", "jobPoint", "prizePoint", "punishpiont", "sportPoint", "dormPoint",
"sumPoint", "sumPointStatus" };
String[] head = { "排名", "学号", "姓名", "基本分", "班级等级分", "其他特分", "任职分", "竞赛加分", "奖惩分", "体育打卡分",
"宿舍加分", "总分", "状态" };
excelUtil.createExcel(pointSumScoreList, file, PointSumScore.class, attrubutes, head, map);
} catch (Exception e) {
e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值