Using the POI technology to generate Excel, use reflection technology to implement automatic mapping of the data in the list.
Exporttableutil.java
public class Exporttableutil {/** * * @Description: Get a CSV-formatted string * @param @param header * @param @param fieldnamelist corresponding property name corresponding to the table header and the value to the data type's property name * @param @param params data * @param @return * @param @throws illegalargumentexception * @param @thr oWS illegalaccessexception * @param @throws nosuchfieldexception * @param @throws securityexception settings file * @return String return type */public static String csv (string[] headlist, string[] fieldnamelist, list<?> params) throws Illegalargumentex Ception, Illegalaccessexception,nosuchfieldexception, SecurityException {StringBuilder StringBuilder = new StringBuilder ();//Add head on firstfor (int i = 0; null! = Headlist && i < headlist.length; i++) {Stringbuilde R.append (Headlist[i]), if (I < headlist.length-1) {stringbuilder.append (",");} else {stringbuilder.append ("\ r \ n");}} Add data from second line to---for (int i = 0; null! = params && i < params.size (); i++) {class<? extend s object> clazz = Params.get (i). Getclass (); for (int j = 0; Null! = Fieldnamelist && J < Fieldnamelist.length; J + +) {String FieldName = fieldnamelist[j ];if (!fieldname.contains (".")) {Field field = Clazz.getdeclaredfield (FieldName); if (null! = Field) {field.setaccessible (true); Object obj = Field.get ( Params.get (i)); if (null! = obj) {stringbuilder.append (obj.tostring ());}} else {stringbuilder.append ("");} if (J < fieldnamelist.length-1) {stringbuilder.append (",");}} Else{object param = Params.get (i), Object valobj = Vectorobj (clazz, FieldName, param); if (null!=valobj) { Stringbuilder.append (Valobj.tostring ());} else {stringbuilder.append ("");} if (J < fieldnamelist.length-1) {stringbuilder.append (",");}}} Stringbuilder.append ("\ r \ n");} return stringbuilder.tostring ();} /** * * @Description: Download documents via response * @param @param response * @param @param fileName * @param @param headlist * @param @param fieldnamelist * @param @param params settings file * @return void return type */public static void Httpexportcsv (HttpServletRequest Request, httpservletresponse response, String fileName, string[] headlist,string[] fieldnamelist, list<?> params) {Map <string, object> map = new hashmap<string, object> (); try {response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Application/x-download"); final String useragent = Request.getheader ("user-agent"); String csvcontent = CSV (headlist, fieldnamelist, params); String finalfilename = null;if (Stringutils.contains (useragent, "MSIE")) {//IE browser finalfilename = Urlencoder.encode ( FileName, "UTF8");} else if (Stringutils.contains (useragent, "Mozilla")) {//Google, Firefox finalfilename = new String (Filename.getbytes (), " Iso8859-1 ");} else {finalfilename = Urlencoder.encode (FileName, "UTF8");//Other Browser}response.setheader ("Content-disposition", " Attachment Filename=\ "" + finalfilename + "\" "); Response.getoutputstream (). Write (Csvcontent.getbytes ());} catch (IllegalArgumentException | illegalaccessexception | nosuchfieldexception | SecurityException | IOException e) {E.printStackTrace (); Map.put ("State", "202"), Map.put ("message", "Data Conversion exception"), try {response.getoutputstream (). Write ( Jsonutils.tojsonstring (map). GetBytes ());} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}}} /** * * @Description: Get the binary stream of Excel table * @param @param headlist header * @param @param fieldnamelist property name corresponds to the header order and must exist in the data type Name corresponds to * @param @param params * @param @return * @param @throws illegalargumentexception * @param @throws Illegalaccessexce Ption * @param @throws nosuchfieldexception * @param @throws SecurityException * @param @throws ioexception settings file * @return Byte[] return type */public static byte[] xls (string[] headlist, string[] fieldnamelist, list<?> params) throws Illegalarg Umentexception, Illegalaccessexception,nosuchfieldexception, SecurityException, IOException {Workbook work = new Hssfworkbook (); Sheet Sheet = Work.createsheet (); Row rowone = sheet.createrow (0); for (int i = 0; null! = Headlist && i < headlist.length; i++) {//Header cell CellOn E = Rowone.createcell (i); Cellone.setcellvalue (Headlist[i]);//Fill value}//data fill for (int i = 0; null! = params && i < P Arams.size (); i++) {class<? extends object> clazz = Params.get (i). GetClass (); Row DataRow = Sheet.createrow (i + 1); for (int j = 0; Null! = Fieldnamelist && J < Fieldnamelist.length; J + +) {S Tring fieldName = Fieldnamelist[j]; Cell cell = Datarow.createcell (j); if (!fieldname.contains (".")) {Field field = Clazz.getdeclaredfield (FieldName); field.setaccessible (true); Object obj = Field.get (Params.get (i)); if ( Null! = obj) {if (obj instanceof String) {Cell.setcellvalue (obj.tostring ());} else if (obj instanceof Double) {Cell.setcel LValue (double) obj); else if (obj instanceof Boolean) {cell.setcellvalue ((Boolean) obj),} else if (obj instanceof Date) {Cell.setcellvalue (Dat e) obj);} else {Cell.setcellvalue (obj.tostring ());}}} else if (Fieldname.contains (".")) {Object param = params.get (i); object valobj = Vectorobj (clazz, FieldName, param); Cell.setcellvalue (nulL = = valobj? Null:valObj.toString ());}}} Byteoutputstream BOS = new Byteoutputstream (); Work.write (BOS); Work.close (); return bos.getbytes ();} private static Object vectorobj (class<? extends Object> Clazz, String fieldName, Object obj) throws Nosuchfieldexcep tion, securityexception,illegalargumentexception, illegalaccessexception {if (!fieldname.contains (".")) {Field field = Clazz.getdeclaredfield (FieldName); field.setaccessible (true); return Field.get (obj);} else {String fieldchildname = fieldname.substring (0, Fieldname.indexof (".")); O Bject NEWOBJ = null;if (null! = fieldchildname) {Field field = Clazz.getdeclaredfield (Fieldchildname); Field.setaccessible (true); newObj = Field.get (obj), if (NEWOBJ = = null) {return null;} else {class<? extends object> C LAZZ2 = Newobj.getclass (); String fieldotherchildname = fieldname.substring (Fieldname.indexof (".") + 1); return Vectorobj (CLAZZ2, Fieldotherchildname, NEWOBJ);}} return null;}} /** * * @Description: Export xls table-------------start with the first column * @param @Param request * @param @param response * @param @param filename * @param @param headlist header * @param @param fieldnamel The Ist property name is the same as the property name of the object type in the data list, corresponding to the order of the table header * @param @param params settings file * @return void return type */public static void Httpexportxls (H Ttpservletrequest request, HttpServletResponse response, String fileName, string[] headlist,string[] fieldnamelist, list<?> params) {map<string, object> Map = new hashmap<string, object> (); try { Response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Application/x-download"); final String useragent = Request.getheader ("User-agent"); byte[] xlscontent = xls (headlist, fieldnamelist, params); String finalfilename = null;if (Stringutils.contains (useragent, "MSIE")) {//IE browser finalfilename = Urlencoder.encode ( FileName, "UTF8");} else if (Stringutils.contains (useragent, "Mozilla")) {//Google, Firefox finalfilename = new String (Filename.getbytes (), " Iso8859-1 ");} else {finalfilename = Urlencoder.encode (FileName, "UTF8");//Other browsers}responSe.setheader ("Content-disposition", "attachment; Filename=\ "" + finalfilename + "\" "); Response.getoutputstream (). write (xlscontent);} catch (IllegalArgumentException | illegalaccessexception | nosuchfieldexception | SecurityException | IOException e) {e.printstacktrace (); Map.put ("State", "202"); Map.put ("Message", "Data Conversion exception"); try { Response.getoutputstream (). Write (jsonutils.tojsonstring (map). GetBytes ());} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}}} /** * * @Description: Export the corresponding file according to the suffix name of the path * @param @param request * @param @param response * @param @param fileName---------- --File name (format *.xls,*.csv) * @param @param headlist--------------table header content * @param @param Fieldnamelist----------property names are the same as the names of the types in the data list, and are mapped by sequential and table headers. * @param @param params--------------data * @param @throws Exception----file name is not valid * @return void return type */public static void http Export (HttpServletRequest request, httpservletresponse response, String fileName, string[] headlist,string[] Fieldnamelist, list<?&Gt params) throws Exception {if (null = = FileName | | Stringutils.isempty (filename) {throw new NullPointerException ("file name cannot be empty");} else {String suffix = filename.substring ( Filename.indexof (".") + 1); if (null! = suffix) {System.out.println (suffix); switch (suffix) {case "CSV": Httpexportcsv ( Request, response, FileName, headlist, fieldnamelist, params) break;case "xls": Httpexportxls (Request, Response, FileName, headlist, fieldnamelist, params), break;case "xlsx": Httpexportxls (Request, Response, FileName, Headlist, Fieldnamelist, params); Break;case "Doc": Break;case "docx": Break;case "PDF": Break;}} else {throw new Exception ("The format of the file name is not valid");}}}
Java uses POI for Excel table generation