Excel file access from internal storage android ap

2019-08-03 19:10发布

问题:

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

回答1:

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