gpt4 book ai didi

excel - 如何使用 savelogue javafx 保存文件

转载 作者:行者123 更新时间:2023-12-04 20:14:59 35 4
gpt4 key购买 nike

我已经创建了 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();
}

}
}

关于excel - 如何使用 savelogue javafx 保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846602/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com