mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
5 years ago
10 changed files with 361 additions and 60 deletions
@ -0,0 +1,87 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.converters.ConverterKeyBuild; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.exception.ExcelDataConvertException; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.read.metadata.holder.ReadHolder; |
||||
|
||||
/** |
||||
* Converting objects |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public class ConverterUtils { |
||||
|
||||
private ConverterUtils() {} |
||||
|
||||
/** |
||||
* Convert it into a String map |
||||
* |
||||
* @param cellDataMap |
||||
* @param readHolder |
||||
* @return |
||||
*/ |
||||
public static Map<Integer, String> convertToStringMap(Map<Integer, CellData> cellDataMap, ReadHolder readHolder) { |
||||
Map<Integer, String> stringMap = new HashMap<Integer, String>(cellDataMap.size() * 4 / 3 + 1); |
||||
for (Map.Entry<Integer, CellData> entry : cellDataMap.entrySet()) { |
||||
CellData cellData = entry.getValue(); |
||||
if (cellData.getType() == CellDataTypeEnum.EMPTY) { |
||||
stringMap.put(entry.getKey(), null); |
||||
continue; |
||||
} |
||||
Converter converter = |
||||
readHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType())); |
||||
if (converter == null) { |
||||
throw new ExcelDataConvertException( |
||||
"Converter not found, convert " + cellData.getType() + " to String"); |
||||
} |
||||
try { |
||||
stringMap.put(entry.getKey(), |
||||
(String)(converter.convertToJavaData(cellData, null, readHolder.globalConfiguration()))); |
||||
} catch (Exception e) { |
||||
throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e); |
||||
} |
||||
} |
||||
return stringMap; |
||||
} |
||||
|
||||
/** |
||||
* Convert it into a Java object |
||||
* |
||||
* @param cellData |
||||
* @param clazz |
||||
* @param contentProperty |
||||
* @param converterMap |
||||
* @param globalConfiguration |
||||
* @return |
||||
*/ |
||||
public static Object convertToJavaObject(CellData cellData, Class clazz, ExcelContentProperty contentProperty, |
||||
Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration) { |
||||
if (clazz == CellData.class) { |
||||
return new CellData(cellData); |
||||
} |
||||
Converter converter = null; |
||||
if (contentProperty != null) { |
||||
converter = contentProperty.getConverter(); |
||||
} |
||||
if (converter == null) { |
||||
converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType())); |
||||
} |
||||
if (converter == null) { |
||||
throw new ExcelDataConvertException( |
||||
"Converter not found, convert " + cellData.getType() + " to " + clazz.getName()); |
||||
} |
||||
try { |
||||
return converter.convertToJavaData(cellData, contentProperty, globalConfiguration); |
||||
} catch (Exception e) { |
||||
throw new ExcelDataConvertException("Convert data " + cellData + " to " + clazz + " error ", e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,73 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 读取头 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class DemoHeadDataListener extends AnalysisEventListener<DemoData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DemoHeadDataListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<DemoData> list = new ArrayList<DemoData>(); |
||||
|
||||
/** |
||||
* 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。 |
||||
* |
||||
* @param exception |
||||
* @param context |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void onException(Exception exception, AnalysisContext context) { |
||||
LOGGER.error("解析失败,但是继续解析下一行", exception); |
||||
} |
||||
|
||||
/** |
||||
* 这里会一行行的返回头 |
||||
* |
||||
* @param headMap |
||||
* @param context |
||||
*/ |
||||
@Override |
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
||||
LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); |
||||
} |
||||
|
||||
@Override |
||||
public void invoke(DemoData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||
if (list.size() >= BATCH_COUNT) { |
||||
saveData(); |
||||
list.clear(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
saveData(); |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
/** |
||||
* 加上存储数据库 |
||||
*/ |
||||
private void saveData() { |
||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
||||
LOGGER.info("存储数据库成功!"); |
||||
} |
||||
} |
Loading…
Reference in new issue