The Apache Poi:java program reads and writes Microsoft Office format documents--Simple and complete examples __java

Source: Internet
Author: User
Tags microsoft outlook
1.Apache POI IntroductionApache POI is the Apache Software Foundation's open source function library, the official name is: Apache poi-the Java API for Microsoft Documents,poi provides APIs to Java programs for Microsoft The ability to read and write office-formatted documents. NET developers can use Npoi (POI for. net) to access the functionality of the POI.


2.Apache POI Structure

HSSF-provides the ability to read and write Microsoft Excel xls format files.

XSSF-provides the ability to read and write Microsoft Excel OOXML xlsx format files. HWPF-provides the ability to read and write Microsoft Word doc format files.

HSLF-provides the ability to read and write Microsoft PowerPoint format files. HDGF-provides the ability to read Microsoft Visio format files.

HPBF-provides the ability to read Microsoft Publisher format files.

HSMF-provides the ability to read Microsoft Outlook format files. 3.Apache POI Application Example:

<1> first need to download the POI jar file from Apache website;

<2> after you have configured the jar file, you can run the following instance

Here, just a simple example of reading and writing Excel tables shows:


Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFRow;

Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Java.io.FileInputStream; Import java. io.
FileOutputStream;
	public class Createexcel {/* Excel file location to store: D disk/public static String outputfile= "D:\\test.xls";
	public static void Main (String argv[]) {readsheet ();
	//Demonstrates how to use the Jakarta POI API to create an Excel document.
			public static void Createsheet () {try{//Create a new Excel workbook Hssfworkbook workbook = newer hssfworkbook ();
			If you want to create a "benefit indicator" worksheet, the statement is: hssfsheet sheet = workbook.createsheet ("benefit indicators");
			
			Create a worksheet in an Excel workbook with the name default value,//hssfsheet sheet = Workbook.createsheet ();
			Creates a row at index 0 (topmost row) Hssfrow row = Sheet.createrow (0);
			Creates a cell (upper left) at the position of index 0 Hssfcell cell =row.createcell (0);
			Defines a cell as a string type Cell.setcelltype (hssfcell.cell_type_string);
			
			
			Enter some content in the cell cell.setcellvalue ("add value"); Create a new output file stream fileoutputStream fout = new FileOutputStream (outputfile);
			Save the corresponding Excel workbook Workbook.write (Fout);
			
			Fout.flush ();
			End of operation, close file Fout.close ();
		System.out.println ("Generate files .... ...");
		catch (Exception e) {SYSTEM.OUT.PRINTLN ("!!!!!!" + e.getmessage ()); }//Demonstrates how to read the data in an Excel document public static void Readsheet () {try{//Create a reference to an Excel workbook file Hssfworkbook workbook = NE
			W Hssfworkbook (New FileInputStream (outputfile));
			Create a reference to the worksheet.
			This example is quoted by name Hssfsheet sheet = workbook.getsheet ("benefit indicator");
			Getsheetat (int index) can also be referenced by index,//In an Excel document, the default index for worksheet one is 0,//The statement is: hssfsheet sheet = workbook.getsheetat (0);
			Reads upper left upper cell hssfrow row = sheet.getrow (0);
			Hssfcell cell = Row.getcell (0);
		Output cell content, Cell.getstringcellvalue () is the value of the unit to take the SYSTEM.OUT.PRINTLN ("upper left Unit is:" + cell.getstringcellvalue ());
		catch (Exception e) {SYSTEM.OUT.PRINTLN ("!!!!" + e);
 }
	}	
}


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.