com.github.cosycode.ext.dataformat.CsvUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extend-mod Show documentation
Show all versions of extend-mod Show documentation
扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块
The newest version!
package com.github.cosycode.ext.dataformat;
import com.github.cosycode.common.lang.BaseRuntimeException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.exceptions.CsvException;
import lombok.NonNull;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.ParseException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Description :
*
* created in 2022/9/6
*
*
* @author CPF
* @since 0.2.2
**/
public class CsvUtils {
private CsvUtils() {
}
/**
* 读取csv,返回一个Iterator,每个String[]表示csv中的一行数据
*
* @param csvFile csv 文件 对象
* @return List 读取的数据
*/
public static List readCSV(File csvFile) throws IOException, CsvException {
DataInputStream dataInputStream = new DataInputStream(Files.newInputStream(csvFile.toPath()));
try (CSVReader csvReader = new CSVReader(new InputStreamReader(dataInputStream, StandardCharsets.UTF_8))) {
return csvReader.readAll();
}
}
/**
* @param csvFile csv 文件
* @param tClass 模版类
* @param 模版类
* @return 读取的 list
*/
public static List readCsvToBeanList(File csvFile, Class tClass) throws IOException, CsvException, InstantiationException, IllegalAccessException, ParseException {
List strings = readCSV(csvFile);
if (strings == null) {
return new ArrayList<>();
}
// 封装函数
Field[] fields = tClass.getDeclaredFields();
Function function = name -> {
for (Field field : fields) {
if (field.getName().equalsIgnoreCase(name)) {
return field;
}
}
return null;
};
// 获取header
List list = new ArrayList<>();
String[] headerString = strings.get(0);
for (String str : headerString) {
str = str.trim();
Field apply = function.apply(str);
if (apply != null) {
apply.setAccessible(true);
list.add(apply);
}
}
Field[] header = list.toArray(new Field[0]);
// 获取 body
List result = new ArrayList<>();
int len = header.length;
int rowNum = 1;
for (int rowLen = strings.size(); rowNum < rowLen; rowNum++) {
String[] data = strings.get(rowNum);
T t = tClass.newInstance();
for (int i = 0; i < len; i++) {
Field field = header[i];
if (field == null) {
continue;
}
String da = data[i] == null ? "" : data[i].trim();
Class> type = field.getType();
try {
if (!da.isEmpty()) {
Object obj = TypeConverter.convertStringToObj(da, type);
field.set(t, obj);
}
} catch (IllegalArgumentException e) {
throw new BaseRuntimeException("convertStringToObj error, data is " + Arrays.toString(data), e);
}
}
result.add(t);
}
return result;
}
public static void writeBeanListToCsvFile(File file, @NonNull List list, Class super T> tClass) throws IOException {
if (list.isEmpty()) {
return;
}
Writer writer = new FileWriter(file);
try (CSVWriter csvWriter = new CSVWriter(writer)) {
String[] fieldString = BeanUtils.getFieldString(tClass);
csvWriter.writeNext(fieldString);
for (T obj : list) {
String[] strings = BeanUtils.getStringValueObj(tClass, obj);
csvWriter.writeNext(strings);
}
}
}
/**
*
* 将 csv 中的行首 和 一行数据转换成一个 Map
*
*
* @param keys json的key,顺序需与csv中的value对应
* @param values csv中数据作为value
*/
public static Map convertMap(String[] keys, String[] values) {
Map json = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
json.put(keys[i], values[i]);
}
return json;
}
/**
*
* 将 csv 中的行首 和 多行数据转换成一个 MapList
*
*
* @param keys 行首,顺序需与csv中的value对应
* @param stringsList 读取csv返回的List
*/
public static List