如何在android中从外部存储中读取excel文件?

时间:2017-07-24 10:39:04

标签: java android excel

我写了一个代码,它将从excel文件(.xls)生成图形,它可以完美地工作,但我必须将文件放在assets文件夹中。现在我想从外部存储器访问文件。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pratiksonigra.graphexcel.MainActivity">

<com.jjoe64.graphview.GraphView
    android:layout_width="wrap_content"
    android:layout_height="350dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    android:id="@+id/graph1"/>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Refresh"
    android:layout_below="@+id/graph1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp" />

JAVA代码:

package com.example.pratiksonigra.graphexcel;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.BarGraphSeries;
import com.jjoe64.graphview.series.DataPoint; 
import com.jjoe64.graphview.series.LineGraphSeries;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class MainActivity extends AppCompatActivity {
LineGraphSeries<DataPoint> series;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Button btn = (Button) findViewById(R.id.button);
    final GraphView graph = (GraphView) findViewById(R.id.graph1);
    graph.getViewport().setXAxisBoundsManual(true);
    graph.getViewport().setYAxisBoundsManual(true);
    graph.getViewport().setMinX(0);
    graph.getViewport().setMaxX(10);
    graph.getViewport().setMaxY(40);
    graph.getViewport().setMinY(0);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                graph.removeAllSeries();
                AssetManager am = getAssets();
                InputStream is = am.open("book.xls");
                Workbook wb = Workbook.getWorkbook(is);
                Sheet s = wb.getSheet(0);
                Cell z = s.getCell(0, 0);
                int row = s.getRows();
                int col = s.getColumns();
                double x, y;
                x = 0.0;
                y = 0.0;

                series = new LineGraphSeries<DataPoint>();

                for (int i = 0; i < row; i++) {
                    for (int j = 0; j < col; j++) {
                        z = s.getCell(j, i);
                        if (j % 2 == 0) {
                            x = Double.parseDouble(z.getContents());
                        } else {
                            y = Double.parseDouble(z.getContents());
                        }
                    }
                    series.appendData(new DataPoint(x, y), true, 15);
                }
                graph.addSeries(series);
            } catch (Exception e) {

            }
        }
    });
}

public boolean onSupportNavigateUp() {
    finish();
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

据我了解您的问题,您希望从手机内部存储中动态选择文件。

然后,您需要在应用中使用文件浏览器,以允许用户浏览并选择文件。

请参阅:https://github.com/vaal12/AndroidFileBrowser

希望这有帮助。