excel导入或者用POI
来源:互联网 发布:襄阳seo云优化 编辑:程序博客网 时间:2024/06/06 11:47
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
- excel导入或者用POI
- Excel导入导出,用POI完成
- poi 导入excel源码
- POI导入excel出错
- 使用POI 导入excel
- POI导入Excel
- excel导入(poi)
- poi导入导出excel
- poi excel导入导出
- poi 导入 Excel
- POI实现excel导入
- java poi 导入excel
- 使用poi 导入 excel
- poi导入excel
- POI数据导入Excel
- poi之excel导入
- POI方法导入Excel
- POI导入/导出 EXCEL
- Spring 4 学习笔记2:控制反转(IoC)和依赖注入(DI)
- 错误、调试、测试
- inport
- Linux中find常见用法示例
- 【翻译自mos文章】在10g中,当发生ORA-00020时,sqlplus登陆会报“connected to an idle instance”
- excel导入或者用POI
- LeetCode-8-String to Integer (atoi) (String/Num)-Easy
- Elasticsearch结合Nginx使用
- Date
- LeetCode Generate Parentheses
- 生产实习(Android)五
- fileload
- 命令模式
- MyCat简单搭建使用