Android下利用Apache POI 開啟DOC 文檔

來源:互聯網
上載者:User

有孩紙在做軟體設計大賽的一題目,需要能查看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,還有很重要的就是就是在如下配置中勾選這兩個包,否者在編譯時間可能沒問題,運行時卻會提示無法解析 啥的。

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() {@Overridepublic 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. 貼運行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.