excel导入或者用POI


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Excel工具类
 * 
 *
 */
public class AccessExcelUtils {
private static Logger log = LoggerFactory.getLogger(AccessExcelUtils.class);


public AccessExcelUtils() {


}


@SuppressWarnings("rawtypes")
public static List parseExcel(FileUploadModel file, Class cls, String[] properties) throws Exception {
// 初始话一个解析后的结果集对象
ArrayList<Object> results = new ArrayList<Object>();
if (file == null || cls == null || properties == null) {
return results;
}
InputStream xlsStream = null;
xlsStream = new FileInputStream(new File(file.getFilePath()));
Workbook workbook = Workbook.getWorkbook(xlsStream);
Sheet[] sheets = workbook.getSheets();
for (int sheetsi = 0; sheetsi < sheets.length; sheetsi++) {
Sheet sheet = sheets[sheetsi];
int rows = sheet.getRows();
int columns = sheet.getColumns();
if (rows <= 0 || columns <= 0) {
continue;
}
// 数据库表字段和excel表格里字段个数不相等
if (columns != properties.length) {
System.out.println("数据库表字段和Excel表格里字段个数不相等");
return null;
}
// 取到所有的给定字段的set方法
HashMap hm = getMethod("set", properties, cls);
// 默认第一行为标题不是数据部分,从row=1开始读取数据
for (int row = 1; row < rows; row++) {
Object obj = cls.newInstance();
for (int column = 0; column < columns; column++) {
// 取出Excel表格中的具体一个单元格的数据
Cell cell = sheet.getCell(column, row);
// 要调用的方法
Method method = (Method) hm.get(properties[column]);
// 当前的单元格的类型
CellType current = null;
// 给参数赋值
Object[] args = new Object[1];
// 得到当前单元格的(字符类型)的值
if (cell.getType() == CellType.LABEL) {
current = CellType.LABEL;
LabelCell labelCell = (LabelCell) cell;
String labelValue = labelCell.getString();
args[0] = labelValue;
}
// 得到当前单元格的(数据类型)的值
if (cell.getType() == CellType.NUMBER) {
current = CellType.NUMBER;
NumberCell numberCell = (NumberCell) cell;
double numberValue = numberCell.getValue();
// 得到返回值参数类型
Class[] paraTypes = method.getParameterTypes();
Class paraType = paraTypes[0];
// 返回值参数类型Long
if (paraType.equals(BigInteger.class)) {
args[0] = new BigInteger(Long.valueOf(Double.valueOf(numberValue).longValue()).toString());
} else if (paraType.equals(BigDecimal.class)) {
args[0] = new BigDecimal(new Double(numberValue).toString());
} else if (paraType.equals(Long.class)) {
args[0] = new Long(new Double(numberValue).longValue());
}
// 返回值参数类型Double
else if (paraType.equals(Double.class)) {
args[0] = new Double(numberValue);
}
// 返回值参数类型Integer
else if (paraType.equals(Integer.class)) {
args[0] = new Integer(new Double(numberValue).intValue());
}
// 返回值参数类型Float
else if (paraType.equals(Float.class)) {
args[0] = new Float(new Double(numberValue).floatValue());
}
// 返回值参数类型String
else {
args[0] = new BigDecimal(numberValue).toString();
if (args[0] != null) {
int index = ((String) args[0]).indexOf(".");
if (index > 1) {
args[0] = ((String) args[0]).substring(0, index);
}
}
}
}
// 得到当前单元格的(日期类型)的值
if (cell.getType() == CellType.DATE) {
current = CellType.DATE;
DateCell dateCell = (DateCell) cell;
Date dateValue = dateCell.getDate();
args[0] = DateUtil.toLongDate(DateUtil.clearTime(dateValue)); // 把日期的时分秒去除只留年月日,并转化为Long类型
}
// 数据类型为空
if (current == null) {
args[0] = null;
}
method.invoke(obj, args);
}
results.add(obj);
}
}
if (xlsStream != null) {
xlsStream.close();
}
log.info(String.format("Excel文件: %s解析成功! ", file.getFilePath()));
// 将数据保存到数据库中
return results;
}


/**
* 返回对象的制定前缀prefix方法的集合

* @param prefix
*            方法前缀
* @param properties
*            属性名称
* @param cls
*            对象类型
* @return
*/
private static HashMap<Object, Method> getMethod(String prefix, String[] properties, Class cls) {
HashMap<Object, Method> hm = new HashMap<Object, Method>();


// 取到cls的方法
Method[] methods = cls.getMethods();
for (int j = 0; j < properties.length; j++) {
for (int i = 0; i < methods.length; i++) {
// 取到cls的以head开头后缀在vector中存在的的方法
if (methods[i].getName().equalsIgnoreCase(prefix + properties[j])) {
hm.put(properties[j], methods[i]);
}
}
}
return hm;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值