Excel 如何使用savedilogue javafx保存文件

Excel 如何使用savedilogue javafx保存文件,excel,javafx,save,Excel,Javafx,Save,我已经创建了excel文件,我希望该文件在JavaFx文件选择器中显示为“文件名”框。因此,我可以使用SaveDialogueBox将该文件保存到任何需要的地方 代码: WriteExcelFile test = new WriteExcelFile(); File exlFile = new File("c:/temp/"+tabName+".xls"); WorkbookSettings wbSettings = new WorkbookSettings(); wbSetting

我已经创建了excel文件,我希望该文件在JavaFx文件选择器中显示为“文件名”框。因此,我可以使用SaveDialogueBox将该文件保存到任何需要的地方

代码:

WriteExcelFile test = new WriteExcelFile();    

File exlFile = new File("c:/temp/"+tabName+".xls");
WorkbookSettings wbSettings = new WorkbookSettings();

wbSettings.setLocale(new Locale("en", "EN"));

WritableWorkbook workbook = Workbook.createWorkbook(exlFile, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
//createLabel(excelSheet);

Label label;
// Write a few headers
label = new Label(0, 0,"ID");
excelSheet.addCell(label);
label = new Label(1, 0,"BIC");
excelSheet.addCell(label);

label = new Label(0, 1,"Hi 0");
excelSheet.addCell(label);
label = new Label(1, 1,"Hi 1");
excelSheet.addCell(label);

workbook.write();
workbook.close();

Stage stage = (Stage) delBtn.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
        new ExtensionFilter("Microsoft Workbook", "*.xls"));

File selectedFile = fileChooser.showSaveDialog(stage);
if (selectedFile != null) {
    //What to write here
}

是的,我用另一种方式得到了答案

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;


/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXText extends Application {

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

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("java-buddy.blogspot.com");
        Group root = new Group();

        try {
            FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

            // index from 0,0... cell A1 is cell(0,0)
            HSSFRow row1 = worksheet.createRow((short) 0);

            HSSFCell cellA1 = row1.createCell((short) 0);
            cellA1.setCellValue("Hello");
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellA1.setCellStyle(cellStyle);

            HSSFCell cellB1 = row1.createCell((short) 1);
            cellB1.setCellValue("Goodbye");
            cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellB1.setCellStyle(cellStyle);

            HSSFCell cellC1 = row1.createCell((short) 2);
            cellC1.setCellValue(true);

            HSSFCell cellD1 = row1.createCell((short) 3);
            cellD1.setCellValue(new Date());
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(HSSFDataFormat
                    .getBuiltinFormat("m/d/yy h:mm"));
            cellD1.setCellStyle(cellStyle);

            workbook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


//        Text textSong = TextBuilder.create()
//                .text
//                .build();                

        Button buttonSave = ButtonBuilder.create()
                .text("Save")
                .build();

        buttonSave.setOnAction(new EventHandler<ActionEvent>() {

          @Override
          public void handle(ActionEvent event) {
              FileChooser fileChooser = new FileChooser();

              //Set extension filter
              FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("excel files (*.xls)", "*.xls");
              fileChooser.getExtensionFilters().add(extFilter);

              //Show save file dialog
              File file = fileChooser.showSaveDialog(primaryStage);

              if(file != null){
                  SaveFile(file);
                  System.out.println("hi");
              }
          }
      });

        VBox vBox = VBoxBuilder.create()
                .children(buttonSave)
                .build();

        root.getChildren().add(vBox);

        primaryStage.setScene(new Scene(root, 500, 400));
        primaryStage.show();

    }

    @SuppressWarnings("unused")
    private void SaveFile(File file){
        try {
            FileOutputStream fileOut = new FileOutputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

            // index from 0,0... cell A1 is cell(0,0)
            HSSFRow row1 = worksheet.createRow((short) 0);

            HSSFCell cellA1 = row1.createCell((short) 0);
            cellA1.setCellValue("Hello");
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellA1.setCellStyle(cellStyle);

            HSSFCell cellB1 = row1.createCell((short) 1);
            cellB1.setCellValue("Goodbye");
            cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellB1.setCellStyle(cellStyle);

            HSSFCell cellC1 = row1.createCell((short) 2);
            cellC1.setCellValue(true);

            HSSFCell cellD1 = row1.createCell((short) 3);
            cellD1.setCellValue(new Date());
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(HSSFDataFormat
                    .getBuiltinFormat("m/d/yy h:mm"));
            cellD1.setCellStyle(cellStyle);

            workbook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFDataFormat;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ButtonBuilder;
导入javafx.scene.layout.VBox;
导入javafx.scene.layout.VBoxBuilder;
导入javafx.scene.text.text;
导入javafx.scene.text.TextBuilder;
导入javafx.stage.FileChooser;
导入javafx.stage.stage;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Date;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFCellStyle;
导入org.apache.poi.hssf.usermodel.HSSFDataFormat;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.util.HSSFColor;
/**
*
*@webhttp://java-buddy.blogspot.com/
*/
公共类JavaFXText扩展应用程序{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共作废开始(最终阶段初级阶段){
setTitle(“java buddy.blogspot.com”);
组根=新组();
试一试{
FileOutputStream fileOut=新的FileOutputStream(“poi test.xls”);
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet工作表=工作簿.createSheet(“POI工作表”);
//从0,0开始的索引…单元格A1是单元格(0,0)
HSSFRow row1=工作表.createRow((短)0);
HSSFCell cellA1=row1.createCell((短)0);
cellA1.setCellValue(“你好”);
HSSFCellStyle cellStyle=工作簿.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID\u前景);
cellA1.setCellStyle(cellStyle);
HSSFCell cellB1=row1.createCell((短)1);
cellB1.setCellValue(“再见”);
cellStyle=workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.LIGHT\u CORNFLOWER\u BLUE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID\u前景);
cellB1.setCellStyle(cellStyle);
HSSFCell cellC1=row1.createCell((短)2);
cellC1.setCellValue(真);
HSSFCell cellD1=row1.createCell((短)3);
cellD1.setCellValue(新日期());
cellStyle=workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat
.GetBuiltInformation(“m/d/yy h:mm”);
Cell1.设置cellStyle(cellStyle);
工作簿。写入(归档);
flush();
fileOut.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//Text textSong=TextBuilder.create()
//.文本
//.build();
Button buttonSave=ButtonBuilder.create()
.text(“保存”)
.build();
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
FileChooser FileChooser=newfilechooser();
//设置扩展筛选器
FileChooser.ExtensionFilter extFilter=newfilechooser.ExtensionFilter(“excel文件(*.xls)”,“*.xls”);
fileChooser.getExtensionFilters().add(extFilter);
//显示保存文件对话框
File File=fileChooser.showsavedilog(primaryStage);
如果(文件!=null){
保存文件(文件);
System.out.println(“hi”);
}
}
});
VBox VBox=VBoxBuilder.create()
.儿童(按钮保存)
.build();
root.getChildren().add(vBox);
原始阶段。设置场景(新场景(根,500400));
primaryStage.show();
}
@抑制警告(“未使用”)
私有void保存文件(文件){
试一试{
FileOutputStream fileOut=新的FileOutputStream(文件);
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet工作表=工作簿.createSheet(“POI工作表”);
//从0,0开始的索引…单元格A1是单元格(0,0)
HSSFRow row1=工作表.createRow((短)0);
HSSFCell cellA1=row1.createCell((短)0);
cellA1.setCellValue(“你好”);
HSSFCellStyle cellStyle=工作簿.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID\u前景);
cellA1.setCellStyle(cellStyle);
HSSFCell cellB1=row1.createCell((短)1);
cellB1.setCellValue(“再见”);
cellStyle=workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.LIGHT\u CORNFLOWER\u BLUE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID\u前景);
cellB1.setCellStyle(cellStyle);
HSSFCell cellC1=row1.createCell((短)2);
cellC1.setCellValue(真);
HSSFCell cellD1=row1.createCell((短)3);
cellD1.setCellValue(新日期());
cellStyle=workbook.createCellStyle()