Excel file access from internal storage android app

最后都变了- 提交于 2019-12-13 00:56:02

问题


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


来源:https://stackoverflow.com/questions/18271159/excel-file-access-from-internal-storage-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!