Java export of personal projects Excel

Source: Internet
Author: User

To combine the projects you do, here is the configuration of the export Excel that I made for the project:

Need to download the Import POI package, do it yourself Google

Take an example of exporting employee information

In the com.demo.utils under the establishment of exportemployeeexcel.java//needy students can be the employee part of the information modified to Package com.demo.utils;import  java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import  java.util.collection;import java.util.iterator;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.hssfrichtextstring;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.demo.entity.employee;public class exportemployeeexcel<t>{@SuppressWarnings ("deprecation") Public void exportexcel ( String[] headers,Collection<Employee> dataset,  Outputstream out)  {//  statement a workbook hssfworkbook&nbsP;workbook = new hssfworkbook ();//  to generate a table hssfsheet sheet =  Workbook.createsheet ();//  Set table default column width is 15 bytes sheet.setdefaultcolumnwidth ((short)  15);//  Generates a style Hssfcellstyle style = workbook.createcellstyle ();//  Set these styles Style.setfillforegroundcolor (HSSFColor.WHITE.index); Style.setfillpattern (Hssfcellstyle.solid_foreground) ; Style.setborderbottom (Hssfcellstyle.border_thin); Style.setborderleft (Hssfcellstyle.border_thin); Style.setborderright (Hssfcellstyle.border_thin); Style.setbordertop (Hssfcellstyle.border_thin); Style.setalignment (Hssfcellstyle.align_center);//  generates a font hssffont font =  Workbook.createfont (); Font.setcolor (HSSFColor.RED.index); font.setfontheightinpoints ((short)  12); Font.setboldweight (hssffont.boldweight_bold);//  apply a font to the current style Style.setfont (font);//  Build and set another style Hssfcellstyle style2 = workbook.createcellstyle (); Style2.setfillforegroundcolor ( HSSFColor.WHITE.index); STYLE2.SETFILLPAttern (Hssfcellstyle.solid_foreground); Style2.setborderbottom (Hssfcellstyle.border_thin); Style2.setBorderLeft ( Hssfcellstyle.border_thin); Style2.setborderright (Hssfcellstyle.border_thin); Style2.setbordertop ( Hssfcellstyle.border_thin); style2.setalignment (Hssfcellstyle.align_center); Style2.setverticalalignment ( Hssfcellstyle.vertical_center);//  Generate another font hssffont font2 = workbook.createfont (); Font2.setboldweight (hssffont.boldweight_normal);//  apply a font to the current style Style2.setfont (Font2);//  Generate table header row Hssfrow row = sheet.createrow (0);for  (short i = 0; i <  headers.length; i++)  {hssfcell cell = row.createcell (i); Cell.setCellStyle (style ); Hssfrichtextstring text = new hssfrichtextstring (Headers[i]); Cell.setCellValue (text);}   Traversal of collection data, resulting in data row iterator<employee> it = dataset.iterator (); Hssffont font3 = workbook.createfont (); Font3.setcolor (Hssfcolor.blue. index); List<list<string>> listrow=new arraylist<list<string>> ();while  ( It.hasnext ())  {employee employee= (Employee) it.next (); List<string> listhol=new arraylist<string> ();//output to Excel Line// It is necessary to change this part to the output of Listhol.add (Employee.getempid () + ""), Listhol.add (Employee.getempname () + ""); string sex =  "0";if  ("0". Equals (Employee.getsex ()))  {sex= "Male";} else if  ("1". Equals (Employee.getsex ()))  {sex= "female";} Listhol.add (sex+ ""); Listhol.add (Employee.getbirthday ()); Listhol.add (Employee.getempdesc () + ""); ListHol.add ( Employee.getempremark () + ""); Listhol.add (Employee.getdept (). Getdeptname () + ""); Listhol.add (employee.geteducation (). Geteduname () + ""); Listhol.add (Employee.getrolers (). Getrolername () + ""); Listrow.add (Listhol);} These are the parts int index = 1;for  (list<string> list : listrow) that need to be modified  {row  = sheet.createrow (Index);short i=0;for  (string string : list)  {//hssfcell cell0 = row.createcell (i); Hssfcell cell0=row.createcell (i); Cell0.setcellstyle (Style2); Hssfrichtextstring richstring = new hssfrichtextstring (string); Richstring.applyfont (FONT3) ; Cell0.setcellvalue (richstring); i++;} index++;} Try {workbook.write (out);}  catch  (ioexception e)  {e.printstacktrace ();}}

Here is the call to the Servlet implementation section:

Com.demo.servlet under the main implementation code if ("Exportemp". Equals (method)) {Resp.addheader ("content-disposition", "attachment; Filename=employee.xls "); List<employee> list=empservice.findallinfo ();//Full Search exportemployeeexcel<employee> ex=new Exportemployeeexcel<employee> (); String[] headers={"Employee number", "Employee Name", "Gender", "date of birth", "description", "remark", "department", "Education", "role"};try {outputstream out= Resp.getoutputstream (); Ex.exportexcel (headers,list, out); Out.close ();} catch (IOException e) {e.printstacktrace ();}}

The DAO Layer and service layer code are ignored

If you don't understand, contact me.

This article is from the "Cute Xiaopp" blog, make sure to keep this source http://smartxiaopp.blog.51cto.com/9349053/1548953

Java export of personal projects 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.