如何使用jfilechooser保存xls文件?

gxwragnw  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(301)

我有一个方法,负责创建一个xls文件使用apachepoi工作簿,我想保存该文件使用 JFileChooser . 现在我可以使用文件编写器创建该文件并将其保存到预定义的位置。
但要求是使用 JFileChooser ,我不知道该怎么做。
这是我的密码:

public void excelFileCreation() 
{
    try
    {
        String path = "D:/";

        String fileName = "EwayBill.xls";

        String filename = path.concat(fileName);

        Workbook wb = new HSSFWorkbook();

        Sheet sheet = wb.createSheet("Eway Bill");

        Row row1 = sheet.createRow((short)0);
        Row row2 = sheet.createRow((short)0);

        CellRangeAddress transactionDetails = new CellRangeAddress(0, 0, 0, 5);
        sheet.addMergedRegion(transactionDetails);
        row2.createCell(0).setCellValue("Transaction details");

        CellRangeAddress fromConsignorDetails = new CellRangeAddress(0, 0, 6, 13);
        sheet.addMergedRegion(fromConsignorDetails);
        row2.createCell(6).setCellValue("Transaction details");

        Row rowhead = sheet.createRow((short)1);
        rowhead.createCell(0).setCellValue("User GSTIN");
        rowhead.createCell(1).setCellValue("Supply Type");
        rowhead.createCell(2).setCellValue("Sub Type");
        rowhead.createCell(3).setCellValue("Document Type");
        rowhead.createCell(4).setCellValue("Document No");

        Row row = sheet.createRow((short)2);
        row.createCell(0).setCellValue(" ");
        row.createCell(1).setCellValue(" ");
        row.createCell(2).setCellValue(" ");
        row.createCell(3).setCellValue(" ");
        row.createCell(4).setCellValue(" ");

        for(int i=0; i<=79; i++)
        {
            sheet.setColumnWidth(i, 6000);
        }

        FileOutputStream fileOut = new FileOutputStream(filename);
        wb.write(fileOut);

        fileOut.close();

        if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 
        {
          java.io.File file = fileChooser.getSelectedFile();
          // save to file
        }

        System.out.println("Your excel file has been generated!");

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
km0tfn4u

km0tfn4u1#

这个问题的确切解决办法是--->

if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 
 {
    File file = fileChooser.getSelectedFile();

    if (file == null) 
     {
       return;
     }

    // It will assign name to the file with extension .xls
     file = new File(file.getParentFile(), file.getName() + ".xls");  

     FileOutputStream fileOut = new FileOutputStream(file);
     wb.write(fileOut);
     fileOut.close();

 }

感谢安德鲁·汤普森的指导。

ltskdhd1

ltskdhd12#

答案很可能改变这一点:

FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);

    fileOut.close();

    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 
    {
      java.io.File file = fileChooser.getSelectedFile();
      // save to file
    }

对此:

if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 
    {
      java.io.File file = fileChooser.getSelectedFile();
      FileOutputStream fileOut = new FileOutputStream(file);
      wb.write(fileOut);

      fileOut.close();
    }

我之所以说“最有可能”,是因为我不能确定是否会看到mcve/sscce。

相关问题