Você precisa ler dados do Excel e convertê -los em dados JSON. Você escreveu uma função de teste e a conversão é normal:
JSON Conversão: org.json.jar
Classe de teste: ImportFile.java:
pacote com.siemens.util; importar java.util.arraylist; importar java.util.list; importar org.json.jsonexception; importar org.json.jsonObject; importar org.apache.poi.ss.usermodel.row; importar org.apache.poi.ss.usermodel.sheet; importar org.apache.poi.ss.usermodel.workbook; // importar com.siemens.entity.master; // importar com.siemens.service.masterService; // import com.siemens.serviceImpl.masterServiceImpl; // import com.siemens.serviceImpl.webServiceImpl; classe pública ImportFile {public static void main (string [] args) lança jsonexception {// mestre mestre = new Master (); // ApplicationContext AC = new ClassPathXMLApplicationContext ("ApplicationContext.xml"); // MasterService MS = (MasterService) AC.GetBean ("MasterService"); Pasta de trabalho wb = null; Folha de folha = nulo; Linha linha = nulo; String CellData = NULL; // caminho do arquivo, string filepath = "f: /haoxy/worktable222.xls"; wb = excelbean.readExcel (filepath); if (wb! = null) {// use para armazenar dados na lista de tabela <JsonObject> listMap = new ArrayList <JsonObject> (); // Obtenha a primeira folha = wb.getSheetat (0); // Obtenha o número máximo de linhas int rownum = sheet.getphysicalNumberOfRows (); // Obtenha a primeira linha da linha = Sheet.getRow (0); // Obtenha o número máximo de colunas int colnum = row.getphysicalNumberOfCells (); // Crie objeto JSON aqui. Se você usar o mapa na medição real, haverá problemas com JSON Data JsonObject jsonmap = new JsonObject (); // LOOP Linha para (int i = 1; i <rownum; i ++) {row = sheet.getRow (i); if (linha! = null) {// Crie objeto de lista para receber Lista de dados do Excel <String> list = new ArrayList <String> (); // coluna de loop para (int! = Null) {// Crie objeto de lista para receber Lista de dados de leitura do Excel <String> list = new ArrayList <String> (); // coluna de loop para (int! = Null) {// Crie objeto de lista para receber Lista de dados de leitura do Excel <String> list = new ArrayList <String> (); // coluna de loop para (int! = Null) {// Crie objeto de lista para receber Lista de dados de leitura do Excel <String> list = new ArrayList <String> (); // coluna de loop para (int) j = 0; j <colnum; j ++) {CellData = (String) ExcelBean.getCellFellFellFellFormatValue (row.getCell (j)); list.add (CellData); } //System.out.println(list.get(59)); // A seguir, é apresentado o formato dos dados de acordo com os requisitos.-Criar um objeto JSON relatará uma exceção e o capturará. JsonObject jsonObject2 = new jsonObject (); jsonObject2.put ("skvdorcode", list.get (0)); jsonObject2.put ("Descrição", list.get (1)); jsonObject2.put ("disciplina", list.get (2)); jsonObject2.put ("pré -fabricadoskids", 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)); // JsonObject JSONTURNKEY = novo JsonObject (); jsonturnkey.put ("PlantDesign", list.get (26)); jsonturnkey.put ("BasicDesign", list.get (27)); jsonturnkey.put ("detalheDesign", list.get (28)); jsonturnkey.put ("fornecedor", list.get (29)); jsonturnkey.put ("Erreção", list.get (30)); jsonturnkey.put ("Comissionamento", list.get (31)); jsonturnkey.put ("BlackBox", list.get (32)); jsonturnkey.put ("OptionalScope", list.get (33)); jsonturnkey.put ("observação", list.get (34)); jsonturnkey.put ("internoRemark", 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 ("detalheDesign", list.get (39)); jsonPowerCore.put ("Fornecedor", list.get (40)); jsonPowerCore.put ("Erreção", list.get (41)); jsonPowerCore.put ("Comissionamento", list.get (42)); jsonPowerCore.put ("BlackBox", list.get (43)); jsonPowerCore.put ("OptionalScope", list.get (44)); jsonPowerCore.put ("Observação", list.get (45)); jsonPowerCore.put ("InternalRemark", list.get (46)); jsonPowerCore.put ("revisão", list.get (47)); // Powerisland jsonObject jsonPowerisland = new JsonObject (); jsonPowerisland.put ("PlantDesign", list.get (48)); jsonPowerisland.put ("BasicDesign", list.get (49)); jsonPowerisland.put ("detalheDesign", list.get (50)); jsonPowerisland.put ("Fornecedor", list.get (51)); jsonPowerisland.put ("Erreção", list.get (52)); jsonPowerisland.put ("Comissionamento", list.get (53)); jsonPowerisland.put ("BlackBox", list.get (54)); jsonPowerisland.put ("OptionalScope", list.get (55)); jsonPowerisland.put ("Observação", list.get (56)); jsonPowerisland.put ("InternalRemark", list.get (57)); jsonPowerisland.put ("revisão", list.get (58)); // Crie o objeto JSONBMT e aninhe ainda mais os objetos acima jsonObject jsonbmt = new jsonObject (); jsonbmt.put ("Turnkey", JSONTURNKEY); jsonbmt.put ("PowerCore", JsonPowerCore); jsonbmt.put ("Powerisland", jsonPowerisland); // aninhe o acima vários no objeto da primeira camada jsonObject2.put ("PowerLantTypes", 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 ("não exagerado"); jsonObject2.put ("crianças", list3); } listmap.add (jsonObject2); } else {break; }} // termina para linha // Adicione um key-griddata jsonmap.put ("griddata", listmap); System.out.println (jsonmap); }}}Lendo a aula de ferramentas do Excel, consulte o método de leitura dos internautas conforme citado:
Excelbean.java:
pacote com.siemens.util; importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.io.ioException; importar java.io.inputStream; importar org.apache.poi.hssf.usermodel.hssfworkbook; importar org.apache.poi.ss.usermodel.cell; importar org.apache.poi.ss.usermodel.dateutil; importar org.apache.poi.ss.usermodel.workbook; importar org.apache.poi.xssf.usermodel.xssfworkbook; public classe ExcelBean {// Leia o Excel Public Static Works Book ReadExcel (String filepath) {Pasa de trabalho WB = NULL; if (filepath == null) {return null; } String extstring = filepath.substring (filepath.lastindexof (".")); InputStream é = nulo; tente {is = new FileInputStream (filepath); if (". xls" .equals (extring)) {return wb = new HSSFWorkbook (IS); } else if (". xlsx" .equals (extstring)) {return wb = novo xssfworkbook (is); } else {return wb = null; }} catch (filenotfoundException e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); } retornar wb; } objeto estático público getCellFormatValue (célula célula) {Object CellValue = NULL; if (célula! = null) {// JULGE TIPO DE CÉLURA (Cell.getCellType ()) {case Cell.cell_type_numeric: {CellValue = String.valueof (Cell.getNumericCellValue ()); quebrar; } case Cell.cell_type_formula: {// julgue se a célula está no formato da data if (dateUtil.IScellDateFormatted (Cell)) {// Converta até a data de formato yyyyy-dd CellValue = Cell.getDateCellValue (); } else {// número CellValue = Cell.getDateCellValue (); } else {// number CellValue = String.valueof (Cell.getNumericCellValue ()); } quebrar; } case Cell.Cell_Type_String: {CellValue = Cell.getRichStringCellValue (). getString (); quebrar; } padrão: CellValue = ""; }} else {CellValue = ""; } retornar CellValue; }}Resumir
O exposto acima são os arquivos Java Reading Excel no formato JSON introduzido pelo editor. Espero que seja útil para todos. Se você tiver alguma dúvida, deixe -me uma mensagem e o editor responderá a todos a tempo. Muito obrigado pelo seu apoio ao site wulin.com!