在android.java中读取Excel文件 [英] Read Excel file in android.java

查看:44
本文介绍了在android.java中读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写此代码以读取Excel文件,然后将其粘贴到资产文件夹(我的文件名:book.xls)中以进行读取.但是,当我按下按钮显示文件时,它不起作用,也不显示任何东西.请帮助我解决我的问题.非常感谢.这是我的代码:

i write this code to read an Excel file which i paste it in assets folder (my file name:book.xls) to read it. but when i press button to show file it doesn't work and doesn't show anything. please help me to solve my issue. Thanks a lot. here is my code:

package com.example.android.readingexcellfile;

import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.InputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void order (View v){

        try

        {
            AssetManager am=getAssets();
            InputStream is=am.open("book.xls");
            Workbook wb=Workbook.getWorkbook(is);
            Sheet s=wb.getSheet(0);
            int row=s.getRows();
            int col=s.getColumns();

            String xx="";
            for (int i=0;i<row;i++)
            {

                for(int c=0;i<col;c++)
                {
                    Cell z=s.getCell(c,i);
                    xx=xx+z.getContents();

                }

                xx=xx+"\n";
            }
            display(xx);
        }

        catch (Exception e){}




    }
    public void display (String value){

        TextView x=(TextView)findViewById(R.id.textView);
        x.setText(value);

    }


}

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.readingexcellfile.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:layout_marginTop="69dp"
        android:onClick="order"
        android:id="@+id/button" />
</RelativeLayout>

推荐答案

display()方法永远不会被调用,因为您的内部 for 循环不会因为该行而结束:

The display() method never gets called as your inner for loop never ends because of the line:

for(int c = 0; i< col; c ++)

将此更改为:

for(int c = 0; c< col; c ++)

(即检查'c'是< 而不是 col 而不是'i'),并且 TextView 值更改为

(i.e. check that 'c' is < than col as opposed to 'i') and the TextView value is changed as intended.

这篇关于在android.java中读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆