Java (POI) export Excel data

Source: Internet
Author: User
Tags set time

Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.net.URLEncoder;

Import Javax.servlet.http.HttpServletResponse;

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.CellRangeAddress;
Import Org.apache.poi.hssf.util.HSSFColor;

@SuppressWarnings ({"Deprecation"})
public class Test46 {

public void Excel (HttpServletRequest request, httpservletresponse response) throws Exception {String sheetname = "Vehicle List"    ;    String titlename = "Application Data tables for vehicles";    String title2name = "self-examination Date:";    String fileName = "Application form for vehicle"; String Beizhuname = "Remark: 1. Test. \r\n2. Testing. \r\n3. Testing.    ";//** set forced line break \ r \ n" * * int columnnumber = 3;    Int[] ColumnWidth = {10, 20, 30}; String[][] DataList = {{"001", "2015-01-01", "IT"}, {"002", "2015-01-02", "Marketing department"}, {"003", "2015-01-03", "    Test "}};        String[] ColumnName = {"Waybill", "Application Time", "Application Department"};        The first step is to create a webbook that corresponds to an Excel file Hssfworkbook wb = new Hssfworkbook ();        The second step, add a sheet in WebBook, corresponding to the Excel file sheet hssfsheet sheet = wb.createsheet (sheetname); Sheet.setdefaultcolumnwidth (15);             Uniformly set column width for (int i = 0; i < ColumnNumber; i++) {for (int j = 0; J <= I; j + +) {if (i = = j) {Sheet.setcolumnwidth (I, columnwidth[j] * 256);        Set the width of each column individually}}}//Create line No. 0 that is the title Hssfrow Row1 = Sheet.createrow ((int) 0); Row1.setheightinpoints (50);//height of the device title//third step Create a header cell style style2 and font style headerFont1 Hssfcellstyle style2 =        Wb.createcellstyle ();        Style2.setalignment (Hssfcellstyle.align_center);        Style2.setverticalalignment (Hssfcellstyle.vertical_center);        Style2.setfillforegroundcolor (HSSFColor.LIGHT_TURQUOISE.index);        Style2.setfillpattern (Hssfcellstyle.solid_foreground); Hssffont headerFont1 = (hssffont) wb.createfont (); Create font style headerfont1.setboldweight (hssffont.boldweight_bold); Font Bold Headerfont1.setfontname ("bold"); Set Font type headerfont1.setfontheightinpoints ((short) 15); Set the font size Style2.setfont (HEADERFONT1); Set the font style for heading styles Hssfcell cell1 = Row1.createcell (0);//Create Title first column sheet.addmergedregion (new cellrangeaddress (0, 0, 0, columnNumber-1)); Merge No. 0 to 17th column CELl1.setcellvalue (TitleName); Set the value title Cell1.setcellstyle (style2);        Set heading style//creation time Hssfrow Row2 = Sheet.createrow ((int) 1);        Row2.setheightinpoints (21);//Set time height//Create Time cell style and table header font style Hssfcellstyle Style2 = Wb.createcellstyle (); Style2.setalignment (hssfcellstyle.align_right);//Create a right-formatted horizontal direction style2.setverticalalignment (hssfcellstyle.ver Tical_center); Vertical direction Hssffont HeaderFont2 = (hssffont) wb.createfont (); Create font style headerfont2.setfontname ("italic"); Set Font type headerfont2.setfontheightinpoints ((short) 12); Set the font size Style2.setfont (HEADERFONT2);         Set the font style for the heading style Hssfcell cell2 = Row2.createcell (0);//Set the value in the first column//merge column header when using POI in the EXECL report design, you encounter a cell merge problem, Use an important function: cellrangeaddress (int, int, int, int)//parameter: Starting line number, terminating line number, starting column number, terminating column number sheet.addmergedregion (new Cell        Rangeaddress (1, 1, 0, columnNumber-1));        SimpleDateFormat DF2 = new SimpleDateFormat ("yyyy year mm DD day"); String date2 = Df2.format (New Date ()); Cell2.setcellvalue (Title2name + date2); Set the value title Cell2.setcellstyle (style2);        Set heading style//create notes Hssfrow row3 = Sheet.createrow ((int) 12); Row3.setheightinpoints (71);//height of the device title//third step Create a header cell style style2 and font style headerFont1 Hssfcellstyle style3 = Wb.crea        Tecellstyle (); Style3.setwraptext (TRUE);//Set Line wrap style3.setalignment (hssfcellstyle.align_left);//Horizontal left Style3.setverticala Lignment (Hssfcellstyle.vertical_center); Vertical direction Hssffont HeaderFont3 = (hssffont) wb.createfont (); Create font style headerfont3.setfontname ("imitation"); Set Font type headerfont3.setfontheightinpoints ((short) 11); Set the font size Style3.setfont (HEADERFONT3);  Set the font style for heading styles Hssfcell cell3 = Row3.createcell (0);//Create Title first column sheet.addmergedregion (New Cellrangeaddress (12, 0, columnNumber-1)); Merge column header cell3.setcellvalue (beizhuname); Set the value title Cell3.setcellstyle (STYLE3); Set heading style//Creating line 1th is the table header hssfrow row = Sheet.createrow ((int) 2);        Row.setheightinpoints (25);//Set header height//create line 1th that is the table header hssfrow row = Sheet.createrow ((int) 1);        Row.setheightinpoints (37);//Set Header height//Fourth step, CREATE table header cell style and header font style Hssfcellstyle style = Wb.createcellstyle ();        Style.setwraptext (TRUE);//Set auto-wrap style.setalignment (hssfcellstyle.align_center); Style.setverticalalignment (Hssfcellstyle.vertical_center);        Create a center format style.setbottombordercolor (HSSFCOLOR.BLACK.INDEX);        Style.setborderbottom (Hssfcellstyle.border_thin);        Style.setborderleft (Hssfcellstyle.border_thin);        Style.setborderright (Hssfcellstyle.border_thin);        Style.setbordertop (Hssfcellstyle.border_thin); Hssffont HeaderFont = (hssffont) wb.createfont (); Create font style headerfont.setboldweight (hssffont.boldweight_bold); Font Bold Headerfont.setfontname ("bold"); Set Font type headerfont.setfontheightinpoints ((short) 10);    Set Font size    Style.setfont (HeaderFont); Set the font style for the heading style//fourth. Step, create a column for the table header for (int i = 0; i < ColumnNumber; i++) {Hssfcell cell            = Row.createcell (i);            Cell.setcellvalue (Columnname[i]);        Cell.setcellstyle (style); }//Fifth step, create the cell and set the value for (int i = 0; i < datalist.length; i++) {row = Sheet.createrow (            (int) i + 2);            Set features for data content new cell style 1 wrap line up and down hssfcellstyle Zidonghuanhang = Wb.createcellstyle (); Zidonghuanhang.setwraptext (TRUE);//Set Wrap Zidonghuanhang. Setverticalalignment (hssfcellstyl E.vertical_center);            Create a center format//Set Border Zidonghuanhang.setbottombordercolor (HSSFColor.BLACK.index);            Zidonghuanhang.setborderbottom (Hssfcellstyle.border_thin);            Zidonghuanhang.setborderleft (Hssfcellstyle.border_thin);            Zidonghuanhang.setborderright (Hssfcellstyle.border_thin); Zidonghuanhang.setbordeRtop (Hssfcellstyle.border_thin);            Set features for data content new cell style 2 wrap top and bottom center around also center Hssfcellstyle zidonghuanhang2 = Wb.createcellstyle (); Zidonghuanhang2.setwraptext (TRUE);//Set Wrap zidonghuanhang2. Setverticalalignment (hssfcellst Yle. Vertical_center); Create a top-down center format zidonghuanhang2.setalignment (hssfcellstyle.align_center);//center//Set Border Zi            Donghuanhang2.setbottombordercolor (HSSFColor.BLACK.index);            Zidonghuanhang2.setborderbottom (Hssfcellstyle.border_thin);            Zidonghuanhang2.setborderleft (Hssfcellstyle.border_thin);            Zidonghuanhang2.setborderright (Hssfcellstyle.border_thin);            Zidonghuanhang2.setbordertop (Hssfcellstyle.border_thin); Hssffont Neirongfont = (hssffont) wb.createfont (); Create content font style neirongfont.setfontname ("Arial"); Sets the content font type neirongfont.setfontheightinpoints ((short) 11); Set the content font size Zidonghuanhang2.setfont (neirongfoNT);            Set the font style for the content style Hssfcell Datacell = null;                for (int j = 0; J < ColumnNumber; J + +) {Datacell = Row.createcell (j);                Datacell.setcellvalue (Datalist[i][j]);            Datacell.setcellstyle (ZIDONGHUANHANG2); }///output Excel file, directly find the browser settings, download complete try {simpledateformat SDF = new SimpleDateFormat ("YyyyMMdd            Hhmmss ");            String date = Sdf.format (new date ());            OutputStream output = Response.getoutputstream ();            String filename = filename + date + ". xls";            Response.getoutputstream (). write (1);            Response.reset (); Response.setheader ("Content-disposition", "attachment;            Filename= ". Concat (string.valueof (urlencoder.encode (filename," UTF-8 ")));            Response.setcontenttype ("Application/msexcel;charset=utf-8");//Set the exported file name is UTF-8 format, support Chinese character name + time Wb.write (output);            Wb.close (); Output.flush ();        Output.close ();        } catch (Exception e) {e.printstacktrace (); }}

}
Recommend a POI knowledge point very full blog address: https://www.cnblogs.com/azhqiang/p/4362090.html

Java (POI) export Excel data

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.