时间:2021-05-19
导入Excel数据的工具类,调用也就几行代码,很简单的。
复制代码 代码如下:
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* Excel导入的工具类.
*/
public class ExcelUtils {
private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
//成功
public static final Integer STATUS_OK = Integer.valueOf(1);
//失败
public static final Integer STATUS_NO = Integer.valueOf(0);
/**
* 私有化构造器
*/
private ExcelUtils(){
}
/**
* 获取excel文件中的数据对象
*
* @param is excel
* @param excelColumnNames excel中每个字段的英文名(应该与pojo对象的字段名一致,顺序与excel一致)
* @return excel每行是list一条记录,map是对应的"字段名-->值"
* @throws Exception
*/
public static List<Map<String, String>> getImportData(InputStream is, List<String> excelColumnNames) throws Exception {
logger.debug("InputStream:{}", is);
if (is == null) {
return Collections.emptyList();
}
Workbook workbook = null;
try {
//拿到excel
workbook = Workbook.getWorkbook(is);
} catch (BiffException e) {
logger.error(e.getMessage(), e);
return Collections.EMPTY_LIST;
} catch (IOException e) {
logger.error(e.getMessage(), e);
return Collections.EMPTY_LIST;
}
logger.debug("workbook:{}", workbook);
if (workbook == null) {
return Collections.emptyList();
}
//第一个sheet
Sheet sheet = workbook.getSheet(0);
//行数
int rowCounts = sheet.getRows() - 1;
logger.debug("rowCounts:{}", rowCounts);
List<Map<String, String>> list = new ArrayList<Map<String, String>>(rowCounts - 1);
//双重for循环取出数据
for(int i = 1; i < rowCounts; i++){
Map<String, String> params = new HashMap<String, String>();
//i,j i:行 j:列
for(int j = 0; j < excelColumnNames.size(); j++){
Cell cell = sheet.getCell(j, i);
params.put(excelColumnNames.get(j), cell.getContents());
}
list.add(params);
}
return list;
}
/**
* 获取导入数据为对象的List
*
* @param data
* @param clazz
* @param excelColumnNames
* @param checkExcel
* @param <T>
* @return
* @throws Exception
*/
public static <T> List<T> makeData(List<Map<String, String>> data, Class<T> clazz, List<String> excelColumnNames, CheckExcel checkExcel) throws Exception {
if(data == null || data.isEmpty() || clazz == null || checkExcel == null) {
return Collections.EMPTY_LIST;
}
List<T> result = new ArrayList<T>(data.size());
for(Map<String, String> d : data) {
if(checkExcel != null && !checkExcel.check(d)) {
continue;
}
T entity = clazz.newInstance();
for(String column : excelColumnNames) {
BeanUtils.setProperty(entity, column, d.get(column));
}
result.add(entity);
}
return result;
}
}
检查excel中每一行的数据是否合法
复制代码 代码如下:
import java.util.Map;
/**
* 检查excel中每一行的数据是否合法
*/
public interface CheckExcel {
/**
* 返回true合法
*
* @param data excel中每一行的数据
* @return
*/
public boolean check(Map<String, String> data);
}
调用部分
复制代码 代码如下:
List<Map<String, String>> data = ExcelUtils.getImportData(is,Constants.EXCEL_COLUMN_NAMES);
List<FeeAllocation> allocations = ExcelUtils.makeData(data, FeeAllocation.class, Constants.EXCEL_COLUMN_NAMES, new CheckExcel() {
public boolean check(Map<String, String> data) {
if(StringUtils.isEmpty(data.get("name")))
return false;
return true;
}
});
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言 本文对应的场景是导入Excel数据,Excel对应的字段都配置在xml文件中。截图如下:代码实战 工具类 实体类:XMLReadModel.cspu
Java中POI导入EXCEL2003和EXCEL2007的实现方法实现代码:importjava.io.FileInputStream;importjava.
Excel2016如何导入外部数据,外部数据的几种导入类型:导入文本类数据、网站类数据、数据库类数据。软件名称:Office2016官方简体中文版免费完整版软件
excel_class是一款php导入excel的工具,它可以方便从excel导入数据到php,也可以方便用php把数据导入到excel。在使用它的时候出现了中
本文实例讲述了PHP将Excel导入数据库及数据库数据导出至Excel的方法。分享给大家供大家参考。具体实现方法如下:一.导入导入需要使用能读取Excel的组件