2012-10-10 107 views
1

可能重複:
how to read doc and excel file in android?如何在Android中讀取SD卡中的.doc文件?

我保存在我的SD卡.doc文件。我需要讀取.doc文件的內容並在TextView中顯示它。

誰能告訴我如何做到這一點?

+0

我建議你使用Apache POI。你可以將這個單詞轉換爲html並渲染它http://poi.apache.org/ –

+0

你可以使用** jOpenDocument **。您可以參考以下鏈接:http://stackoverflow.com/questions/7790503/how-to-read-write-doc-and-excel-file-in-android http://www.jopendocument.org/ – kittu88

+0

這裏顯示.doc擴展文件的碼中的SD卡中存在[代碼來顯示的.pdf,的.text,.DOC,.DOCX,.doc擴展文件在SD卡存在(http://pavantilak.blogspot.in/2012/02/碼到顯示pdftextdocdocxdoc.html)請參閱此鏈接http://stackoverflow.com/questions/12816281/how-to-read-doc-and-excel-file-in-android希望它會幫助你... –

回答

0

一個解決方案是使用Apache POI Java庫,用於解析.doc文件。

爲了讓SD卡中的Android上的一個文件,你可以使用類似

new File(getExternalFilesDir(null), "word.doc"); 
+0

如何解析.docx文件? –

2
public void onCreate(Bundle b){ 
    super.onCreate(b); 
     setContentView(R.layout.main); 
     String extPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.Separator; 
    InputStream inputStream = assetManager.open(extPath + "file.doc"); 
    String text = loadFile(inputStream); 
    TextView tv = (TextView)findViewById(R.id.txtv); 
    tv.setText(text); 



} 



public String loadFile(InputStream inputStream){ 
ByteArrayOutputStream b = new ByteArrayOutputStream(); 
byte[] bytes = new byte[4096]; 
int length = 0; 
    while(){ 
    b.write(bytes, 0, length); 
    } 
return new String(b.toByteArray(), "UTF8"); 
} 
0

試試這個代碼

File file=new File("/sdcard/word.doc"); 
         if(file.exists()) 
         { 
           Uri path=Uri.fromFile(file); 
           Intent intent=new Intent(Intent.ACTION_VIEW); 
           intent.setDataAndType(path, "application/doc"); 

           try 
           { 

            startActivity(intent); 
           } 
           catch(ActivityNotFoundException e) 
           { 
            Toast.makeText(TestActivity.this, "No software for Doc", Toast.LENGTH_SHORT).show(); 
           } 
         } 
+0

您好,先生,這段代碼不起作用startActivity。 –