Java implementation Export Excel features __java

Source: Internet
Author: User
Tags add time

Seeing is believing, directly on the effect chart:



First of all, I use this in the project:

The first step is to create a webbook that corresponds to an Excel file
In the second step, add a sheet to the WebBook, corresponding to the sheet in the Excel file
Step three, add the No. 0 row of the table header to the sheet, and note that the old version poi has a limited number of rows in Excel
Step fourth, set the style
Step fifth, set the export data
Sixth step, pop-up download box

I took every step of the way out, in order to modularize, to achieve the solution of the program, easy reuse, the following I posted the code:

The first step is to create a webbook, corresponding to an Excel file  
hssfworkbook wb = new Hssfworkbook ();  
The second step is to add a sheet to the WebBook, corresponding to the sheet  
 hssfsheet sheet = wb.createsheet ("Channel signed Interface") in the Excel file;  
In the third step, add the No. 0 row of the header in the sheet, noting that the old version poi has a limit on the number of rows in Excel  
hssfrow row = Sheet.createrow ((int) 0);
 The fourth step, set the styles
hssfcellstyle style = Setheadstyle (row, WB, N, hssffont.boldweight_bold);
The fifth step is to set the export data
map<string, object> datamap = new hashmap<string, object> ();
Datamap.put ("Interfacelist", signinterfacelist);
Setexportdate (Datamap, sheet, row, style, 5);
The sixth step, pop-up download box
popdownload (response, WB, FilePath);

    /** * * @Description set style * @author <p style= "color: #8e8e8e; font-family: Microsoft James Black; font-size=16px;f Ont-weight:bold; " >Cloud</p> * @date <p style= "color: #000; font-family: Microsoft Ya-hei; font-size=16px;" >2016-11-25 Afternoon 3:06:34</p> * @param row row * @param WB table * @param height height * @param f ontsize Font size * @param fontweight Font weight * @return/private Hssfcellstyle Setheadstyle (Hssfrow row
        , Hssfworkbook wb, int height, int fontsize, short fontweight) {//Set height Row.setheight ((short) heights);  
        Create a cell and set the value header to set the header center Hssfcellstyle style = Wb.createcellstyle (); Style.setverticalalignment (Hssfcellstyle.vertical_center)//Vertical Center style.setalignment (hssfcellstyle.align_center)
        ;//Horizontal Center//Set Font Hssffont fonts = Wb.createfont (); Font.setfontheightinpoints ((short) fontsize);//Set Font size font.setboldweight (fontweight); bold weight Style.setFont://Select the format you want to use return style; /** * * @Description Set Export data * @author <p style= "color: #8e8e8e; font-family: Microsoft Ya-hei; font-size= 16px;font-weight:bold; " >Cloud</p> * @date <p style= "color: #000; font-family: Microsoft Ya-hei; font-size=16px;"        >2016-11-25 pm 3:44:41</p> * @param datamap Export Data Map * @param sheet Export table * @param row Line * @param style style * @param exporttype export Type * * @SuppressWarnings ("unchecked") private void Setexportdate (map<string, object> datamap, Hssfsheet sheet, Hssfrow row, Hssfcellstyle style, Integer Exportty

        PE) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");  if (Exporttype = = 5) {list<interfaceconcludesign> signinterfacelist = (list<interfaceconcludesign>)
            Datamap.get ("Interfacelist"); Set table title string[] title = new string[]{"Interface name", "Interface Class", "Sub-interface", "Parent Interface", "interface standbyNote "," enter "," Output ", purchase price", "package", "Business Name", "Enterprise to pick Up", "Channel personnel", "product model", "Scope", "Update Frequency", "type", "Small Classification", "Company Code",
            "Change Information", "Test Address", "Call Address", "Test Scope", "Contract Start Time", "Contract End Time", "Add Time", "remarks"};
            Sethead (row, style, title);
            int c = 0;
                for (Interfaceconcludesign i:signinterfacelist) {C + +;
                row = Sheet.createrow ((int) c);
                Date pactsttime = I.getpact_sttime ();
                Date pactentime = I.getpact_entime ();
                Date addtime = I.getadd_time (); Time processing String packstimestr = Pactsttime!= null?
                Common.fromdatey (pactsttime): ""; String packetimestr = Pactentime!= null?
                Common.fromdatey (pactentime): ""; String addtimestr = Addtime!= null?
                Common.fromdatey (addtime): ""; Step fourth, create the cell, and set the value string[] DataArray = new String[]{i.getinterface_name (), I.getcatalog_name (), I.getson _interfAce_name (), I.getparent_interface_name (), I.getinterface_remark (), I.getout_parameter (), I.getput_ Parameter (), I.getbuy_price (), I.getpack_name (), I.getcompany_name (), I.getcompany_abutment (), I.G
                        Etchannel_user (), I.getproduct_model (), I.getscope (), I.getupd_rate (), I.gettype (), I.gettiny_catalog (), I.getcompany_code (), I.getupd_info (), I.gettest_url (), I.getcell_url (), I.gettest_scope (), Packstimestr, PackETimeS
                TR, ADDTIMESTR, I.getremark ()};
                    for (int j = 0; J < Dataarray.length J + +) {Row.createcell (j). Setcellvalue (Dataarray[j]); if (Dataarray[j]!= null && dataarray[j].length () > 0) {Sheet.setcolumnwidt
                    H (J, Dataarray[j].tostring (). Length () * 712); }}/** * * * @Description pop-up download prompt box * @author <p style= "COLOR: #8 E8e8e;fonT-family: Microsoft James Black; font-size=16px;font-weight:bold; >Cloud</p> * @date <p style= "color: #000; font-family: Microsoft Ya-hei; font-size=16px;" >2016-11-25 pm 1:25:51</p> * @param response Request header information * @param WB form * @param filePath file path Path * @throws IOException/private void Popdownload (httpservletresponse response, Hssfworkbook WB, String fi
        Lepath) throws ioexception{//initialization stream Bytearrayoutputstream OS = new Bytearrayoutputstream ();
        Bufferedinputstream bis = null;
        Bufferedoutputstream BOS = NULL;
            try {//table finalization data wb.write (OS);
            byte[] content = Os.tobytearray ();
            InputStream is = new Bytearrayinputstream (content);
            Set response parameters, you can open the download page response.reset ();
            Response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8"); Response.setheader ("Content-disposition", "attachment;filename=" + new String (FilePath). GetbytES (), "iso-8859-1"));
            Servletoutputstream out = Response.getoutputstream ();
            bis = new Bufferedinputstream (IS);
            BOS = new Bufferedoutputstream (out);
            byte[] buff = new byte[2048];
            int bytesread;
            while ( -1!= (bytesread = bis.read (buff, 0, buff.length))) {bos.write (buff, 0, bytesread);
            The catch (Exception e) {logger.info ("File download Failed");
        E.printstacktrace ();
            Finally {//close stream if (bis!= null) bis.close ();
            if (BOS!= null) bos.close ();
        if (OS!= null) os.close (); }
    }



The above program is used in my project, directly copied out may not be used, the above is just to achieve the way, welcome to Exchange: Q 294706865;

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.