文章目录
EasyExcel 不支持csv文件的导入,需要自己写逻辑处理 如何能共用EasyExcel的监听器
@Service
public class ImportCsvUtil {
@Autowired
private ThreadPoolTaskExecutor executor;
public void importCsv(InputStream inputStream, AnalysisEventListener<Map<Integer,String>> readListener) throws Exception {
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream,"GBK"));
int count = 0;
Map<Integer,String> headMap = new HashMap<>();
while (true){
count ++;
String s = reader.readLine();
if (s != null){
if (count == 1){
String[] split = s.split(",");
for (int i = 0; i < split.length; i++) {
headMap.put(i,specialUnicode(split[i]));
}
readListener.invokeHeadMap(headMap, null);
}else {
String[] split = s.split(",");
Map<Integer, String> entity = new HashMap<>();
for (int i = 0; i < split.length; i++) {
entity.put(i,split[i]);
}
readListener.invoke(entity,null);
}
}else {
break;
}
}
readListener.doAfterAllAnalysed(null);
}
public static String specialUnicode(String str){
if (str.startsWith("\uFEFF")){
str = str.replace("\uFEFF", "");
}else if (str.endsWith("\uFEFF")){
str = str.replace("\uFEFF","");
}
return str;
}
}
|