您的位置:首页 > 运维架构 > Apache

[置顶] Android下利用Apache POI 打开DOC 文档

2016-08-03 10:02 274 查看
有孩纸在做软件设计大赛的一题目,需要能查看Doc 和各种office 文档,搜索自然便看到了POI,据说POI 文档写的不好,不过其他的貌似不是太简单,就是太复杂。

操作了一下,把文件打开了。记录一下。

1. POI 项目主页:点击打开POI主页

主页上很大的几个单词: Apache POI - the Java API for Microsoft Documents

2. 下载了最新的 3.9 的 bin 和 src 。

3. 看见主页里有 component 一段,看了下,英语也不大好,大意是说什么不是所有情况下所有的包都需要被导入,(确实,操作DOC时就导入了两个包,生成后的APK竟然达到5.X M)。


Component Map

The Apache POI distribution consists of support for many document file formats. This support is provided in several Jar files. Not all of the Jars are needed for every format. The following tables show the relationships between POI components, Maven repository
tags, and the project's Jar files.
ComponentApplication typeMaven artifactId
POIFSOLE2 Filesystempoi
HPSFOLE2 Property Setspoi
HSSFExcel XLSpoi
HSLFPowerPoint PPTpoi-scratchpad
HWPFWord DOCpoi-scratchpad
HDGFVisio VSDpoi-scratchpad
HPBFPublisher PUBpoi-scratchpad
HSMFOutlook MSGpoi-scratchpad
XSSFExcel XLSXpoi-ooxml
XSLFPowerPoint PPTXpoi-ooxml
XWPFWord DOCXpoi-ooxml
OpenXML4JOOXMLpoi-ooxml-schemas, ooxml-schemas
This table maps artifacts into the jar file name. "version-yyyymmdd" is the POI version stamp. For the latest release it is 3.6-20091214.
Maven artifactIdPrerequisitesJAR
poicommons-logging, commons-codec, log4jpoi-version-yyyymmdd.jar
poi-scratchpadpoipoi-scratchpad-version-yyyymmdd.jar
poi-ooxmlpoi, poi-ooxml-schemas, dom4jpoi-ooxml-version-yyyymmdd.jar
poi-ooxml-schemas (*)xmlbeans, stax-api-1.0.1poi-ooxml-schemas-version-yyyymmdd.jar
poi-examples (*)poi, poi-scratchpad, poi-ooxmlpoi-examples-version-yyyymmdd.jar
ooxml-schemasxmlbeans, stax-api-1.0.1ooxml-schemas-1.1.jar
(*) starting with 3.6-beta1-20091124.

poi-ooxml requires poi-ooxml-schemas. This is a substantially smaller version of the ooxml-schemas jar (ooxml-schemas-1.1.jar for POI 3.7 or later, ooxml-scheams-1.0.jar for POI 3.5 and 3.6). The larger ooxml-schemas jar is normally only
required for development

The OOXML jars require a stax implementation. Previously we suggested "geronimo-stax-api_1.0_spec", but now "stax-api-1.0.1" is used for better compatibilty with other Apache projects. Any compliant implementation should work fine though.
也就是说,如果我要操作 DOC 文档的话 ,只需要引用两个jar就可以了:poi-scratchpad 和 poi。

4. 配置 。

新建,导入jar,还有很重要的就是就是在如下配置中勾选这两个包,否者在编译时可能没问题,运行时却会提示无法解析 啥的。



5. 贴代码:

/*
* Fuction:	openDocFormPath
* Open a file use file path
* Input: 	pathname
* Return:	A HWPFdocument object
* Author:	j.ghang@hotmail.com 2013.05.22
*/
private HWPFDocument openDocFromPath(String pathname) {
File docFile = null;
InputStream fileInputS = null;
HWPFDocument doc = null;
docFile	= new File(pathname);
try {
fileInputS = new FileInputStream(docFile);
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

try {
doc = new HWPFDocument(fileInputS);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return doc;
}


public class Doc3 extends Activity {

private TextView doc_text = null;
private Button btn_show = null;
private String pathname = "/mnt/sdcard/test.doc";
private String content = "content";

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

HWPFDocument doc = null;
doc = openDocFromPath(pathname);

Range r = doc.getRange();
content = r.text();
r.delete();

doc_text = new TextView(this);
btn_show = new Button(this);
this.btn_show = (Button)super.findViewById(R.id.btn_show); //
this.doc_text = (TextView)super.findViewById(R.id.hello);//

this.btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
doc_text.setText(content);//doc_text.setText("hello hello!!!");
new AlertDialog.Builder(Doc3.this).setTitle("Tips").setMessage(content).show();

}
});

}


6. 贴运行效果图。





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: