Packagecom. Heibeiedu.test2;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportJava.lang.reflect.Method;Importjava.util.List;ImportJava.util.Map;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;ImportJavax.swing.JOptionPane;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;Importorg.apache.poi.hssf.usermodel.HSSFRichTextString;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; Public classListtoexcel {/** * * @paramname * Excel saved theme name *@paramData * contains map and list map store field correspondence (Ziduan, the first character of the field is ordinal) * List storage object (listData) *@return[0] is filename [1] is filepath*/ Public Staticstring[] Objlisttoexcel (string name, map data, string path) {Map<string, string> Ziduan = (map<string, string>) data.get ("Ziduan"); List ListData= (List) data.get ("ListData"); Object[] Keys=Ziduan.keyset (). ToArray (); String[] Ziduankeys=NewString[keys.length]; for(intk = 0; K < Keys.length; k++) {String temp=keys[k].tostring (); intXuhao = integer.valueof (temp.substring (0, 1)); Ziduankeys[xuhao]= temp.substring (1); } Try{File NewFile=NewFile (path); if(Newfile.exists ()) {intII = Joptionpane.showconfirmdialog (NULL, "The file already exists, are you sure you want to overwrite it?" "," File already exists ", joptionpane.yes_no_option); if(ii = = 0) {//DetermineNewfile.createnewfile (); System.out.println ("Create file succeeded:" +path); Hssfworkbook WB=NewHssfworkbook (); Hssfsheet sheet=Wb.createsheet (); for(inti = 0; I < listdata.size (); i++) {Hssfrow row=Sheet.createrow (i); Object obj=Listdata.get (i); for(intj = 0; J < Ziduankeys.length; J + +) {Hssfcell cell=Row.createcell (j); if(i = = 0) {sheet.setcolumnwidth (J,6000); Cell.setcellvalue (NewHssfrichtextstring (Ziduan.get (j +ziduankeys[j])); } Else{String Ziduanname=(String) ziduankeys[j]; System.out.println (Ziduanname); Ziduanname= Ziduanname.replacefirst (ziduanname.substring (0, 1), ziduanname.substring (0, 1). toUpperCase ()); Ziduanname= "Get" +Ziduanname; Class Clazz=Class.forName (Obj.getclass (). GetName ()); Method[] Methods=Clazz.getmethods (); Pattern Pattern=Pattern.compile (ziduanname); Matcher Mat=NULL; for(Method m:methods) {Mat=Pattern.matcher (M.getname ()); if(Mat.find ()) {Object shuxing= M.invoke (obj,NULL); if(Shuxing! =NULL) {Cell.setcellvalue (shuxing.tostring ());//you can do data format processing here.}Else{Cell.setcellvalue (""); } Break; } } } } } OutputStream out=NewFileOutputStream (path); Wb.write (out);//Write FileOut.flush (); Out.close (); } } Else{newfile.createnewfile (); System.out.println ("Create file succeeded:" +path); Hssfworkbook WB=NewHssfworkbook (); Hssfsheet sheet=Wb.createsheet (); for(inti = 0; I < listdata.size (); i++) {Hssfrow row=Sheet.createrow (i); Object obj=Listdata.get (i); for(intj = 0; J < Ziduankeys.length; J + +) {Hssfcell cell=Row.createcell (j); if(i = = 0) {sheet.setcolumnwidth (J,6000); Cell.setcellvalue (NewHssfrichtextstring (Ziduan.get (j +ziduankeys[j])); } Else{String Ziduanname=(String) ziduankeys[j]; System.out.println (Ziduanname); Ziduanname= Ziduanname.replacefirst (ziduanname.substring (0, 1), ziduanname.substring (0, 1). toUpperCase ()); Ziduanname= "Get" +Ziduanname; Class Clazz=Class.forName (Obj.getclass (). GetName ()); Method[] Methods=Clazz.getmethods (); Pattern Pattern=Pattern.compile (ziduanname); Matcher Mat=NULL; for(Method m:methods) {Mat=Pattern.matcher (M.getname ()); if(Mat.find ()) {Object shuxing= M.invoke (obj,NULL); if(Shuxing! =NULL) {Cell.setcellvalue (shuxing.tostring ());//you can do data format processing here.}Else{Cell.setcellvalue (""); } Break; }}}} OutputStream out=NewFileOutputStream (path); Wb.write (out);//Write FileOut.flush (); Out.close (); } return NULL; } Catch(Exception e) {e.printstacktrace (); return NULL; } }}
The Test code is:
Try{List ListData=NewArrayList ();//---------should traverse the real list here, but there is no way to test webservice now, so write the test code first----------------------------- for(inti = 0; I < 6; i++) {Checkperson person=NewCheckperson (); PERSON.SETDHHM ("145647583486"); PERSON.SETGMSFHM ("13546876874786643"); PERSON.SETMZ ("MZ"); PERSON.SETXM ("XM"); PERSON.SETXZ ("XZ"); Listdata.add (person); } Map<string, string> Ziduan =NewHashmap<string, string>(); Ziduan.put ("0DHHM", "phone number");//the number in front of the property represents the order of the fields. Ziduan.put ("1MZ", "tentative");//It is better to change the format of the source code to "ordinal-field". Ziduan.put ("2XM", "name"); Ziduan.put ("3XZ", "Address"); Map Data=NewHashMap (); Data.put ("ListData", ListData); Data.put ("Ziduan", Ziduan); Listtoexcel.objlisttoexcel ("Search results list for demolition workers", Data,path); }Catch(Exception e3) {e3.printstacktrace (); }
java-list<object> form client to Excel file