Import and export features

Source: Internet
Author: User
Tags getmessage

One, step

1. Import:

I. FTL<form id= "form" action= "enctype=" Multipart/form-data "method=" POST "> <input type=" file "id=" FileID "Name=" FileName "class= "Btn dropdown-toggle btn-primary" style= "width:220px; Float:left, "/> <input type=" hidden "name=" ProjectID "id=" ProjectID "value=" "> <inp UT type= "button" value= "Import source Sound" onclick= "importlist ();"class= "Btn dropdown-toggle btn-success" style= "margin-left:20px;" /></form>Note: The background gets the value of the property, you must specify the name= ""two. Jsfunction importlist () {var filePath= $ ("#fileID"). Val (); if(FilePath = = "") {alert (' Please select File! '); return; }         if(Pnationid = = '-1 ' | | nationid = = '-1 ') {alert (' Please select a country! '); return; } var form= $ ("#form"); var ProjectID= GetQueryString ("ProjectID"); $(' #projectId '). Val (ProjectID); Form.attr ("Action", "/nps/analysis/import"); Form.attr ("Method", "POST"); Form.submit ();}//Get ProjectIDfunction getquerystring (name) {var reg=NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)"); var r= WINDOW.LOCATION.SEARCH.SUBSTR (1). Match (REG); if(r! =NULL)        returnUnescape (r[2]); return NULL;} Three. Controller@requestmapping (Value= "/analysis/import", method =requestmethod.post) PublicString Importsource (HttpServletRequest request, @RequestParam ("FileName") commonsmultipartfile file, Integer ProjectID) {InputStream in=NULL; String msg= ""; Try{ in=File.getinputstream (); Msg=npssourceanalysisservice.importlist (in, File.getoriginalfilename (), Pnationid,nationid);    In.close (); } Catch(IOException e) {e.printstacktrace ();        Logger.info (E.getmessage ()); Msg= "Import failed!" "; } finally {              if(In! =NULL) {                  Try{in.close ();//Close the stream}Catch(IOException e) {logger.debug ("In close IOException:" +e.getmessage ()); }              }    }    return"Redirect:/nps/analysis/list?projectid=" +projectid+ "&msg=" +msg;} Four. Service PublicString importlist (InputStream in,string fileName); @Transactional Publicstring Importlist (InputStream in, string Filename,integer Pnationid,integer Nationid) {string [] Table=NULL; String msg=NULL; Try{Table= Excelutils.readexcel (in,filename,0); } Catch(Exception e) {e.printstacktrace (); return"File import failed, please check the file format!" "; } Map<String,Integer> Tablecolummap =NewHashmap<string, integer>(); if(Checkutils.isnullorblank (table)|| Table.length = = 0) {            return"There isn't any data in the table!" "; }        if(Table.length = = 1) {            return"Only table header data exists in the table!" "; }        //Verifying the contents of an import file         for(inti = 0; i < table[0].length; i++) {String cell= Table[0][i].trim ();        Tablecolummap.put (cell, I); } List<String> errormsgs =NewArraylist<string>(); String[] Titles={"ID", "description", "Product name", "Model", "version", "Time", "number of times", "score (question 1)", "score (question 2)"};  for(inti = 0; i < titles.length; i++) {String title=Titles[i]; if(!Tablecolummap.containskey (title)) {Errormsgs.add ("Excel table header column is incorrect, there is no" +title+ "column, please modify Excel after importing"); if(Errormsgs.size () >= 10)return"The import file header is incorrect, please check and re-import!" "; }        }                                if(errormsgs.size () = = 0) {npssourceanalysis npssourceanalysis=Newnpssourceanalysis ();  for(inti = 1; i < table.length; i++) {String SourceID= Table[i][tablecolummap.get ("ID")].trim (); String descript= Table[i][tablecolummap.get ("description")].trim (); String Product= Table[i][tablecolummap.get ("name")].trim (); String Inner= Table[i][tablecolummap.get ("model"))].trim (); String ROM= Table[i][tablecolummap.get ("Version")].trim (); String Createtime= Table[i][tablecolummap.get ("Time")].trim (); String SN= Table[i][tablecolummap.get ("SN")].trim (); String CSR= Table[i][tablecolummap.get ("score (question 1)"))].trim (); String recommend= Table[i][tablecolummap.get ("score (question 2)"))].trim ();                    Npssourceanalysis.setsourceid (Integer.parseint (SourceID));                    Npssourceanalysis.setdescript (descript);                    Npssourceanalysis.setproduct (product);                    Npssourceanalysis.setinnerversion (inner);                    Npssourceanalysis.setouterversion (outer);                    Npssourceanalysis.setrom (ROM);                    Npssourceanalysis.setcreatetime (Createtime);                                                    NPSSOURCEANALYSIS.SETSN (SN);                    NPSSOURCEANALYSIS.SETCSR (Integer.parseint (CSR));                    Npssourceanalysis.setrecommend (Integer.parseint (recommend)); Npssourceanalysis.sethandled (0); Npssourceanalysis.setabroad (0);                    Npssourceanalysis.setpnationid (Pnationid);                                        Npssourceanalysis.setnationid (Nationid);                                                            Npssourceanalysismapper.insert (npssourceanalysis); }                    } Else{             for(String errormsg:errormsgs) {msg= msg + errormsg + ";"; }        }                returnmsg; } Five, Required tool class Excelutils.readexcel () six. Dependency<dependency> <groupId>org.apache.poi</groupId> <artifactid>poi-ooxml</artifactid&gt       ; <version>3.16-beta1</version></dependency> <dependency> <groupid>org.apache.poi&lt ;/groupid> <artifactId>poi</artifactId> <version>3.16-beta1</version></dependency ><dependency> <groupId>org.apache.commons</groupId> <artifactid>commons-lang3</ Artifactid> <version>3.1</version></Dependency<!--https://mvnrepository.com/artifact/org.samba.jcifs/jcifs --<dependency> <groupId>org.samba.jcifs</groupId> <artifactId>jcifs</artifactId> < Version>1.2.19</version></dependency>

2. Export:

A. Controller/*** Source Acoustic analysis Results export*/@RequestMapping (Value= "/devide/explorexcel", method ={requestmethod.post}) Public voidExplorexcel (httpservletrequest request,httpservletresponse response)throwscsexception {Integer ProjectID= Integer.valueof (Request.getparameter ("ProjectID")); String StartTime= Request.getparameter ("StartTime"); String Endtime= Request.getparameter ("Endtime"); List<Version> versions=Versionservice.getversionlistbyprojectid (ProjectID); String Product=versions.get (0). GetProduct (); List<NPSAnalysisDevide> list =Npsanalysisdevideservice.selectallbad (product,starttime,endtime);                                        Npsanalysisdevideservice.exportlist (response,list,product); } two. Service Public voidExportlist (httpservletresponse response, list<npsanalysisdevide>list,string projectName); @Override Public voidExportlist (HttpServletResponse response,list<npsanalysisdevide>items,string ProjectName) {buildexcelutils Beutils=Newbuildexcelutils (items,projectname) {@Override Public voidBuilddatato2007excel (ExcelWrite2007 excel,xssfsheet sheet, Xssfcellstyle contentformat,list<?>items,string ProjectName) {@SuppressWarnings ("Unchecked") List<NPSAnalysisDevide> list = (list<npsanalysisdevide>) Items; intROWIDX = 0;  for(Npsanalysisdevide item:list) {rowidx++; Excel.setval (sheet, ROWIDX,0, Item.getsourceid (), Contentformat); Excel.setval (sheet, ROWIDX,1, Item.getdescript (), Contentformat); Excel.setval (sheet, ROWIDX,2, Item.getproduct (), Contentformat); Excel.setval (sheet, ROWIDX,3, Item.getinnerversion (), Contentformat); Excel.setval (sheet, ROWIDX,4, Item.getouterversion (), Contentformat); Excel.setval (sheet, ROWIDX,5, Item.getrom (), Contentformat); Excel.setval (sheet, ROWIDX,6, Item.getcreatetime (), Contentformat); Excel.setval (sheet, ROWIDX,6, Item.getcreatetime (), Contentformat); Excel.setval (sheet, ROWIDX,7, ITEM.GETSN (), Contentformat); Excel.setval (sheet, ROWIDX,8, ITEM.GETCSR (), Contentformat); Excel.setval (sheet, ROWIDX,9, Item.getrecommend (), Contentformat); Excel.setval (sheet, ROWIDX,10, Item.getclassfy (), Contentformat); Excel.setval (sheet, ROWIDX,11, Item.getopinion (), Contentformat); Excel.setval (sheet, ROWIDX,12, Item.getemotion (), Contentformat); Excel.setval (sheet, ROWIDX,13, Item.getregional (), Contentformat); Excel.setval (sheet, ROWIDX,14, Item.getnationstr (), Contentformat);    }                        }    }; int[] Redtitlecols = {};//Set the list header for a red style    int[] colswidths = {3000,3000,5000,15000,2500,2500,3500,5000,5000,2000,3000,3000,3000,3000,3000};//Set column widthsBeutils.exportexcel ("Result", Historytitle,redtitlecols, colswidths, response); } three. Tool class Buildexcelutils four. Dependency<dependency> <groupId>org.apache.poi</groupId> <artifactid>poi-ooxml</artifactid&gt       ; <version>3.16-beta1</version></dependency> <dependency> <groupid>org.apache.poi&lt ;/groupid> <artifactId>poi</artifactId> <version>3.16-beta1</version></dependency ><dependency> <groupId>org.apache.commons</groupId> <artifactid>commons-lang3</ Artifactid> <version>3.1</version></dependency

Import and export features

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.