main
黄海 9 months ago
parent 0633abfc24
commit 263b423870

@ -3,6 +3,8 @@ package com.dsideal.base.DataEase.Controller;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import com.dsideal.base.BaseApplication; import com.dsideal.base.BaseApplication;
import com.dsideal.base.DataEase.Model.DataEaseModel; import com.dsideal.base.DataEase.Model.DataEaseModel;
import com.dsideal.base.DataEase.Model.ExcelReader;
import com.dsideal.base.DataEase.Model.ExcelRow;
import com.dsideal.base.Interceptor.EmptyInterface; import com.dsideal.base.Interceptor.EmptyInterface;
import com.dsideal.base.Interceptor.IsLoginInterface; import com.dsideal.base.Interceptor.IsLoginInterface;
import com.dsideal.base.Interceptor.IsNumericInterface; import com.dsideal.base.Interceptor.IsNumericInterface;
@ -211,7 +213,8 @@ public class DataEaseController extends Controller {
return; return;
} }
//检查上传的excel获取它有哪些列与数据集的列是否一致 //检查上传的excel获取它有哪些列与数据集的列是否一致
List<String> cols = dm.getColumnNamesFromExcel(uploadFile.getFile().getAbsolutePath()); String excelPath = uploadFile.getFile().getAbsolutePath();
List<String> excelCols = dm.getColumnNamesFromExcel(excelPath);
//获取指定数据表有哪些列 //获取指定数据表有哪些列
Record rDataSet = dm.getDataSetById(id); Record rDataSet = dm.getDataSetById(id);
String tableName = rDataSet.getStr("table_name"); String tableName = rDataSet.getStr("table_name");
@ -219,9 +222,9 @@ public class DataEaseController extends Controller {
List<String> mysqlCols = dm.getColumns(tableName); List<String> mysqlCols = dm.getColumns(tableName);
//id列需要特殊处理 //id列需要特殊处理
if (mysqlCols.contains("id")) mysqlCols.remove("id"); mysqlCols.remove("id");
//对比两个数组集是不是一致,就是对比列名是不是完全匹配,使用交集去检查 //对比两个数组集是不是一致,就是对比列名是不是完全匹配,使用交集去检查
Set<String> set1 = new HashSet<>(cols); Set<String> set1 = new HashSet<>(excelCols);
Set<String> set2 = new HashSet<>(mysqlCols); Set<String> set2 = new HashSet<>(mysqlCols);
//是不是完整匹配 //是不是完整匹配
boolean match = set1.equals(set2); boolean match = set1.equals(set2);
@ -230,7 +233,10 @@ public class DataEaseController extends Controller {
return; return;
} }
//如果一致,那么需要先把数据集的指定行政区划列的表清空,再导入数据 //如果一致,那么需要先把数据集的指定行政区划列的表清空,再导入数据
//TODO ExcelReader excelReader = new ExcelReader();
List<ExcelRow> rows = excelReader.readXlsxFile(excelPath, excelCols);
dm.saveDataSetTable(identity_id, id, area_name, rows);
//返回结果 //返回结果
Kv kv = Kv.create(); Kv kv = Kv.create();

@ -14,7 +14,6 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@ -41,31 +40,29 @@ public class DataEaseModel {
* *
* *
* @param dataset_id id * @param dataset_id id
* @param ja json
*/ */
public void saveDataSetTable(int identity_id, int dataset_id, String xmqh, JSONArray ja) { public void saveDataSetTable(int identity_id, int dataset_id, String xzqh, List<ExcelRow> listExcelRow) {
String tableName = getDataSetById(dataset_id).getStr("table_name"); String tableName = getDataSetById(dataset_id).getStr("table_name");
if (identity_id > 1) { if (identity_id > 1) {
String sql = "delete from dataease.`" + tableName + "` where `行政区划`=?"; String sql = "delete from `" + tableName + "` where `行政区划`=?";
Db.update(sql, xmqh); Db.use(DB_NAME).update(sql, xzqh);
} else { } else {
String sql = "delete from dataease.`" + tableName + "`"; String sql = "delete from `" + tableName + "`";
Db.update(sql); Db.use(DB_NAME).update(sql);
} }
List<Record> list = new ArrayList<>(); List<Record> list = new ArrayList<>();
for (int i = 0; i < ja.size(); i++) { //数据行
JSONObject jsonObject = ja.getJSONObject(i); for (int i = 1; i < listExcelRow.size(); i++) {
//遍历jo的每一个属性 ExcelRow row = listExcelRow.get(i);
// 或者使用keySet和for-each循环遍历
Record record = new Record(); Record record = new Record();
for (Object key : jsonObject.keySet()) { for (int j = 0; j < row.getData().size(); j++) {
Object value = jsonObject.get(key); String value = row.getData().get(j);
if (value.equals("null")) value = null; //第一行是表头
record.set(key.toString(), value); record.set(listExcelRow.getFirst().getData().get(j), value);
} }
list.add(record); list.add(record);
} }
Db.use("dataease").batchSave(tableName, list, 100); Db.use(DB_NAME).batchSave(tableName, list, 100);
} }
@ -386,7 +383,7 @@ public class DataEaseModel {
* @param filePath * @param filePath
* @return * @return
*/ */
public static List<String> getColumnNamesFromExcel(String filePath) { public List<String> getColumnNamesFromExcel(String filePath) {
List<String> columnNames = new ArrayList<>(); List<String> columnNames = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(filePath); try (FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis)) { Workbook workbook = new XSSFWorkbook(fis)) {

@ -0,0 +1,47 @@
package com.dsideal.base.DataEase.Model;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
public class ExcelReader {
public List<ExcelRow> readXlsxFile(String filePath, List<String> cols) {
List<ExcelRow> rows = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
int rowNum = 0;
for (Row row : sheet) {
List<String> rowData = new ArrayList<>();
for (Cell cell : row) {
rowData.add(getCellValue(cell));
}
if (rowNum == 0) {
// 如果文件中没有列名,可以在这里手动指定
rows.add(new ExcelRow(cols)); // 根据您的实际列名进行修改
rowNum++;
continue;
}
rows.add(new ExcelRow(rowData));
rowNum++;
}
} catch (Exception e) {
e.printStackTrace();
}
return rows;
}
private String getCellValue(Cell cell) {
return switch (cell.getCellType()) {
case STRING -> cell.getStringCellValue();
case NUMERIC -> String.valueOf(cell.getNumericCellValue());
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
case FORMULA -> cell.getCellFormula();
default -> "";
};
}
}

@ -0,0 +1,17 @@
package com.dsideal.base.DataEase.Model;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Setter
@Getter
public class ExcelRow {
private List<String> data;
public ExcelRow(List<String> data) {
this.data = data;
}
}
Loading…
Cancel
Save