Hutool读取Excel的方法
需求:单个Excel文件读取
public class FileTest{
public static void main(String[] args) throws SQLException {
ExcelReader reader = ExcelUtil.getReader("/file.xlsx");
List<List<Object>> readAll = reader.read();
System.out.println(readAll.toString());
}
}
需求: 本地有部分文件需要导入数据库
多文件文件存储位置: D盘
直接上代码:
public class FileTest{
public static void main(String[] args) throws SQLException {
File f = new File("D://temp/result/week");
for(File temp : f.listFiles()) {
if(temp.isFile()) {
System.out.println(temp.getName());
ExcelReader reader2= ExcelUtil.getReader(temp, 0);
List<Map<String,Object>> readAll2 = reader2.readAll();
for(Map<String,Object> map:readAll2){
Db.use().insert(
Entity.create("table")
.set("date_week", map.get("date"))
);
}
}
}
}
public static void readCsv(String[] args) throws SQLException, ParseException {
CsvReader reader = CsvUtil.getReader();
//从文件中读取CSV数据
CsvData data = reader.read(FileUtil.file("D://temp/2022.csv"));
List<CsvRow> rows = data.getRows();
int numbers=rows.size();
for (int i=1;i<numbers;i++) {
CsvRow csvRow=rows.get(i);
Db.use().insert(
Entity.create("table")
.set("year",csvRow.getRawList().get(0))
.set("month", csvRow.getRawList().get(0))
);
}
}
}