Use poi to produce Excel

Source: Internet
Author: User

Apache poi is an open-source library of the Apache Software Foundation. Poi provides APIs for Java programs to read and write Microsoft Office files.

The project must generate an Excel file in this format.


To use poi, download the corresponding jar package to the project. Please search for it by yourself



First, abstract the form into Java Bean.

My bean is as follows:

package com.poi.entity;import java.util.ArrayList;import java.util.List;public class PlatformDetail {private List<ProductDetailTab> productDetailTabs;private Borrower borrower;private Investor investor;public PlatformDetail(){productDetailTabs=new ArrayList<ProductDetailTab>();borrower=new Borrower();investor=new Investor();}public List<ProductDetailTab> getProductDetailTabs() {return productDetailTabs;}public void setProductDetailTabs(List<ProductDetailTab> productDetailTabs) {this.productDetailTabs = productDetailTabs;}public Borrower getBorrower() {return borrower;}public void setBorrower(Borrower borrower) {this.borrower = borrower;}public Investor getInvestor() {return investor;}public void setInvestor(Investor investor) {this.investor = investor;}}
Package COM. poi. entity; import Java. util. arraylist; import Java. util. list; public class productdetailtab {list <string> heads; private double investsum; private integer investcycle; private double annualyield; private double interestsum; List <productdetailitem> datas; Public productdetailtab () {heads = new arraylist <string> (); heads. add ("Total"); heads. add ("Investment Cycle"); heads. add ("annualized rate of return (%)"); heads. add ("Investor name"); heads. add ("investment amount"); heads. add ("investor purchase Day"); heads. add ("lending date"); heads. add ("remittance date"); heads. add ("days of interest calculation"); heads. add ("Expiration interest"); heads. add ("Remarks"); datas = new arraylist <productdetailitem> ();} public list <string> getheads () {return heads ;} public void setheads (list <string> heads) {This. heads = heads;} public double getinvestsum () {return investsum;} public void setinvestsum (double investsum) {This. investsum = investsum;} public integer getinvestcycle () {return investcycle;} public void setinvestcycle (integer investcycle) {This. investcycle = investcycle;} public double getannualyield () {return annualyield;} public void setannualyield (double annualyield) {This. annualyield = annualyield;} public double getinterestsum () {return interestsum;} public void setinterestsum (double interestsum) {This. interestsum = interestsum;} public list <productdetailitem> getdatas () {return datas;} public void setdatas (list <productdetailitem> datas) {This. datas = datas ;}}

package com.poi.entity;import java.util.Date;public class ProductDetailItem {private String investorName;private Double investment;private Date buyDate;private Date loansDate;private Date repayDate;private int interestDays;private Double interest;private String remark;public String getInvestorName() {return investorName;}public void setInvestorName(String investorName) {this.investorName = investorName;}public Double getInvestment() {return investment;}public void setInvestment(Double investment) {this.investment = investment;}public Date getBuyDate() {return buyDate;}public void setBuyDate(Date buyDate) {this.buyDate = buyDate;}public Date getLoansDate() {return loansDate;}public void setLoansDate(Date loansDate) {this.loansDate = loansDate;}public Date getRepayDate() {return repayDate;}public void setRepayDate(Date repayDate) {this.repayDate = repayDate;}public int getInterestDays() {return interestDays;}public void setInterestDays(int interestDays) {this.interestDays = interestDays;}public Double getInterest() {return interest;}public void setInterest(Double interest) {this.interest = interest;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}}


The generator is as follows:

Package COM. poi. utils; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. util. list; import Org. apache. poi. hssf. usermodel. hssfcellstyle; import Org. apache. poi. hssf. usermodel. hssfdataformat; import Org. apache. poi. hssf. usermodel. hssfrow; import Org. apache. poi. hssf. usermodel. hssfsheet; import Org. apache. poi. hssf. usermodel. hssfworkbook; import Org. apache. poi. SS. usermode L. cell; import Org. apache. poi. SS. usermodel. cellstyle; import COM. poi. entity. *; public class implements {private hssfworkbook WB; private platformdetail PFD; private cellstyle aligncenter; private cellstyle alignright; private cellstyle datestyle; private cellstyle topborder; private cellstyle totaltopborder; private cellstyle money; public platformdetailxlscreator (platformdetail PFD) {This. WB = New hssfworkbook (); this. PFD = PFD; aligncenter = WB. createcellstyle (); // align aligncenter. setalignment (hssfcellstyle. align_center); // The align style alignright = WB. createcellstyle (); alignright. setalignment (hssfcellstyle. align_right); datestyle = WB. createcellstyle (); hssfdataformat format = WB. createdataformat (); datestyle. setdataformat (format. getformat ("yyyy/mm/DD"); datestyle. setalignment (hssfcellstyle. align_righ T); topborder = WB. createcellstyle (); topborder. setbordertop (hssfcellstyle. border_thick); totaltopborder = WB. createcellstyle (); totaltopborder. setbordertop (hssfcellstyle. border_thick); totaltopborder. setalignment (hssfcellstyle. align_right); totaltopborder. setdataformat (format. getformat ("#,## 0.00"); money = WB. createcellstyle (); money. setdataformat (format. getformat ("#,## 0.00"); money. setalignment (hssfcellstyle. Align_right); Init ();} public void Init () {hssfsheet sheet1 = WB. createsheet ("product details"); createsheet1 (sheet1);} public void write (string path) throws ioexception {file outputfile = new file (PATH); If (! Outputfile. exists () {outputfile. createnewfile ();} fileoutputstream OS = new fileoutputstream (outputfile); WB. write (OS); OS. close ();} // sheet1private void createsheet1 (hssfsheet sheet) {sheet. setdefacolumcolumnwidth (12); int totalrow = 0; List <productdetailtab> tabs = PFD. getproductdetailtabs (); List <string> heads = tabs. get (0 ). getheads (); // write the table header sheet. createfreezepane (0, 1); hssfrow row0 = sheet. createrow (0); For (INT I = 0; I Test class:

Package COM. poi. test; import Java. io. ioexception; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. list; import COM. poi. entity. platformdetail; import COM. poi. entity. productdetailitem; import COM. poi. entity. productdetailtab; import COM. poi. utils. platformdetailxlscreator; public class test {public static void main (string [] ARGs) throws parseexception {simpledateformat SDF = new simpledateformat ("yyyy/mm/DD "); platformdetail = new platformdetail (); List <productdetailtab> tabs = platformdetail. getproductdetailtabs (); productdetailtab tab = new productdetailtab (); tab. setinvestsum (96800.00); tab. setinvestcycle (7); tab. setannualyield (5.45); tab. setinterestsum (1012323.11); productdetailitem item = new productdetailitem (); item. setinvestorname ("Tang yijuan"); item. setinvestment (123200.00); item. setbuydate (SDF. parse ("2014/9/28"); item. setloansdate (SDF. parse ("2014/9/28"); item. setrepaydate (SDF. parse ("2014/9/28"); item. setinterestdays (7); item. setinterest (12121210.10); item. setremark ("My remarks"); tab. getdatas (). add (item); tab. getdatas (). add (item); tab. getdatas (). add (item); tab. getdatas (). add (item); tabs. add (Tab); tabs. add (Tab); platformdetailxlscreator creator = new platformdetailxlscreator (platformdetail); try {creator. write ("d :\\ platformdetail.xls");} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}



Use poi to produce Excel

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.