Android下利用Apache POI 打开DOC 文档


有孩纸在做软件设计大赛的一题目,需要能查看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.

Component Application type Maven artifactId
POIFS OLE2 Filesystem poi
HPSF OLE2 Property Sets poi
HSSF Excel XLS poi
HSLF PowerPoint PPT poi-scratchpad
HWPF Word DOC poi-scratchpad
HDGF Visio VSD poi-scratchpad
HPBF Publisher PUB poi-scratchpad
HSMF Outlook MSG poi-scratchpad
XSSF Excel XLSX poi-ooxml
XSLF PowerPoint PPTX poi-ooxml
XWPF Word DOCX poi-ooxml
OpenXML4J OOXML poi-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 artifactId Prerequisites JAR
poi commons-logging, commons-codec, log4j poi-version-yyyymmdd.jar
poi-scratchpad poi poi-scratchpad-version-yyyymmdd.jar
poi-ooxml poi, poi-ooxml-schemas, dom4j poi-ooxml-version-yyyymmdd.jar
poi-ooxml-schemas (*) xmlbeans, stax-api-1.0.1 poi-ooxml-schemas-version-yyyymmdd.jar
poi-examples (*) poi, poi-scratchpad, poi-ooxml poi-examples-version-yyyymmdd.jar
ooxml-schemas xmlbeans, stax-api-1.0.1 ooxml-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,还有很重要的就是就是在如下配置中勾选这两个包,否者在编译时可能没问题,运行时却会提示无法解析 啥的。


Android下利用Apache POI 打开DOC 文档_第1张图片


5. 贴代码:


/* 
     * Fuction:	openDocFormPath
     * Open a file use file path
     * Input: 	pathname 
     * Return:	A HWPFdocument object
     * Author:	[email protected] 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. 贴运行效果图。


Android下利用Apache POI 打开DOC 文档_第2张图片

Android下利用Apache POI 打开DOC 文档_第3张图片

Android下利用Apache POI 打开DOC 文档_第4张图片














你可能感兴趣的:(学习,安卓)