2023年10月30日15:07:15

master
zhangmeng 2023-10-30 15:07:21 +08:00
parent f094344178
commit 2226b85e38
1 changed files with 50 additions and 7 deletions

View File

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