gpt4 book ai didi

java - 如何使用 JFileChooser 在 Java 中保存文件

转载 作者:行者123 更新时间:2023-11-29 05:14:55 26 4
gpt4 key购买 nike

我正在使用 java swing 开发一种基于桌面的软件,它需要以 pdf 和 excel 文件的形式导出报告,并将接受来自用户的路径。我有 pdf 和 excel 文件的导出报告,但现在我想接受来自用户的路径,并且用户必须为该文件命名。

try
{
String filename="sales111.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("Sales Report in Excel");

HSSFRow rowhead= sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Invoice Numberr");
rowhead.createCell((short) 1).setCellValue("date");
rowhead.createCell((short) 2).setCellValue("Customer Name");
rowhead.createCell((short) 3).setCellValue("customer Code");
rowhead.createCell((short) 4).setCellValue("Stock Item Name");
rowhead.createCell((short) 5).setCellValue("Product Quantity");
rowhead.createCell((short) 6).setCellValue("Product Rate");
rowhead.createCell((short) 7).setCellValue("Total Amount");
rowhead.createCell((short) 8).setCellValue("Tax Category");
rowhead.createCell((short) 9).setCellValue("Tax Amount");
rowhead.createCell((short) 10).setCellValue("Transport Charges");
rowhead.createCell((short) 11).setCellValue("Net Amount");
rowhead.createCell((short) 11).setCellValue("Credit Limit");

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/BOA", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from SalesVoucher");
int i=1;

while(rs.next())
{
HSSFRow row= sheet.createRow((short)i);
row.createCell((short) 0).setCellValue(rs.getString("innum"));
row.createCell((short) 1).setCellValue(rs.getString("date"));
row.createCell((short) 2).setCellValue(rs.getString("scname"));
row.createCell((short) 3).setCellValue(rs.getString("sccode"));
row.createCell((short) 4).setCellValue(rs.getString("stname"));
row.createCell((short) 5).setCellValue(Integer.toString(rs.getInt("pquantity")));
row.createCell((short) 6).setCellValue(Double.toString(rs.getDouble("prate")));
row.createCell((short) 7).setCellValue(Double.toString(rs.getDouble("samount")));
row.createCell((short) 8).setCellValue(Double.toString(rs.getDouble("staxcat")));
row.createCell((short) 9).setCellValue(Double.toString(rs.getDouble("stamount")));
row.createCell((short) 10).setCellValue(Double.toString(rs.getDouble("strans")));
row.createCell((short) 11).setCellValue(Double.toString(rs.getDouble("stota")));
row.createCell((short) 11).setCellValue(Integer.toString(rs.getInt("scredlim")));

i++;
}

FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
System.out.println("Your Sales Report Excel file has been generated!");

String name1="";

FileSave(filename,name1);
}
catch ( Exception ex )
{
System.out.println(ex);
}
}
public void FileSave(final String title,final String name)
{
final JFileChooser chooser=new JFileChooser();
// chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setDialogTitle(title);
chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.dir")));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(final File f)
{
return f.isDirectory();
}
public String getDescription(){
return "Folder To Save In";
}
}
);
final int r=chooser.showSaveDialog(null);
File file;

if (r == JFileChooser.APPROVE_OPTION)
{
if (name != null)
{
file=new File(chooser.getSelectedFile().getPath() + File.separator + name);
}
else
{
// file=new File(filename);
file=new File(chooser.getSelectedFile().getPath());
}
}
}

这是我的代码,它创建 excel 文件并仅在当前目录中保存文件,并且不接受用户的文件名。

有人可以给我建议吗?

最佳答案

您只需在 FileSave 中创建一个空文件。

您必须从该方法返回选定的文件名并在调用中使用返回的文件名

FileOutputStream fileOut =  new FileOutputStream(filename)

BWT java 代码约定假设方法名不以大写字母开头

关于java - 如何使用 JFileChooser 在 Java 中保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26925275/

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