Sie müssen Excel -Daten lesen und in JSON -Daten umwandeln. Sie haben eine Testfunktion geschrieben und die Konvertierung ist normal:
JSON -Konvertierung: org.json.jar
Testklasse: ImportFile.java:
Paket com.siemens.util; Import Java.util.ArrayList; importieren java.util.list; import org.json.jsonexception; import org.json.jsonObject; import org.apache.poi.s.usermodel.row; import org.apache.poi.s.usermodel.sheet; import org.apache.poi.s.usermodel.workbook; // com.siemens.entity.master importieren; // com.siemens.service.masterservice; // com.siemens.serviceImpl.masterServiceImpl; // com.siemens.serviceImpl.webserviceImpl; public class ImportFile {public static void main (String [] args) löst jSonexception {// Master Masters = new Master () aus; // ApplicationContext ac = new classPathXmlApplicationContext ("ApplicationContext.xml"); // MastertService MS = (Masterservice) ac.getBean ("MasterService"); Arbeitsbuch WB = NULL; Blatt Blatt = null; Row row = null; String cellData = null; // Dateipfad, String filepath = "f: /haoxy/worktable222.xls"; wb = excelbean.readExcel (filepath); if (wb! // das erste Blatt abrufen = wb.getsheetat (0); // Die maximale Anzahl von Zeilen intowum = Sheet.GetPhysicalNumberofrows () erhalten; // die erste zeile zeile = sheet.getrow (0); // Erhalten Sie die maximale Anzahl von Spalten int colnum = row.getPhysicalNumberofcells (); // Erstellen Sie hier das JSON -Objekt. Wenn Sie MAP in der tatsächlichen Messung verwenden, wird es Probleme mit JSON -Daten geben. // Loop Row für (int i = 1; i <rownum; i ++) {row = sheet.getRow (i); if (row! // Schleifespalte für (int! = Null) {// Erstellen Sie das Listenobjekt, um die Liste der Excel -Daten zu empfangen. // Schleifespalte für (int! = Null) {// Erstellen Sie das Listenobjekt, um die Liste der Excel -Daten zu empfangen. // Schleifenspalte für (int! = Null) {// Erstellen Sie das Listenobjekt, um die Ausleser -Excel -Datenliste <string> list = new ArrayList <string> () zu empfangen; // Loop -Spalte für (int) j = 0; j <colnum; j ++) {cellData = (String) excelbean.getCellFormAtValue (row.getCell (j)); list.add (cellData); } //System.out.println(List.get(59)); // Folgendes ist das Format der Daten gemäß den Anforderungen.-Erstellen eines JSON-Objekts meldet eine Ausnahme und erfasst sie. JsonObject jsonObject2 = new JSONObject (); JsonObject2.put ("skvdorcode", list.get (0)); JsonObject2.put ("Beschreibung", list.get (1)); JsonObject2.put ("Disziplin", list.get (2)); JsonObject2.put ("vorgefertigte Kids", List.get (3)); JsonObject2.put ("onrack", list.get (4)); JsonObject2.put ("offrack", list.get (5)); JsonObject2.put ("Yard", list.get (6)); JsonObject2.put ("hsrg", list.get (7)); JsonObject jsonpptdata = new JsonObject (); Jsonpptdata.put ("sc ff", list.get (8)); JSONPPTDATA.PUT ("CC SS CT", List.get (9)); JSONPPTDATA.PUT ("CC SS OCC", List.get (10)); JSONPPTDATA.PUT ("CC SS ACC", List.get (11)); JSONPPTDATA.PUT ("CC MS CT", List.get (12)); JSONPPTDATA.PUT ("CC MS OCC", List.get (13)); JSONPPTDATA.PUT ("CC MS ACC", List.get (14)); // Turnkey JsonObject JSONTURNKEY = new JsonObject (); jSonturnKey.put ("plantdesign", list.get (26)); jSonturnKey.put ("BasicDesign", List.get (27)); jSonturnKey.put ("detaildesign", list.get (28)); JSONTURNKEY.PUT ("Lieferant", list.get (29)); JSONTURNKEY.PUT ("ERREKEKTION", LIST.get (30)); JSONTURNKEY.PUT ("Inbetriebnahme", Liste.get (31)); jSonturnKey.put ("Blackbox", list.get (32)); JSONTURNKEY.PUT ("optionalScope", list.get (33)); jSonturnKey.put ("Bemerkung", list.get (34)); JSONTURNKEY.PUT ("InternalRemark", List.get (35)); JSONTURNKEY.PUT ("Revision", List.get (36)); // PowerCore jsonObject jsonPowercore = new JsonObject (); JsonPowerCore.put ("Plantdesign", list.get (37)); JsonPowerCore.put ("BasicDesign", List.get (38)); JsonPowerCore.put ("detaildesign", list.get (39)); JsonPowerCore.put ("Lieferant", Liste.get (40)); JsonPowerCore.put ("eRRECTION", list.get (41)); JsonPowerCore.put ("Inbetriebnahme", Liste.get (42)); JsonPowercore.put ("Blackbox", list.get (43)); JsonPowerCore.put ("optionalScope", list.get (44)); JsonPowercore.put ("Bemerkung", Liste.get (45)); JsonPowerCore.put ("InternalRemark", List.get (46)); JsonPowerCore.put ("Revision", list.get (47)); // Powerisland JsonObject jsonPowerisland = new JsonObject (); JsonPowerisland.put ("Plantdesign", list.get (48)); JsonPowerisland.put ("BasicDesign", List.get (49)); JsonPowerisland.put ("detaildesign", list.get (50)); JsonPowerisland.put ("Lieferant", Liste.get (51)); JsonPowerisland.put ("ERREKECTION", LIST.get (52)); JSONPOWERISLAND.PUT ("Inbetriebnahme", List.get (53)); JsonPowerisland.put ("Blackbox", List.get (54)); JsonPowerisland.put ("optionalScope", list.get (55)); JsonPowerisland.put ("Bemerkung", Liste.get (56)); JsonPowerisland.put ("InternalRemark", List.get (57)); JsonPowerisland.put ("Revision", List.get (58)); // JSONBMT -Objekt erstellen und die oben genannten Objekte weiter nisten. JSONBMT.PUT ("Turnkey", jSonturnKey); JSONBMT.PUT ("PowerCore", JsonPowerCore); JSONBMT.PUT ("Powerisland", JsonPowerisland); // Nest die oben genannten in das erste Schichtobjekt JSONObject2.put ("PowerPlantTypes", Jsonpptdata); JsonObject2.put ("BusinessMixTypes", JSONBMT); JsonObject2.put ("Treedepth", Integer.ParseInt (list.get (59) .Substring (0, list.get (59) .Indexof ('.')))); if (integer.parseInt (list.get (59) .Substring (0, list.get (59) .Indexof ('.'))) <= 2) {list <string> list3 = new ArrayList <string> (); List3.Add ("Nicht-leer-Platzhalter"); JsonObject2.put ("Kinder", List3); } listmap.add (jsonObject2); } else {break; }} // Ende für Row // Fügen Sie ein Key-GridData jsonmap.put ("GridData", listMap) hinzu; System.out.println (JSONMAP); }}}Wenn Sie die Excel -Tool -Klasse lesen, siehe die Lesemethode der Internetnutzer wie zitiert:
Excelbean.java:
Paket com.siemens.util; import Java.io.FileInputStream; importieren java.io.filenotfoundException; importieren java.io.ioException; importieren java.io.inputstream; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.s.usermodel.cell; import org.apache.poi.s.usermodel.dateUtil; import org.apache.poi.s.usermodel.workbook; import org.apache.poi.xssf.usermodel.xssfworkbook; public class excelbean {// excel public static Workbook ReadExcel (String filepath) {Arbeitsbuch wb = null; if (filepath == null) {return null; } String extstring = filepath.substring (filepath.lastindexof (".")); InputStream ist = null; try {is = new FileInputStream (filepath); if (". xls" .Equals (extstring)) {return wb = new HSSFWorkbook (ist); } else if (". xlsx" .Equals (extstring)) {return wb = new XSSFWorkbook (ist); } else {return wb = null; }} catch (FilenotFoundException e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); } return wb; } öffentliches statisches Objekt getCellFormatValue (Zellzelle) {Object CellValue = null; if (cell! brechen; } case cell.cell_type_formula: {// beurteilen, ob die Zelle im Datumsformat if (dateUtil.iscellDateFormated (cell)) {// bis zum Datumsformat yyyy-mm-dd cellValue = cell.getDatecellValue () konvertieren; } else {// number cellValue = cell.getDateCellValue (); } else {// number cellValue = string.ValueOf (cell.getNumericcellValue ()); } brechen; } case cell.cell_type_string: {cellValue = cell.getrichstringCellValue (). getString (); brechen; } Standard: cellValue = ""; }} else {cellValue = ""; } return cellValue; }}Zusammenfassen
Das obige ist die vom Editor eingeführte Java Reading Excel -Dateien in JSON -Format. Ich hoffe, es wird für alle hilfreich sein. Wenn Sie Fragen haben, hinterlassen Sie mir bitte eine Nachricht und der Editor wird allen rechtzeitig antworten. Vielen Dank für Ihre Unterstützung auf der Wulin.com -Website!