2017-01-07 3 views
4

이 코드를 작성하여 자산 폴더 (내 파일 이름 : book.xls)에 붙여 넣으면 Excel 파일을 읽을 수 있습니다. 하지만 버튼을 누르면 파일이 보이지 않고 아무 것도 보여주지 않습니다. 제 문제를 해결하도록 도와주세요. 고마워요. 여기 내 코드는 다음 display() 방법은 결코 당신의 내면의 for 루프로android.java에서 Excel 파일 읽기

<?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>

+1

이 문제를 해결했으면 다음으로 고려해야 할 것은 "왜 Excel 파일입니까?"입니다. 파일이 자산 폴더에 임베드되어있어 사용자가 변경할 수없는 경우이를 .csv 파일과 같은보다 친숙한 형식으로 변환하거나 Java로 데이터 클래스를 만드는 것이 좋습니다. 이 데이터? 그런 다음 Excel 파일과 Jexcel 라이브러리를 모두 생략하면 APK를 훨씬 더 작게 만들 수 있습니다. – GreyBeardedGeek

답변

1

라는 결코 극복 : 여기

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이다 라인 때문에 끝납니다 :

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

변경이 다음과

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

(즉 'c'가 'i'와 반대로 col보다 <인지 확인하고 TextView 값이 의도 한대로 변경됩니다.

+0

정말 고마워요. –