java實(shí)現(xiàn)excel導(dǎo)入數(shù)據(jù)的工具類(lèi)
導(dǎo)入Excel數(shù)據(jù)的工具類(lèi),調(diào)用也就幾行代碼,很簡(jiǎn)單的。
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導(dǎo)入的工具類(lèi).
*/
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);
/**
* 私有化構(gòu)造器
*/
private ExcelUtils(){
}
/**
* 獲取excel文件中的數(shù)據(jù)對(duì)象
*
* @param is excel
* @param excelColumnNames excel中每個(gè)字段的英文名(應(yīng)該與pojo對(duì)象的字段名一致,順序與excel一致)
* @return excel每行是list一條記錄,map是對(duì)應(yīng)的"字段名-->值"
* @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();
}
//第一個(gè)sheet
Sheet sheet = workbook.getSheet(0);
//行數(shù)
int rowCounts = sheet.getRows() - 1;
logger.debug("rowCounts:{}", rowCounts);
List<Map<String, String>> list = new ArrayList<Map<String, String>>(rowCounts - 1);
//雙重for循環(huán)取出數(shù)據(jù)
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;
}
/**
* 獲取導(dǎo)入數(shù)據(jù)為對(duì)象的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中每一行的數(shù)據(jù)是否合法
import java.util.Map;
/**
* 檢查excel中每一行的數(shù)據(jù)是否合法
*/
public interface CheckExcel {
/**
* 返回true合法
*
* @param data excel中每一行的數(shù)據(jù)
* @return
*/
public boolean check(Map<String, String> data);
}
調(diào)用部分
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;
}
});
- java常用工具類(lèi)之DES和Base64加密解密類(lèi)
- java正則表達(dá)式表單驗(yàn)證類(lèi)工具類(lèi)(驗(yàn)證郵箱、手機(jī)號(hào)碼、qq號(hào)碼等)
- java連接數(shù)據(jù)庫(kù)增、刪、改、查工具類(lèi)
- Java中StringUtils工具類(lèi)的一些用法實(shí)例
- java使用jdbc連接數(shù)據(jù)庫(kù)工具類(lèi)和jdbc連接mysql數(shù)據(jù)示例
- java常用工具類(lèi)之?dāng)?shù)據(jù)庫(kù)連接類(lèi)(可以連接多種數(shù)據(jù)庫(kù))
- Java常用數(shù)字工具類(lèi) 數(shù)字轉(zhuǎn)漢字(1)
- java常用工具類(lèi)之Excel操作類(lèi)及依賴(lài)包下載
- java文件操作工具類(lèi)分享(file文件工具類(lèi))
- java常用工具類(lèi) UUID、Map工具類(lèi)
相關(guān)文章
java統(tǒng)計(jì)漢字字?jǐn)?shù)的方法示例
這篇文章主要介紹了java統(tǒng)計(jì)漢字字?jǐn)?shù)的方法,結(jié)合實(shí)例形式分析了java正則判定、字符串遍歷及統(tǒng)計(jì)相關(guān)操作技巧,需要的朋友可以參考下2017-05-05java request.getParameter中文亂碼解決方法
今天跟大家分享幾個(gè)解決java Web開(kāi)發(fā)中,request.getParameter()獲取URL中文參數(shù)亂碼的解決辦法,需要的朋友可以參考下2020-02-02SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法
本篇文章主要介紹了SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法,詳細(xì)的介紹了Spring Schedule 與 Quartz 整合的兩種方法,有興趣的可以了解一下。2017-03-03關(guān)于.java編譯成.class?與?.class反編譯成.java問(wèn)題
這篇文章主要介紹了關(guān)于.java編譯成.class?與?.class反編譯成.java問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09教你一步到位部署運(yùn)行MyBatis3源碼(保姆級(jí))
一個(gè)框架的運(yùn)行流程從最簡(jiǎn)單的一個(gè)helloworld來(lái)看其源碼就能了解到框架的原理是什么,這篇文章主要給大家介紹了關(guān)于如何一步到位部署運(yùn)行MyBatis3源碼的相關(guān)資料,需要的朋友可以參考下2022-06-06intellij idea 2021.2 打包并上傳運(yùn)行spring boot項(xiàng)目的詳細(xì)過(guò)程(spring boot 2
這篇文章主要介紹了intellij idea 2021.2 打包并上傳運(yùn)行一個(gè)spring boot項(xiàng)目(spring boot 2.5.4),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Java GUI進(jìn)階之流式布局管理器FlowLayout專(zhuān)項(xiàng)精講
FlowLayout-流式布局管理器,按水平方向依次排列放置組件,排滿一行,換下一行繼續(xù)排列。排列方向(左到右 或 右到左)取決于容器的componentOrientation屬性2022-04-04java如何判斷一個(gè)數(shù)是否是素?cái)?shù)(質(zhì)數(shù))
這篇文章主要介紹了java如何判斷一個(gè)數(shù)是否是素?cái)?shù)(質(zhì)數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java讀寫(xiě).properties文件解決中文亂碼問(wèn)題
這篇文章主要介紹了Java讀寫(xiě).properties文件解決中文亂碼問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11