Android读取word文件

Android中读取word文件需要用第三方jar包tm-extractors-0.4.jar。

[java]
package com.word.read;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.textmining.text.extraction.WordExtractor;

import Android.app.Activity;
import Android.os.Bundle;
import Android.os.Environment;
import Android.widget.TextView;

public class WordReader extends Activity {
/** Called when the activity is first created. */

private TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text = (TextView) findViewById(R.id.text);

String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "/baojinggong.doc");
text.setText(str.trim().replace("\r", ""));
}

public String readWord(String file){
//创建输入流用来<a title="读取doc文件" href="http://www.android-study.com/pingtaikaifa/38.html">读取doc文件</a>
FileInputStream in;
String text = null;
try {
in = new FileInputStream(new File(file));
WordExtractor extractor = null;
//创建WordExtractor
extractor = new WordExtractor();
//进行提取对doc文件
text = extractor.extractText(in);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return text;
}
}
[/java]

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>