【问题标题】:How to move (excel) files into a directory after selecting it from fileChooser and adding it to tableview (javafx)从fileChooser中选择文件并将其添加到tableview(javafx)后如何将(excel)文件移动到目录中
【发布时间】:2017-06-27 22:15:35
【问题描述】:

我正在尝试将选定的 Excel 文件从 fileChooser 添加到目录中,然后将其显示到表格中 (TableView)。我有一些东西以前会将文件添加到目录中,但后来 excel 文件最终会损坏。即使我确实手动移动了文件(以免文件损坏),表格也不会正确显示文件名。

看起来像这样:

(忽略第二个表,我还没有做任何事情。按Add Excel File 打开文件资源管理器并仅显示 excel 文件。第一个表正在从目录中获取文件并启动,但随后的名称文件没有显示。只有一个高亮表明可能存在某些东西。)

TableView<File> files = new TableView();
TableColumn sheets = new TableColumn();
files.getColumns().add(sheets);
files.getItems().addAll(sSheets.listFiles());
optionsWind.add(files, 0, 1);

Label tLabel = new Label("Your Templates");
optionsWind.add(tLabel, 1, 0);

TableView<File> template = new TableView();
TableColumn t = new TableColumn();
t.getColumns().add(t);
template.getItems().addAll(templates.listFiles());
optionsWind.add(template, 1, 1);

//Buttons grid
GridPane buttons = new GridPane();
buttons.setAlignment(Pos.CENTER);
buttons.setVgap(20);

//excel file
Button addFile = new Button();
addFile.setText("Add Excel File");
buttons.add(addFile, 0, 1);
addFile.setOnAction(new EventHandler<ActionEvent>(){
     public void handle(ActionEvent e){                             
         FileChooser excChs = new FileChooser();
         excChs.setTitle("Open Excel File");
         excChs.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xlsx"));
         File selectFile = excChs.showOpenDialog(secondaryStage);

         //add stuff here
}});

【问题讨论】:

  • 以字节读取文件。将文件写入字节。将文件保存在新位置。如果文件成功保存在新位置,请删除旧文件。
  • 要在 TableView 中显示 excel 文件,您需要从 Apache POI 之类的内容开始。
  • 您使用什么 API 来读取 Excel 文件?

标签: javafx


【解决方案1】:

您可以使用此应用程序来更好地了解如何将文件保存到新位置。你应该在不同的线程上问你的其他问题。一个话题应该只有一个问题。

一个按钮加载一个 Excel 文件。另一个按钮将加载的文件保存到您选择的位置并使用您选择的名称。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication132 extends Application
{
    byte[] fileContent;

    @Override
    public void start(Stage primaryStage)
    {


        Button btn = new Button();
        btn.setText("Load file");
        btn.setOnAction(new EventHandler<ActionEvent>()
        {

            @Override
            public void handle(ActionEvent event)
            {
                try 
                {
                    FileChooser excChs = new FileChooser();
                    excChs.setTitle("Open Excel File");
                    excChs.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xlsx"));
                    File selectFile = excChs.showOpenDialog(primaryStage);
                    fileContent = Files.readAllBytes(selectFile.toPath());
                    System.out.println("File loaded!");
                }
                catch (IOException ex) {
                    System.out.println(ex.toString());
                }
            }
        });

        Button btnSaveFile = new Button();
        btnSaveFile.setText("Save loaded file");
        btnSaveFile.setOnAction((event)->{
            try 
            {
                FileChooser excChs = new FileChooser();
                excChs.setTitle("Save Excel File");
                excChs.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xlsx"));
                File saveFile = excChs.showSaveDialog(primaryStage);
                if(saveFile != null && fileContent.length > 0)
                {
                    Files.write(saveFile.toPath(), fileContent);
                    System.out.println("File saved!");
                }
            }
            catch (IOException ex) {
                System.out.println(ex.toString());
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(btn, btnSaveFile);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }


}

如果您知道要将文件保存到哪个目录,则应该对其进行硬编码。这个例子展示了如何做到这一点。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication132 extends Application
{
    byte[] fileContent;

    @Override
    public void start(Stage primaryStage)
    {


        Button btn = new Button();
        btn.setText("Load file");
        btn.setOnAction(new EventHandler<ActionEvent>()
        {

            @Override
            public void handle(ActionEvent event)
            {
                try 
                {
                    FileChooser excChs = new FileChooser();
                    excChs.setTitle("Open Excel File");
                    excChs.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xlsx"));
                    File selectFile = excChs.showOpenDialog(primaryStage);
                    fileContent = Files.readAllBytes(selectFile.toPath());
                    System.out.println("File loaded!");

                    String directoryToSaveFile = "TheAbsolutePathToSavingDirectory";<--Change this
                    File saveFile = new File(directoryToSaveFile + selectFile.getName());
                    if(saveFile.createNewFile())
                    {
                        if(fileContent.length > 0)
                        {
                            Files.write(saveFile.toPath(), fileContent);
                            System.out.println("File saved to " + saveFile.getAbsolutePath());
                        }
                    }
                    else
                    {
                        System.out.println("No file was created!");
                    }
                }
                catch (IOException ex) {
                    System.out.println(ex.toString());
                }
            }
        });



        VBox root = new VBox();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2017-10-09
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多