简体   繁体   English

如何使用JFileChooser保存XLS文件?

[英]How to save XLS file using JFileChooser?

I have method which is responsible to create an XLS file using Apache POI workbook and I want to save that file using JFileChooser . 我有负责使用Apache POI工作簿创建XLS文件的方法,并且我想使用JFileChooser保存该文件。 Right now I can able to create that file using file writer and save it to a pre-defined location. 现在,我可以使用文件编写器创建该文件并将其保存到预定义的位置。

But the requirement is to save that file using JFileChooser , and I am not able to understand how to do it. 但是要求是使用JFileChooser保存该文件,而我不知道该怎么做。

Here is my code: 这是我的代码:

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();
    }
}

The answer is most likely to change this: 答案最有可能改变这种情况:

    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
    }

To this: 对此:

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

      fileOut.close();
    }

I say 'most likely' because I cannot be sure short of seeing an MCVE / SSCCE. 我说“最有可能”是因为我不确定是否会看到MCVE / SSCCE。

The Exact working Solution for the Question is ---> 该问题的确切可行解决方案是--->

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();

 } 

Thanks Andrew Thompson for guidance. 感谢安德鲁·汤普森(Andrew Thompson)的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM