Use Apache POI to enable the export of Excel functionality

Source: Internet
Author: User
The Apache poi is a free, Open-source Java API Written in Java that provides the ability to read and write to Microsoft Office format files.
The official download address for the Apache POI Source and library (jar package) is: http://poi.apache.org/download.html
I also provided a download address: http://download.csdn.net/detail/gaojinshan/7487253
It is the latest version (POI-3.10-FINAL-20140208), containing the source code and library (tar.gz format).

The reference code is as follows:

Package test;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import java.util.List;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi.hssf.usermodel.HSSFFont;
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.hssf.util.HSSFColor;

Import Com.alibaba.fastjson.JSON; Class Datainfo {private string countdate;//statistic date private string channelid;//channel number public string Getcountdate () {R
	Eturn countdate;
	} public void Setcountdate (String countdate) {this.countdate = countdate;
	Public String Getchannelid () {return channelid;
	} public void Setchannelid (String channelid) {this.channelid = Channelid;

}
}; public class Exportexceltest {public static void main (string[] args) throws IOException {//create Hssfworkbook WB = NE
		W Hssfworkbook (); Hssfsheet sheet = wb.creatEsheet ();
		Create a cell style hssfcellstyle Titlecellstyle = Wb.createcellstyle ();
		Specifies that the cell is centered and the border is fine titlecellstyle.setalignment (hssfcellstyle.align_center);
		Titlecellstyle.setverticalalignment (Hssfcellstyle.vertical_center);
		Titlecellstyle.setbordertop (Hssfcellstyle.border_thin);
		Titlecellstyle.setborderbottom (Hssfcellstyle.border_thin);
		Titlecellstyle.setborderright (Hssfcellstyle.border_thin);
		Titlecellstyle.setborderleft (Hssfcellstyle.border_thin);
		Set the fill color Titlecellstyle.setfillforegroundcolor (HSSFColor.SKY_BLUE.index);
		Titlecellstyle.setfillpattern (Hssfcellstyle.solid_foreground);
		Specifies that line Titlecellstyle.setwraptext (true) is automatically wrapped when the cell contents are not displayed;
		Set cell font Hssffont Titlefont = Wb.createfont ();
		Titlefont.setfontheightinpoints ((short) 12);
		Titlefont.setboldweight (Hssffont.boldweight_bold);
		Titlecellstyle.setfont (Titlefont);
		Hssfrow headerrow = sheet.createrow (0);
		Hssfcell Headercell = null;
		String[] titles = {"Statistical date", "channel number"}; for (int c = 0; c < titles.Length
			C + +) {Headercell = Headerrow.createcell (c);
			Headercell.setcellstyle (Titlecellstyle);
			Headercell.setcellvalue (Titles[c]);
		Sheet.setcolumnwidth (c, (30 * 160)); //------------------------------------------------------------------//Create cell styles Hssfcellstyle CellStyle = Wb.crea
		Tecellstyle ();
		Specifies that the cell is centered and the border is fine cellstyle.setalignment (hssfcellstyle.align_center);
		Cellstyle.setverticalalignment (Hssfcellstyle.vertical_center);
		Cellstyle.setbordertop (Hssfcellstyle.border_thin);
		Cellstyle.setborderbottom (Hssfcellstyle.border_thin);
		Cellstyle.setborderright (Hssfcellstyle.border_thin);
		Cellstyle.setborderleft (Hssfcellstyle.border_thin);
		Set cell font Hssffont font = Wb.createfont ();
		Titlefont.setfontheightinpoints ((short) 11);
		Font.setboldweight (Hssffont.boldweight_normal);
		Cellstyle.setfont (font); String infostr = "[{\ channelid\": \ "bodao\", \ "countdate\": \ "2014-06-11\"}, "+" {\ "channelid\": \ "dingzhi\", \ " Countdate\ ": \" 2014-06-12\ "}," + "{\"channelid\": \ "ruiwei\", \ "countdate\": \ "2014-06-13\"}] ";
		list<datainfo> list = Json.parsearray (Infostr, Datainfo.class);
			for (int r = 0; r < list.size (); r++) {Datainfo item = List.get (r);
			Hssfrow row = Sheet.createrow (r + 1);
			Hssfcell cell = null;
			int c = 0;
			Cell = Row.createcell (c + +);
			Cell.setcellstyle (CellStyle);
			Cell.setcellvalue (Item.getcountdate ());
			Cell = Row.createcell (c + +);
			Cell.setcellstyle (CellStyle);
		Cell.setcellvalue (Item.getchannelid ());
		} fileoutputstream fileout = new FileOutputStream ("Test.xls");
		Wb.write (fileout);
		Fileout.close ();
	System.out.println ("Done");
 }
}

The effect chart is as follows:

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.