JAVA POI Application Series (1)--Generate Excel

Source: Internet
Author: User

POI Profile (official website: http://poi.apache.org/)


Apache POI is an open source library of the Apache Software Foundation, where POI provides the ability for Java programs to read and write to Microsoft Office format files. Since 2009-09-28, the release of the 3.5 version, providing support for Office2007;

Some important places about Apache POI :


1) Apache POI contains HSSF implementations for excel97-2007 (. xls files).
2) Apache POI XSSF Implementation is used to process the Excel2007 file (. xlsx).
3) Apache POI HSSF and XSSF provide a mechanism for reading/writing/modifying Excel tables.
4) Apache POI provides an extended SXSSF for XSSF to handle very large Excel work cells. The SXSSF API requires less memory, so it is appropriate to work with a very large spreadsheet while the heap memory is limited.
5) There are two modes to choose from-event mode and user mode. The event pattern requires less memory because tokens is used to read and process Excel. User mode is more object-oriented and easy to use, so use user mode in our example.
6) Apache POI provides powerful support for additional Excel features, such as handling formulas, creating cell styles-colors, borders, fonts, headers, feet, data validation, images, hyperlinks, and more.

Add Maven dependencies:

1  <Dependency>2         <groupId>Org.apache.poi</groupId>3         <Artifactid>Poi</Artifactid>4         <version>3.9</version>5  </Dependency>

The code is as follows:

1  PackageCom.yanwu.www.spring_hibernate.util;2 3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 6 ImportOrg.apache.poi.hssf.usermodel.HSSFCell;7 ImportOrg.apache.poi.hssf.usermodel.HSSFCellStyle;8 ImportOrg.apache.poi.hssf.usermodel.HSSFRow;9 ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;Ten ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; One  A  Public classsimpleexcelwrite{ -      -      Public Static voidMain (string[] args) { the          -         //The first step is to create workbook -Hssfworkbook WB =NewHssfworkbook ();  -            +         //Step two Create sheet -Hssfsheet sheet = wb.createsheet ("Test");  +            A         //The third step creates a row row: Add Table header 0 rows atHssfrow row = Sheet.createrow (0);  -Hssfcellstyle style =Wb.createcellstyle ();  -         //style.setalignment (Hssfcellstyle.align_center); //Center -            -            -         //Fourth Step Create cell inHssfcell cell = Row.createcell (0);//first Cell -Cell.setcellvalue ("name");  to Cell.setcellstyle (style);  +          -          the          *Cell = Row.createcell (1);//Second Cell $Cell.setcellvalue ("Age"); Panax Notoginseng Cell.setcellstyle (style);  -            the         +         //fifth step inserting data A         the          for(inti = 0; I < 5; i++) {   +             //Create Rows -row = Sheet.createrow (i+1);  $             //Create cells and add data $Row.createcell (0). Setcellvalue ("AA" +i);  -Row.createcell (1). Setcellvalue (i);  -              the         }   -           Wuyi         //The sixth step is to save the generated Excel file to the specified path the         Try {   -FileOutputStream Fout =NewFileOutputStream ("C:\\users\\ibm_admin\\desktop\\promotion\\a.xls");  Wu Wb.write (Fout);  - Fout.close ();  About}Catch(IOException e) { $ E.printstacktrace ();  -         }   -            -SYSTEM.OUT.PRINTLN ("Excel file Generation succeeded ...");  A     }   +        the  -          $     } the      the      the      the     

Effect:

JAVA POI Application Series (1)--Generate Excel

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.