Java Implementation POI Export Excel

Source: Internet
Author: User
Tags set background

The web framework is Struts2 with the following jar packages:

Exportexcelaction.java
ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFCellStyle;ImportOrg.apache.poi.hssf.usermodel.HSSFFont;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.hssf.util.HSSFColor;ImportOrg.apache.poi.ss.util.Region; Public  class exportexcelaction {    PrivateInputStream InputStream; PublicStringExport() {Try{Hssfworkbook WB =NewHssfworkbook (); Hssfsheet sheet = Wb.createsheet ("New Sheet"); Sheet.setdefaultcolumnwidth ( -);//Default column widthHssffont font = Wb.createfont (); Font.setfontname ("Blackbody"); Font.setfontheightinpoints (( Short) -);//Set Font sizeFont.setcolor (HSSFColor.WHITE.index);//Font ColorHssfcellstyle Headstyle = Wb.createcellstyle ();//Head styleHeadstyle.setalignment (Hssfcellstyle.align_center);//Horizontal CenterHeadstyle.setborderbottom (Hssfcellstyle.border_thin);//Bottom BorderHeadstyle.setborderleft (Hssfcellstyle.border_thin);//Left BorderHeadstyle.setbordertop (Hssfcellstyle.border_thin);//Top BorderHeadstyle.setborderright (Hssfcellstyle.border_thin);//Right borderHeadstyle.setfillforegroundcolor (HSSFColor.TEAL.index);//Set background colorHeadstyle.setfillpattern (Hssfcellstyle.solid_foreground); Headstyle.setfont (font);//Select the font format you want to useHssfcellstyle Contentstyle = Wb.createcellstyle ();//Content styleContentstyle.setborderbottom (Hssfcellstyle.border_thin);//Bottom BorderContentstyle.setborderleft (Hssfcellstyle.border_thin);//Left BorderContentstyle.setbordertop (Hssfcellstyle.border_thin);//Top BorderContentstyle.setborderright (Hssfcellstyle.border_thin);//Right borderHssfrow Row_h = Sheet.createrow (( Short)0); Hssfcell ch = Row_h.createcell (0); Ch.setcellvalue ("Test export Excel");            Ch.setcellstyle (Headstyle); Sheet.addmergedregion (NewRegion (0, ( Short)0,0, ( Short)2));//Specify merge area            //table headerHssfrow Row1 = Sheet.createrow (( Short)1); Hssfcell cell_1_0 = Row1.createcell (0); Cell_1_0.setcellvalue ("Name");            Cell_1_0.setcellstyle (Headstyle); Hssfcell cell_1_1 = Row1.createcell (1); Cell_1_1.setcellvalue ("Date of birth");            Cell_1_1.setcellstyle (Headstyle); Hssfcell cell_1_2 = Row1.createcell (2); Cell_1_2.setcellvalue ("Address"); Cell_1_2.setcellstyle (Headstyle);//ContentHssfrow row2 = Sheet.createrow (2); Hssfcell cell_2_0 = Row2.createcell (0); Cell_2_0.setcellvalue ("Itmyhome");            Cell_2_0.setcellstyle (Contentstyle); Hssfcell cell_2_1 = Row2.createcell (1); Cell_2_1.setcellvalue ("1990-05-01");            Cell_2_1.setcellstyle (Contentstyle); Hssfcell cell_2_2 = Row2.createcell (2); Cell_2_2.setcellvalue ("Beijing Changping District");            Cell_2_2.setcellstyle (Contentstyle); FileOutputStream fileout =NewFileOutputStream ("D:/mysheet.xls");//export pathWb.write (fileout);            Fileout.close (); InputStream =NewFileInputStream ("D:/mysheet.xls");//Download}Catch(Exception e)        {E.printstacktrace (); }return "Success"; } PublicInputStreamgetInputStream() {returnInputStream; } Public void Setinputstream(InputStream InputStream) { This. InputStream = InputStream; }}
Struts.xml
<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/d Tds/struts-2.0.dtd "><struts>    < package name="Json_code" extends= "json-default">        < Export a report!--        <action name="Exportexcelaction" method="Export"class ="Com.itmyhome.ExportExcelAction">                        <result type="Stream">                <param name="ContentType">Application/octet-stream</param>                <param name="InputName">InputStream</param>                <param name="Contentdisposition">Attachment;filename= "Export.xls"</param>                <param name="buffersize">4096</param>            </result>        </Action>    </Package >    < package name="Build" extends="Struts-default">        <action name="Login " method="Login" class ="Com.itmyhome.Login">            <result name="Success">index.jsp</result>        </Action>    </Package ></struts>

Export Excel

Itmyhome
Source: Download

Java Implementation POI Export 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.