1

I want to be able to copy an excel file from my android app res\raw folder to the client phone's internal storage, and be able to retrieve the data from the excel file and update the excel file in the internal storage as needed. So far all I have is this:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal --- write files to internal storage and

http://www.vogella.com/articles/JavaExcel/article.html --- using jexcel to retrieve data from and edit an excel file

I can't seem to be able to link the two. How do I go on implementing this app?

Thanks

Ahmed Zafar
  • 665
  • 2
  • 10
  • 24

1 Answers1

3

For Read and Write To use Apache POI library

To some sample example is there for android through read and write excel sheet

1) Creating/Reading an Excel file in Android

2) Android Read Write EXCEL file using Apache POI

For Assets to SDcard Follow this link Or use this code.

    package com.paresh.copyfileassetstoAssets;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import android.app.Activity;
    import android.content.res.AssetManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;

    public class CopyFileAssetsToSDCardActivity extends Activity 
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

           CopyAssets();
        }

        private void CopyAssets() {
            AssetManager assetManager = getAssets();
            String[] files = null;
            try {
                files = assetManager.list("Files");
            } catch (IOException e) {
                Log.e("tag", e.getMessage());
            }

            for(String filename : files) {
                System.out.println("File name => "+filename);
                InputStream in = null;
                OutputStream out = null;
                try {
                  in = assetManager.open("Files/"+filename);   // if files resides inside the "Files" directory itself
                  out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
                  copyFile(in, out);
                  in.close();
                  in = null;
                  out.flush();
                  out.close();
                  out = null;
                } catch(Exception e) {
                    Log.e("tag", e.getMessage());
                }
            }
        }
        private void copyFile(InputStream in, OutputStream out) throws IOException {
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
              out.write(buffer, 0, read);
            }
        }
    }
Amit Prajapati
  • 13,525
  • 8
  • 62
  • 84
  • Thanks for the help but the issue I am having is different. Even though I have now stored the excel file to internal storage, I need to retrieve data from the excel file. I know how to read the file in internal storage just now how to get data from it using the Apache POI library. Help needed. Thanks. – Ahmed Zafar Aug 16 '13 at 11:51
  • Even though I'm going for internal memory rather than external this answer helped out a lot! Thanks! – Ahmed Zafar Aug 16 '13 at 15:26