Vous devez lire les données Excel et les convertir en données JSON. Vous avez écrit une fonction de test et la conversion est normale:
Conversion JSON: org.json.jar
Classe de test: ImportFile.java:
Package Com.Siemens.util; import java.util.arraylist; Importer java.util.list; import org.json.jsonexception; import org.json.jsonObject; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workBook; // Importer com.simens.entity.master; // Importer com.Siemens.service.masterservice; // Importer com.Siemens.ServiceImpl.masterserviceImpl; // Importer com.Siemens.ServiceImpl.WebServiceImpl; classe publique ImportFile {public static void main (String [] args) lève jSonException {// Master Masters = new Master (); // applicationContext ac = new ClassPathXmlApplicationContext ("ApplicationContext.xml"); // MasterService MS = (MasterService) ac.getBean ("MasterService"); Classeur wb = null; Feuille de feuille = null; Row Row = NULL; String CellData = null; // Chemin de fichier, chaîne filepath = "f: /haoxy/worktable222.xls"; wb = excelbean.readExcel (filepath); if (wb! = null) {// Utiliser pour stocker des données dans la liste de table <JSonObject> listMap = new ArrayList <JSonObject> (); // Obtenez la première feuille de feuille = wb.getSheetAt (0); // Obtenez le nombre maximum de lignes int rownum = sheet.getPhysicalNumberofrows (); // Obtenez la première ligne de ligne = sheet.getRow (0); // Obtenez le nombre maximum de colonnes int colnum = row.getPhysicalNumberofCells (); // Créez l'objet JSON ici. Si vous utilisez MAP dans la mesure réelle, il y aura des problèmes avec les données JSON JSONObject JSONMAP = new JSONObject (); // ligne de boucle pour (int i = 1; i <rownum; i ++) {row = sheet.getRow (i); if (row! = null) {// Créez l'objet de liste pour recevoir la liste de données Excel <string> list = new ArrayList <string> (); // Colonne de boucle pour (int! = Null) {// Créez l'objet List pour recevoir la liste de données Excel lecture <string> list = new ArrayList <string> (); // Colonne de boucle pour (int! = Null) {// Créez l'objet List pour recevoir la liste de données Excel lecture <string> list = new ArrayList <string> (); // Colonne de boucle pour (int! = Null) {// Créez l'objet de liste pour recevoir la liste de données Excel lecture <string> list = new ArrayList <string> (); // Colonne de boucle pour (int) j = 0; j <colnum; j ++) {CellData = (String) ExcelBean.getCellFormatValue (row.getCell (j)); list.add (CellData); } //System.out.println(List.get(59)); // Ce qui suit est le format des données en fonction des exigences .-- La création d'un objet JSON rapportera une exception et la capturera. JSONObject JSONObject2 = new JSONObject (); JSONObject2.put ("skvdorcode", list.get (0)); JSONObject2.put ("Description", list.get (1)); JSONObject2.put ("discipline", list.get (2)); JSONObject2.put ("PrefabricatedSKids", 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)); JSONTURKKEY.PUT ("DetailDesign", List.get (28)); JSONTURKKEY.PUT ("Fournisseur", List.get (29)); JSONTURNKEY.PUT ("Errection", List.get (30)); jsonturnkey.put ("Commission", list.get (31)); JSONTURKKEY.PUT ("BlackBox", List.Get (32)); JSONTURNKEY.PUT ("OptionAlScope", list.get (33)); JSONTURKKEY.PUT ("Remarque", List.get (34)); JSONTURKKEY.PUT ("InternalRemark", List.get (35)); JSONTURKKEY.PUT ("révision", 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 ("fournisseur", list.get (40)); jsonpowercore.put ("errection", list.get (41)); jsonpowercore.put ("Commission", list.get (42)); jsonpowercore.put ("Blackbox", list.get (43)); JSONPowerCore.put ("OptionAlScope", list.get (44)); JSONPowerCore.put ("Remarque", list.get (45)); jsonpowercore.put ("internalRark", list.get (46)); jsonpowercore.put ("révision", 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 ("fournisseur", list.get (51)); jsonpowerisland.put ("errection", list.get (52)); jsonpowerisland.put ("Commission", list.get (53)); jsonpowerisland.put ("Blackbox", list.get (54)); jsonpowerisland.put ("optionalscope", list.get (55)); jsonpowerisland.put ("Remarque", list.get (56)); jsonpowerisland.put ("InternalRemark", list.get (57)); jsonpowerisland.put ("révision", list.get (58)); // Créer un objet JSONBMT et nid les objets ci-dessus jsonObject JSONBMT = new JSONObject (); jsonbmt.put ("clé en main", jSonturnkey); JSONBMT.PUT ("PowerCore", JSONPowerCore); JSONBMT.PUT ("Powerisland", Jsonpowerisland); // nid ci-dessus 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 ("non-vide-place"); JSONObject2.put ("Enfants", List3); } listmap.add (jsonObject2); } else {break; }} // End For Row // Ajouter un Key-GridData JSONMAP.PUT ("GRIDDATA", ListMap); System.out.println (JSONMAP); }}}Lire la classe d'outils Excel, voir la méthode de lecture des internautes comme cité:
Excelbean.java:
Package Com.Siemens.util; import java.io.fileInputStream; import java.io.filenotfoundException; Importer java.io.ioException; import java.io.inputStream; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.dateutil; import org.apache.poi.ss.usermodel.workBook; import org.apache.poi.xssf.usermodel.xssfworkbook; classe publique ExcelBean {// Read Excel Public Static Workbook ReadExcel (String FilePath) {Workbook WB = NULL; if (filePath == null) {return null; } String extString = filepath.substring (filePath.LastIndexof (".")); InputStream est = null; try {is = new FileInputStream (filePath); if (". xls" .equals (extString)) {return wb = nouveau hssfworkbook (is); } else if (". xlsx" .equals (extString)) {return wb = new xssfworkbook (is); } else {return wb = null; }} catch (filenotFoundException e) {e.printStackTrace (); } catch (ioException e) {e.printStackTrace (); } return wb; } Objet statique public getCellFormatValue (Cell Cell) {objet CellValue = null; if (cell! = null) {// juge de type de cellule Switch (Cell.getCellType ()) {Case Cell.Cell_Type_Numeric: {CellValue = String.Valueof (Cell.GetNumericCellValue ()); casser; } Case Cell.Cell_Type_Formula: {// juger si la cellule est dans le format de date if (dateUtil.iscellDateFormatted (Cell)) {// converti au format date yyyy-mm-dd cellValue = cell.getDateCellValue (); } else {// nombre CellValue = Cell.getDateCellValue (); } else {// Number CellValue = String.ValueOf (Cell.GetNumericCellValue ()); } casser; } case Cell.Cell_Type_String: {CellValue = Cell.getRichStringCellValue (). GetString (); casser; } Default: CellValue = ""; }} else {CellValue = ""; } return CellValue; }}Résumer
Ce qui précède est les fichiers Java Reading Excel au format JSON introduit par l'éditeur. J'espère que ce sera utile à tout le monde. Si vous avez des questions, veuillez me laisser un message et l'éditeur répondra à tout le monde à temps. Merci beaucoup pour votre soutien au site Web Wulin.com!