Apache POI api with JAVA
What is Apache POI?
Apache POI’s cross-platform Open Source Java APIs allow users to read and write various file formats from the Microsoft Office suite of applications, including Word, PowerPoint, Excel, Outlook, Visio, and Publisher.
Apache POI is deployed in many highly-visible environments including CERN, Deutsche Bank, Freddie Mac, IBM, J.P. Morgan, Lawrence Livermore National Laboratory, NASA, SAP, and Siemens, among others. In addition, Apache POI is also used in Open Source projects such as Alfresco, JasperReports and Apache Tika.
“Apache POI’s powerful solutions give users the ability to create and maintain many Office OpenXML (OOXML) and OLE2-based file formats ,With POI, you can do almost anything that you can with Microsoft Office products, only using Java.”
“Apache POI is a vital component of WSO2’s Middleware Platforms and Open PaaS. WSO2 Governance Registry makes use of Apache POI to extract keywords to build its search index for MS Office Documents stored within the resource repository. Meanwhile, WSO2 Data Services Server relies on Apache POI to access MS Excel spreadsheets as Data Sources; making it possible to read data from any Excel Sheet and expose them through Data Services.”
In addition, POI’s robust spreadsheet API enables advanced formatting, graphics, conditional formatting, data validations and evaluation of Excel formulas. Its streaming spreadsheet API increases performance when used for very large spreadsheet production with limited heap space.
The spreadsheet libraries in Apache POI have been invaluable in our efforts to streamline workflows for renewable-energy infrastructure modeling and analysis. The clean and robust API that POI provides made it easy for us to embed externally provided spreadsheets within our applications and to evaluate the formulae within those spreadsheets as part of a larger set of analytic computations. POI opened new avenues for exposing spreadsheets as Web services and for Apache Ant-based regression testing of spreadsheets.
To improve functionality, the Apache POI project often collaborates with other Apache projects and Open Source communities that include Apache Cocoon, Lucene, OpenOffice, and Tika.
Latest Stable Release :
The latest stable release of the project is v3.7 (October 2010) and the latest development version is 3.8beta4 (August 2011). Highlights include:
– the ability to read and write OLE2 files, including .xls, .doc, and .ppt, as well as MFC serialization API based file formats;
– the ability to read and write OOXML files, including .xlsx, .docx, and .pptx;
– a low-level API to support Open Packaging Conventions using openxml4j;
– highly-developed Java APIs for Excel workbooks, Word documents, and PowerPoint presentations;
– support for Outlook messages and attachments;
– converters for Excel and Word documents to streamline document production and consumption in HTML and XSLF-FO formats;
and
– porting other OOXML and OLE2 formats.
Office Open XML support
POI supports the ISO/IEC 29500:2008 Office Open XML file formats since version 3.5. A significant contribution for OOXML support came from Sourcesense, an open source company which was commissioned by Microsoft to develop this contribution. spurred controversy, some POI contributors questioning POI OOXML patent protection regarding Microsoft’s Open Specification Promise patent license.
Architecture
The Apache POI project contains the following subcomponents:
- POIFS (Poor Obfuscation Implementation File System) – This component reads and writes Microsoft‘s OLE 2 Compound document format. Since all Microsoft Office files are OLE 2 files, this component is the basic building block of all the other POI elements. POIFS can therefore be used to read a wider variety of files, beyond those whose explicit decoders are already written in POI.
- HSSF (Horrible SpreadSheet Format) – reads and writes Microsoft Excel (XLS) format files. It can read files written by Excel 97 onwards; this file format is known as the BIFF 8 format. As the Excel file format is complex and contains a number of tricky characteristics, some of the more advanced features cannot be read.
- XSSF (XML SpreadSheet Format) – reads and writes Office Open XML (XLSX) format files. Similar feature set to HSSF, but for Office Open XML files.
- HPSF (Horrible Property Set Format) – reads “Document Summary” information from Microsoft Office files. This is essentially the information that one can see by using the File|Properties menu item within an Office application.
- HWPF (Horrible Word Processor Format) – aims to read and write Microsoft Word 97 (DOC) format files. This component is in initial stages of development.
- HSLF (Horrible Slide Layout Format) – a pure Java implementation for Microsoft PowerPoint files. This provides the ability to read, create and edit presentations (though some things are easier to do than others)
- HDGF (Horrible DiaGram Format) – an initial pure Java implementation for Microsoft Visio binary files. It provides an ability to read the low level contents of the files.
- HPBF (Horrible PuBlisher Format) – a pure Java implementation for Microsoft Publisher files.
- HSMF (Horrible Stupid Mail Format) – a pure Java implementation for Microsoft Outlook MSG files.
- DDF (Dreadful Drawing Format) – a package for decoding the Microsoft Office Drawing format.
The HSSF component is the most advanced feature of the library. Other components (HPSF, HWPF, and HSLF) are usable, but less full-featured.
The POI library is also provided as a Ruby or ColdFusion extension.
Working with .doc format :
This below program simply explains how to read data from the MS wordfile(.DOC) line by line using Apache POI.
for executing this program we need to download Apache POI api and make jar files in classpath.
package multidocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
/**
*
* @author vijay
*/
public class NewDocReader {
public static void main(String args[]) throws FileNotFoundException, IOException
{
File docFile=new File(“c:\\multi\\multi.doc”); // file object created
FileInputStream finStream=new FileInputStream(docFile.getAbsolutePath()); // file input stream with docFile
HWPFDocument doc=new HWPFDocument(finStream);// throws IOException and need to import org.apache.poi.hwpf.HWPFDocument;
WordExtractor wordExtract=new WordExtractor(doc); // import org.apache.poi.hwpf.extractor.WordExtractor
String [] dataArray =wordExtract.getParagraphText();
// dataArray stores the each line from the document
for(int i=0;i<dataArray.length;i++)
{
System.out.println(“\n–”+dataArray[i]);
// printing lines from the array
}
finStream.close(); //closing fileinputstream
}
}
