0

I have created a excel file Programmatically.

HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet firstSheet = workbook.createSheet("Sheet No: 1");
        HSSFSheet secondSheet = workbook.createSheet("Sheet No: 2");
        HSSFRow rowA = firstSheet.createRow(0);
        HSSFCell cellA = rowA.createCell(0);
        cellA.setCellValue(new HSSFRichTextString("Sheet One"));
        HSSFRow rowB = secondSheet.createRow(0);
        HSSFCell cellB = rowB.createCell(0);
        cellB.setCellValue(new HSSFRichTextString("Sheet two"));
        FileOutputStream fos = null;
        try {
            String str_path = Environment.getExternalStorageDirectory().toString();
            File file ;
            file = new File(str_path, getString(R.string.app_name) + ".xls");
            fos = new FileOutputStream(file);
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Toast.makeText(MainActivity.this, "Excel Sheet Generated", Toast.LENGTH_SHORT).show();
        }

I do not want to keep this file in the device storage as it's Programmatically created. I want to upload this file to the PHP server. How can it be done?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Omkar Ghurye
  • 195
  • 1
  • 8
  • why do you say that it doesn't exist? You created it – kingston Mar 27 '21 at 18:19
  • @kingston I do not want to keep this file in the device storage. I want to upload the programmatically created file to the PHP server. – Omkar Ghurye Mar 27 '21 at 18:29
  • Yes, you can delete it after you send it. To find how to send it, google "retrofit post multipart". For example https://stackoverflow.com/a/38891018/987753. Of course this is necessary only if you need to create the file on the file system. Otherwise you can just post a buffer. – kingston Mar 28 '21 at 10:17

1 Answers1

1

If the file is created in your local storage and you want to upload the file on php server then you need to make an POST request api from server side then call it in your application and pass your data to the api, that way you can send your file to store on any server you want

Shailendra
  • 391
  • 1
  • 10
  • 17
  • well, I don't know how to call an api and send data to it in android because I am working in laravel framework (php) and i developed api in laravel to get and store data but if you want to test an api in android then you can search for public api for testing for developers in google for example you can visit **https://jsonplaceholder.typicode.com/** – Shailendra Mar 28 '21 at 07:53