Java implementation Front-end interactive export Excel

Source: Internet
Author: User

The first step, the front end gets the parameters and accesses the background through form forms or URLs

Define params object receive key value pair parameters
var params = {
    ischecked:1,
    starttime: "2017-03-05",
    endtime: "2017-03-08", Status:1
    ,
    arbtype: 2423,
    claimsstatus:3412,
    arbdutyid:542,
    siteid:54,
    provinceid:1342
};
To stitch a Param object into a string
var Encodeparam = function (JSON) {
    var tmps = [];
    for (Var key in JSON) {
        Tmps.push (key + ' = ' + Json[key]);
    }
    Return Tmps.join (' & ');
}
Define URL

var url=basepath+ "Submenucontroller/export?" +encodeparam (query);
Determine if there is a form with ID _exportform, if not, create a hidden form, put the URL in, and then submit
var exportform = document.getElementById ("_ Exportform ");
if (!exportform) {
    exportform = document.createelement ("form");
    Exportform.setattribute (' id ', ' _exportform ');
    Exportform.setattribute ("action", URL);
    Exportform.setattribute ("Method", "post");
}
Document.body.appendChild (exportform);
Exportform.submit ();
Second step, background receive parameters, stitching Excel Title, query out list, call Excel Generation tool class

@RequestMapping (value = "Export", method = Requestmethod.post) @ResponseBody the public void Exportsitedeclarelist (
            HttpServletResponse response,submenuquery query) {string[] Headarray = {"Waybill number", "claim case", "Declaration dot name", "province", "declaring type", "Responsibility type",
    "Time of arbitration", "date of shipment", "Arbitration Date", "Single person", "time of Separation"};
    list<submenuvo> list = this.submenuFacade.findSubmenuPageQuery (query). GetRows ();
    list<object[]> contentlist = new arraylist<> (); if (List.size () >0) {for (Submenuvo entity:list) {object[] o = {Entity.getew
                    BNo (),//"Waybill number Entity.getclaimsstatus (),//Claims Entity.getdeclaresitename (),//Reporting dot Name
                    Entity.getprovinceareaname (),//Province Entity.getarbtypename (),//reporting type Entity.getarbdutyname (),//liability Type Entity.getarbregistertime (),//Quorum registration time Entity.gete
    Wbdate (),//Date Entity.getarbchecktime (),//Date of Arbitration review                Entity.getusername (),//single entity.getcreatetime ()//Split time};
        Contentlist.add (o);
    }}else{return;
    try {execlutil.exportexcel (response, Headarray, ContentList, "Submenulist.xls");
    catch (Exception e) {logger.error ("dot claim declaration export error" +e);
 }
}

Step three, Excel tool class

Package util;
Import org.apache.poi.hssf.usermodel.*;

Import Org.apache.poi.hssf.util.HSSFColor;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.List;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;



Import Javax.servlet.http.HttpServletResponse; 
   /** * Excel Export Tool class */public class Execlutil {/** * @param title * @param headers * @param DataSet * * public static InputStream Exportexcel (String title, string[] headers, list<object[]> dataset) {Inputstrea
      M is;
      Bytearrayoutputstream OS = new Bytearrayoutputstream ();
      Declares a work thin hssfworkbook workbook = new Hssfworkbook ();
      Generate a table Hssfsheet sheet = workbook.createsheet (title);
      Sets the table default column width to 15 bytes sheet.setdefaultcolumnwidth ((short) 15); Generate a style hssfcellstyle style = Workbook.cReatecellstyle ();
      Set these styles Style.setfillforegroundcolor (HSSFColor.SKY_BLUE.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);
      Generate a font Hssffont font = Workbook.createfont ();
      Font.setcolor (HSSFColor.VIOLET.index);
      Font.setfontheightinpoints ((short) 12);
      Font.setboldweight (Hssffont.boldweight_bold);
      Apply the font to the current style style.setfont (font);
      Build and set another style Hssfcellstyle Style2 = Workbook.createcellstyle ();
      Style2.setfillforegroundcolor (HSSFColor.LIGHT_YELLOW.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 the font to the current style Style2.setfont (Font2);
      Declares a paint top manager hssfpatriarch patriarch = Sheet.createdrawingpatriarch (); Define the size and location of the annotation, as detailed in the document hssfcomment comment = patriarch.createcomment (new hssfclientanchor (0, 0, 0, 0, short
      ) 4, 2, (short) 6, 5); Set the comment content comment.setstring (new hssfrichtextstring () to add a comment in poi.
      ")); Set note Author, when the mouse is moved to a cell, it can be in the status bar

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.