我想创建一个新的excel文件,以便用户在运行时提供该文件的名称,并将该文件保存在用户想要的位置。 我尝试了一些代码,但它每次都在相同的位置创建一个具有相同名称的文件。它不会询问用户的文件名和位置。
private void buttonCALCULATEActionPerformed(java.awt.event.ActionEvent evt) {
filename="D:\\test.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("Report in Excel");
HSSFRow rowhead=sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Plot Id");
rowhead.createCell((short) 1).setCellValue("Species Name");
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
System.out.println("\n Your Excel file has been generated!");
//String name1=" ";
FileSave();
}
public void FileSave() throws IOException
{
JFileChooser chooser=new JFileChooser("./");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel files",".xls");
chooser.addChoosableFileFilter(filter);
int returnVal1=chooser.showSaveDialog(this);
// chooser.setFileFilter(filter);
chooser.setFileSelectionMode(chooser.FILES_ONLY);
chooser.setDialogTitle("Save File");
chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.home")));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(final File f)
{
return f.isDirectory()|| file.getAbsolutePath().endsWith(".xls");
}
public String getDescription(){
return "Excel files (*.xls)";
}
}
);
if (returnVal1 == JFileChooser.APPROVE_OPTION)
{
file1 = chooser.getSelectedFile();
}
}
答案 0 :(得分:1)
在int returnVal1=chooser.showSaveDialog(this);
if (returnVal1 == JFileChooser.APPROVE_OPTION)
public void FileSave() throws IOException {
JFileChooser chooser = new JFileChooser("./");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel files", ".xls");
chooser.addChoosableFileFilter(filter);
// chooser.setFileFilter(filter);
chooser.setFileSelectionMode(chooser.FILES_ONLY);
chooser.setDialogTitle("Save File");
chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.home")));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(final File f) {
return f.isDirectory() || file.getAbsolutePath().endsWith(".xls");
}
public String getDescription() {
return "Excel files (*.xls)";
}
});
int returnVal1 = chooser.showSaveDialog(this);
if (returnVal1 == JFileChooser.APPROVE_OPTION) {
file1 = chooser.getSelectedFile();
}
}
更改FileSave
以返回File
(如果用户取消了文件选择器,则返回null
)
public File FileSave() throws IOException {
File file1 = null;
//...
return file1;
}
在您尝试保存文件之前询问该文件......
private void buttonCALCULATEActionPerformed(java.awt.event.ActionEvent evt) {
File file = FileSave();
if (file != null) {
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("Report in Excel");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Plot Id");
rowhead.createCell((short) 1).setCellValue("Species Name");
try (FileOutputStream fileOut = new FileOutputStream(file)) {
hwb.write(fileOut);
} catch (IOException exp) {
exp.printStackTrace();
}
System.out.println("\n Your Excel file has been generated!");
}
}
您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码
答案 1 :(得分:0)
我尝试了这个并且它有效 public void FileSave()抛出IOException {
JFileChooser chooser=new JFileChooser(".");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel files","xls","excel");
chooser.addChoosableFileFilter(filter);
chooser.setFileFilter(filter);
chooser.setFileSelectionMode(chooser.FILES_AND_DIRECTORIES);
chooser.setDialogTitle("Save File");
chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.home")));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(final File f)
{
return f.isDirectory()|| file.getAbsolutePath().endsWith(".xls");
}
public String getDescription()
{
return "Excel files (*.xls)";
}
});
int returnVal1=chooser.showSaveDialog(this);
if (returnVal1 == JFileChooser.APPROVE_OPTION)
{
file1 = chooser.getSelectedFile();
if(!file1.exists())
{
FileOutputStream fileOut = new FileOutputStream(file1);
hwb.write(fileOut);
fileOut.close();
System.out.println("\n Your Excel file has been generated!");
JOptionPane.showMessageDialog(this,"File Created.");
}
else if(file1.exists())
{
int res=JOptionPane.showConfirmDialog(this,"File already exists.Do you wish to overwrite?");
if(res == JOptionPane.YES_OPTION)
{
FileOutputStream fileOut = new FileOutputStream(file1);
hwb.write(fileOut);
fileOut.close();
System.out.println("\n Your Excel file has been generated!");
JOptionPane.showMessageDialog(this,"File Created.");
}
else if(res == JOptionPane.NO_OPTION)
{
int returnVal2=chooser.showSaveDialog(this);
if (returnVal2 == JFileChooser.APPROVE_OPTION)
{
File file2 = chooser.getSelectedFile();
if(!file2.exists())
{
FileOutputStream fileOut = new FileOutputStream(file2);
hwb.write(fileOut);
fileOut.close();
System.out.println("\n Your Excel file has been generated!");
JOptionPane.showMessageDialog(this,"File Created.");
}
}
}
else if (res == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(this, "User cancelled operation.");
}
}
}
// return file1;
}