/** * Excel Document Goto Object * * @author DXM * */public class Exceltoobject {/** * convert * * @param file */public static void convert (F ile file, string Savepath, string pack) {//Excel exists if (null = = File | |!file.exists ()) {return;} Create storage path Directory File Save = new file (Savepath), if (!save.exists ()) {save.mkdirs ();} try {//Excel filename string filename = File.getname ();//Create input stream InputStream input = new FileInputStream (file); Workbook WB = null;//initializes the IF (Filename.endswith ("xlsx")) {wb = new Xssfworkbook (input) According to the file format (2003 or 2007);} else {wb = (Wor Kbook) New Hssfworkbook (input);} Get the first form sheet sheet = wb.getsheetat (0);//File output stream FileOutputStream fos = null;//Get the first form iterator iterator<row> rows = Shee T.rowiterator ();//INSERT statement list<string> typelist = new arraylist<string> (); list<string> namelist = new arraylist<string> (); StringBuffer sb = new StringBuffer ();//pack Name pack = "Package" + pack + "; \ n";//The iterator that gets the first form while (Rows.hasnext ()) {//Gets the number of rows According to row row = Rows.next ();//Column 0,1,2 represents the property name, type, description cell cell0 = ROW.getcell (0); Cell cell1 = Row.getcell (1); Cell cell2 = Row.getcell (2); String name = Cell0.getstringcellvalue (). Trim (); String type = Cell1.getstringcellvalue (). Trim (); String mark = Cell2.getstringcellvalue (). Trim ();//filter NULL data if (Name.isempty ()) {continue;} Filter description if (Name.equals ("field")) {continue;} The new table starts int index = TYPE.INDEXOF ("T_"); if ( -1! = index) {//writes to the previous class file if (null! = FOS) {getset (typelist, NameList, SB); Fos.wri Te (sb.tostring () getBytes ("UTF-8"); Fos.close (); Typelist.clear (); Namelist.clear (); sb = new StringBuffer ();} Class Initial Capital type = type.substring (index + 2); String C = string.valueof (Type.charat (0)), C = c.touppercase (), type = c + type.substring (1);//delete old class file F = new (sav Epath + "/" + Type + ". Java"), if (F.exists ()) {F.delete ();} Create file output stream fos = new FileOutputStream (f);//Splicing Output Statement sb.append (pack), Sb.append ("/**\n *"); Sb.append (name); Sb.append ("\ n * \ n * @author juling\n *\n */\n "); Sb.append (" public class "); Sb.append (type);//Splicing Inherits Class if (!mark.isempty ()) {Sb.append (" ext Ends "); Sb.append (Mark); Sb.append ("{\ n"); Sb.append ("\tprivate static final long serialversionuid = -1l;\n"); if (Mark.equals ("VData") {Sb.insert (Pack.length (), "Import com.mohe.common.db.vdata;\n\n");}} else {sb.append ("{\ n");} Continue;} The stitching output statement sb.append ("\ n"), Sb.append ("\t//"), Sb.append (Mark), Sb.append ("\ n"), Sb.append ("\tpublic"); Sb.append (type ); Sb.append (""); Sb.append (name); Sb.append ("; \ n"); If you insert the import java.util.Date package for the time type (type.equals ("Date")) {Sb.insert (Pack.length (), "Import java.util.date;\n\n");} Typelist.add (type); Namelist.add (name);} Writes the last class file if (null! = FOS) {getset (typelist, NameList, SB), Fos.write (Sb.tostring (). GetBytes ("UTF-8")); Fos.close (); Typelist.clear (); Namelist.clear (); sb = new StringBuffer ();}} catch (IOException ex) {ex.printstacktrace ();}} /** * Fill Get,set method */private static void Getset (List<string> typelist, list<string> namelist, StringBuffer SB) {for (int i = 0; i < typelist.size (); i++) {//attribute type and name string type = Typelist.get (i); String name = Namelist.get (i);Get,set method The first letter of the uppercase String C = string.valueof (Name.charat (0)); c = C.touppercase (); String n = c + name.substring (1);//Splicing Output Statement sb.append ("\n\tpublic"); Sb.append (type); Sb.append ("get"); Sb.append (n); Sb.append ("() {"); Sb.append ("\n\t\treturn"); Sb.append (name); Sb.append ("; \n\t}\n"); Sb.append ("\n\tpublic void Set" ); Sb.append (n); Sb.append ("("); Sb.append (type); Sb.append (""); Sb.append (name); Sb.append (") {"); Sb.append ("\n\t\ Tthis. "); Sb.append (name); Sb.append ("="); Sb.append (name); Sb.append ("; \n\t}\n");} Sb.append ("\ n}"); public static void Main (string[] args) throws Interruptedexception {String app = System.getproperty ("User.dir"); String Modelpath = app + "*"; Exceltoobject.convert (New File ("*"), Modelpath, "*"), Objecttomapping.convert (new file (Modelpath), App + "*", "TimeKey" , true);}}
Java generates entity classes based on fixed-format Excel