0

i'm creating a excel file by FileOutputStream.

    private boolean saveExcelFile(Context context, final String fileName) {

        if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
            checkRunTimePermission();
            return false;
        }
        final boolean[] success = {false};
        GetAllPeople(new VolleyCallBack() {
            @Override
            public void onSuccess() {
                Workbook wb = new HSSFWorkbook();
                Cell c = null;
                CellStyle cs = wb.createCellStyle();
                Sheet sheet1 = null;
                sheet1 = wb.createSheet("مخاطبین");
                Row row = sheet1.createRow(0);
                c = row.createCell(0);
                c.setCellValue("شماره تماس");
                c = row.createCell(1);
                c.setCellValue("نام و نام خانوادگی");

             
               File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                if (!root.exists()) {
                    root.setReadable(true);
                    root.setWritable(true);
                    root.mkdirs();
                }

                File file = new File(root, fileName);
                FileOutputStream os = null;

                try {
                    os = new FileOutputStream(file);
                    wb.write(os);
                    success[0] = true;
                } catch (IOException e) {
                } catch (Exception e) {
 
                } finally {
                    try {
                        if (null != os)
                            os.close();
                    }
                    catch (Exception ex) {
                        
                    }
                }

            }
        });
        return success[0];
    }

note:i Added storage permission on Manifest. file is creating successfully and without any errors. i'm searching file name in file explorer but I can't find this file.

radin
  • 251
  • 3
  • 16
  • Have you checked the `root` folder? Otherwise try to create the file on your desktop – XtremeBaumer Jul 01 '20 at 07:11
  • if (!root.mkdirs()){Toast(..sorry could not create dir..); return;}. Please adapt your code. – blackapps Jul 01 '20 at 07:12
  • Which file explorer are you talking about? – blackapps Jul 01 '20 at 07:12
  • You should print the stack trace in all catch blocks. And display toasts so the use is informed. – blackapps Jul 01 '20 at 07:14
  • `if (!root.exists()) { root.setReadable(true); root.setWritable(true);` If root does not exists then you cannot make it readable or what else as it does not exist yet. – blackapps Jul 01 '20 at 07:17
  • you are missing `wb.close();` after `wb.write(s);` – XtremeBaumer Jul 01 '20 at 07:19
  • @XtremeBaumer yes, i checked all folder. – radin Jul 01 '20 at 07:23
  • @blackapps file is creating successfully and without any errors. i clean toast for Shorthand – radin Jul 01 '20 at 07:25
  • @XtremeBaumer i used os.close(); in finally. – radin Jul 01 '20 at 07:28
  • How do you know that the file exists if you cannot even find it? Which file explorer? I asked that before. If you can call wb.close() then you should do that. Its quite different from os.close(). Please tell full path of file. – blackapps Jul 01 '20 at 07:33
  • @blackapps google files. i can't call wb.close(). path: /storage/emulated/0/Pictures/my.xls – radin Jul 01 '20 at 07:59
  • If the Files app does not show your file in that Pictures directory then your file does not exist and was not created. Why would you store an xls file in the Pictures directory? – blackapps Jul 01 '20 at 08:38
  • `can't call wb.close().`. Why not? Please give better info. – blackapps Jul 01 '20 at 08:39
  • @blackapps i'm using this lib: https://programchi.ir/wp-content/uploads/2018/03/poi-3.7.zip. and this lib don't have close method. i chose picture folder for testing. – radin Jul 01 '20 at 09:00
  • Firstly, why use that old versions? HSSF is old. Use XSSF instead. Also Apache poi is already at version 4.1.2. The version you use is 10 years old. Secondly, I am very sure that even that version has `workbook.close()` – XtremeBaumer Jul 01 '20 at 11:55
  • Seems my second point was wrong after all... read https://stackoverflow.com/questions/12261014/close-filehandle-for-workbook-apache-poi – XtremeBaumer Jul 01 '20 at 12:01
  • @XtremeBaumer https://pasteboard.co/JfD8LMS.jpg but this ver. dont have wb.close() – radin Jul 01 '20 at 12:07

1 Answers1

0

I updated the library. Thanks to @XtremeBaumer . Finally, I changed the address of the Excel file.

       dialog = LoadingDialog.Companion.get(PeopleActivity.this).show();
       GetAllPeople(new VolleyCallBack() {
           @Override
           public void onSuccess() {
               Workbook wb = new HSSFWorkbook();
               Cell cell = null;
               CellStyle cellStyle = wb.createCellStyle();

               Sheet sheet = null;
               sheet = wb.createSheet("مخاطبین من");

               Row row = sheet.createRow(0);
               cell = row.createCell(0);
               cell.setCellValue("شماره تماس");
               cell = row.createCell(1);
               cell.setCellValue("نام و نام خانوادگی");
               cell = row.createCell(2);
               cell.setCellValue("مجموعه ها");

               final Sheet finalSheet = sheet;
               for(int i =1;i<=Expeooles.size();i++){
                   row = sheet.createRow(i);
                   cell = row.createCell(0);
                   cell.setCellValue(Expeooles.get(i-1).getPhone());
                   cell = row.createCell(1);
                   cell.setCellValue(Expeooles.get(i-1).getName());
                   cell = row.createCell(2);
                   cell.setCellValue(Expeooles.get(i-1).getCharity());
               }

               File root = android.os.Environment.getExternalStorageDirectory();
               File file = new File(root.getAbsolutePath() + "/download","myfile.xls");
               FileOutputStream outputStream = null;
               try {
                   outputStream = new FileOutputStream(file);
                   wb.write(outputStream);
                   Toasty.success(PeopleActivity.this,"فایل اکسل با موفقیت در پوشه دانلود ایجاد شد.",Toasty.LENGTH_SHORT,true).show();
               }catch (Exception ex){
                   Toasty.error(PeopleActivity.this,ex.getMessage(),Toasty.LENGTH_LONG,true).show();
               }
               finally {

                   try {
                       outputStream.close();
                       wb.close();
                   } catch (IOException e) {
                       e.printStackTrace();
                       Toasty.error(PeopleActivity.this,e.getMessage(),Toasty.LENGTH_LONG,true).show();
                   }
               }
               dialog.hide();
           }
       });

   }```

I guess the main problem was the address I set for the file.
radin
  • 251
  • 3
  • 16