Open the DOC file using Apache POI in Android

Source: Internet
Author: User

There is a child paper in the software design competition, you need to be able to view the doc and various office documents, search will naturally see poi, it is said that the poi document is not well written, however, others seem to be either too simple or too complicated.

After the operation, the file is opened. Record it.

1. Poi project homepage: Click to open the poi Homepage

Apache poi-the Java API for Microsoft documents ents

2. download the latest 3.9 bin and SRC.

3. I saw a section of component on the home page, but it was not very good at English. The general idea is that not all packages need to be imported in all circumstances. (indeed, two packages were imported during the doc operation, and the generated APK reached 5.x m ).

Component Map

The Apache poi Distribution consists of support for your 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 Poi-scratchpad
Hwpf Word doc Poi-scratchpad
Hdgf Visio Poi-scratchpad
Hpbf Publisher pub Poi-scratchpad
HSMF Outlook msg Poi-scratchpad
Xssf Excel XLSX Poi-ooxml
Effecf 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. previusly 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 shother work fine though.

That is to say, if I want to operate the DOC file, I only need to reference two jar files: poi-scratchpad and poi.


4. configuration.

To create and import a jar package, check the two packages in the following configuration. Otherwise, the package may not be resolved during compilation.

5. paste the Code:


/** 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) {// Catch Block E automatically generated by todo. printstacktrace ();} Try {Doc = new hwpfdocument (fileinputs);} catch (ioexception e) {// Catch Block E automatically generated by todo. 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. getran Ge (); 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) {// method stub doc_text.settext (content) automatically generated by todo; // doc_text.settext ("hello !!! "); New alertdialog. Builder (doc3.this). settitle (" Tips "). setmessage (content). Show ();}});}

6. paste and run.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.