Java Implementation Excel table export data

Source: Internet
Author: User

/** * Export FirstRow (Entitybean) list first row of data in the list EB, key-value pairs (not including ordinal) Example: ("name", "name") * data (entitybean[]) list * * @author ZHAOJQ */PUBL IC PR ExportData (entitybean eb) {if (EB = = null) {return new PR (0, "parameter is null", NULL);} try{//CREATE Table file String rootpath = Nameedpathutil.getdefaultsavepath (); String filepath = "/lsip/excel/"; File F = new file (RootPath + filepath)//Create folder path//If the folder does not exist create if (!f.exists () &&!f.isdirectory ()) {f.mkdirs ();} String filename = system.currenttimemillis () + ". xls"; File File = new file (f, filename), if (!file.exists ()) {file.createnewfile ();} Entitybean firstrow = Eb.getbean ("FirstRow"); entitybean[] data = Eb.getbeans ("data");//Create workbook Writableworkbook workbook = workbook.createworkbook (file);// Create a new page Writablesheet sheet = workbook.createsheet ("1th page", 0);//Label (column, row, data)//First row string[] keys = Firstrow.getbeanfieldnames ();//Gets the table header keyfor (int i = 0; i < keys.length; i++) {Label firstrow = new label (i, 0, FirstRow. GetString (Keys[i]); Sheet.addcell (firstrow);} Create a data row for (int i = 0; i < Data.length; i++) {for (int j = 0; J < Keys.length; J + +) {Label DataRow = new Label (j, 1 + I, data[i].getstring (Keys[j])); Sheet.addcel L (DataRow);}} Workbook.write (); Workbook.close (); String Contex = Lsipconfigutil.getdownloadconfig (); return new PR (1, "", String.Format ("%sleap/download/default%s%s", Contex, filepath, filename);} catch (Exception e) {global.getinstance (). LogError (e); return new PR (0, e.tostring (), null);}}

/** * Test List export */public PR exportexamqd (searchparameters spar) {Try{entitybean par = new Entitybean (); Spar.setorder ("Seatno "); Spar.setpagesize (10000); entitybean[] List = EXAMSEARCH4EB (SPAR), if (Stringutil.isnullorempty (list) return new PR (1, "Query result is empty", null);// The first row of data Entitybean Firstbean = new Entitybean () firstbean.set ("AreaName", "region"), Firstbean.set ("Examroomid", "Test Center"); Firstbean.set ("Examsubject", "Examination subject"), Firstbean.set ("Examtime", "Test Time"), Firstbean.set ("Cardno", "certificate Number"); Firstbean.set ("PersonName", "name"), Firstbean.set ("mobile", "mobile phone number"), Firstbean.set ("Examstate", "Examination registration status"); Firstbean.set ("Examscore", "score"), Firstbean.set ("Seatno", "seat number"), Firstbean.set ("Address", "correspondence"); for (int i = 0;i < list.length;i++) {Entitybean _data = list[i]; String areaname = Zonepro.getinstance (). Getareaname (_data.getstring ("Areaid")); _data.set ("AreaName", areaname); if ( _data.getstring ("examroomid") = null) {Leapcodevalue _examroomid= codetypecache.getinstance (). GetCodeValue ("LSIP_ Examroom ", _data.getstring (" Examroomid ")); if (_examroomid! = null) _data.set ("Examroomid", _examroomid.getcodevalue ());} if (_data.getstring ("examsubject") = null) {Leapcodevalue _examsubject= codetypecache.getinstance (). GetCodeValue (" Lsip_examsubject ", _data.getstring (" Examsubject ")); if (_examsubject! = null) _data.set (" Examsubject ", _ Examsubject.getcodevalue ());} if (_data.getstring ("examtype") = null) {Leapcodevalue _examtype= codetypecache.getinstance (). Getcodevalue ("LSIP_ Examtype ", _data.getstring (" Examtype ")), if (_examtype! = null) _data.set (" Examtype ", _examtype.getcodevalue ());} if (_data.getstring ("examstate") = null) {Leapcodevalue _examstate= codetypecache.getinstance (). Getcodevalue ("LSIP_ Examstate ", _data.getstring (" Examstate ")), if (_examstate! = null) _data.set (" Examstate ", _examstate.getcodevalue ());} if (_data.getstring ("examtime") = null) {_data.set ("Examtime", _data.getstring ("Examtime"). Substring (0, 16));}} Par.set ("FirstRow", Firstbean);p ar.set ("Data", list); return new Basedataservice (). ExportData (PAR);} catch (Exception e) {GlObal.getinstance (). LogError (e); return new PR (0,e.getmessage (), null);}}

  

Java Implementation Excel table export data

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.