diff --git a/src/main/java/com/zhangmeng/tools/controller/ExcelFileReadController.java b/src/main/java/com/zhangmeng/tools/controller/ExcelFileReadController.java index 338eaad..10eb3b0 100644 --- a/src/main/java/com/zhangmeng/tools/controller/ExcelFileReadController.java +++ b/src/main/java/com/zhangmeng/tools/controller/ExcelFileReadController.java @@ -9,17 +9,26 @@ import com.zhangmeng.tools.utils.AlertUtils; import com.zhangmeng.tools.utils.ImagePath; import javafx.application.Platform; import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.Button; +import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; +import javafx.scene.control.cell.MapValueFactory; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.FileChooser; import javafx.stage.Stage; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import java.io.File; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,7 +39,9 @@ public class ExcelFileReadController { public Button file_choose; @FXML - public TableView result_view; + public TableView> result_view; + + private final ObservableList> result_list = FXCollections.observableArrayList(); @FXML public TextField file_path; @@ -42,7 +53,7 @@ public class ExcelFileReadController { @FXML public void initialize() { - + result_view.setItems(result_list); file_choose.setText(null); ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE))); iv.setPreserveRatio(true); @@ -62,10 +73,8 @@ public class ExcelFileReadController { Stage alert = AlertUtils.alert_loading(file_choose.getScene().getWindow()); new Thread(() -> { - //打开文件 readBySax(); - Platform.runLater(() -> { log.info("打开文件....."); alert.close(); @@ -88,10 +97,44 @@ public class ExcelFileReadController { } } - public void readBySax(){ + public void readBySax() { ExcelReader reader = ExcelUtil.getReader(choose_file.getValue()); - List> readAll = reader.readAll(); - System.out.println(readAll); + List> mapList = reader.readAll(); +// for (int i = 1; i <= mapList.size(); i++) { +// Map map = mapList.get(i-1); +// map.put("id",i); +// } + update_stage(mapList); + } + public void update_stage(List> list) { + result_list.clear(); + result_view.getColumns().clear(); + Platform.runLater(() -> { + if (list.size() > 0) { + Map map1 = list.get(0); + for (Map.Entry entry : map1.entrySet()) { + String key = entry.getKey(); + TableColumn, String> column = new TableColumn<>(key); + column.setCellValueFactory(new MapValueFactory(key)); + result_view.getColumns().add(column); + } + + for (Map map : list) { + Map map2 = new HashMap<>(); + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue() == null ? "" : entry.getValue().toString(); + map2.put(key, new SimpleStringProperty(value)); + } + result_list.add(map2); + } + } + }); + } + + public String str_print(String obj) { + byte[] bytes = obj.getBytes(StandardCharsets.UTF_8); + return StringUtils.toEncodedString(bytes, Charset.forName("GBK")); } }