Excelデータを読み取り、JSONデータに変換する必要があります。テスト関数を作成しましたが、変換は正常です。
JSON変換:org.json.jar
テストクラス:Importfile.java:
パッケージcom.siemens.util; java.util.arraylistをインポートします。 java.util.listをインポートします。 org.json.jsonexceptionをインポートします。 Import org.json.jsonobject; org.apache.poi.ss.s.usermodel.rowをインポートします。 org.apache.poi.ss.s.usermodel.sheetをインポートします。 Import org.apache.poi.ss.s.usermodel.workbook; // com.siemens.entity.masterをインポートします。 // com.siemens.service.masterserviceをインポートします。 // com.siemens.serviceimpl.masterserviceimplをインポートします。 // com.siemens.serviceimpl.webserviceimplをインポートします。 public class importfile {public static void main(string [] args)throws jsonexception {// master masters = new Master(); // ApplicationContext AC = new ClassPathXMLApplicationContext( "ApplicationContext.xml"); // MasterService MS =(MasterService)AC.GetBean( "MasterService");ワークブックwb = null;シートシート= null;行の行= null;文字列cellData = null; //ファイルパス、string filepath = "f:/haoxy/worktable222.xls"; wb = excelbean.readexcel(filepath); if(wb!= null){//テーブルリストにデータを保存するために使用<jsonobject> listmap = new arraylist <jsonobject>(); //最初のシートシートを取得= wb.getsheetat(0); //行の最大数を取得しますint rownum = sheet.getphysicalNumberOfRows(); //最初の行を取得= sheet.getrow(0); //列の最大数を取得int colnum = row.getPhysicalNumberOfCells(); //ここでJSONオブジェクトを作成します。実際の測定でMAPを使用する場合、JSONデータJSonObject jsonmap = new jsonobject()に問題があります。 //(int i = 1; i <rownum; i ++){row = sheet.getrow(i); if(row!= null){//リストオブジェクトを作成して読み取りexcelデータリスト<string> list = new arraylist <string>(); //(int!= null)のループ列{//リストオブジェクトを作成して読み取りexcelデータリスト<string> list = new arraylist <string>(); //(int!= null)のループ列{//リストオブジェクトを作成して読み取りexcelデータリスト<string> list = new arraylist <string>(); //(int!= null)のループ列{//リストオブジェクトを作成して読み取りexcelデータリスト<string> list = new arraylist <string>(); //(int)j = 0; j <colnum; j ++){celldata =(string)excelbean.getCellFormatValue(row.getCell(j)); list.add(celldata); } //system.out.println(list.get(59)); //以下は、要件に従ってデータの形式です。-JSONオブジェクトを作成すると、例外が報告され、キャプチャされます。 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( "ヤード"、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 = new jsonobject(); jsonturnkey.put( "plantdesign"、list.get(26)); jsonturnkey.put( "BasicDesign"、list.get(27)); jsonturnkey.put( "detaildesign"、list.get(28)); jsonturnkey.put( "supplier"、list.get(29)); jsonturnkey.put( "Errection"、list.get(30)); jsonturnkey.put( "Commistioning"、list.get(31)); jsonturnkey.put( "blackbox"、list.get(32)); jsonturnkey.put( "optionalscope"、list.get(33)); jsonturnkey.put( "areme"、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( "supplier"、list.get(40)); jsonpowercore.put( "Errection"、list.get(41)); jsonpowercore.put( "Commistioning"、list.get(42)); jsonpowercore.put( "blackbox"、list.get(43)); jsonpowercore.put( "optionalscope"、list.get(44)); jsonpowercore.put( "areme"、list.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( "supplier"、list.get(51)); jsonpowerisland.put( "Errection"、list.get(52)); jsonpowerisland.put( "Commistioning"、list.get(53)); jsonpowerisland.put( "blackbox"、list.get(54)); jsonpowerisland.put( "optionalscope"、list.get(55)); jsonpowerisland.put( "areme"、list.get(56)); jsonpowerisland.put( "internalRemark"、list.get(57)); jsonpowerisland.put( "Revision"、list.get(58)); // jsonbmtオブジェクトを作成し、上記のオブジェクトをさらにネストするjsonobject jsonbmt = new jsonobject(); jsonbmt.put( "ターンキー"、jsonturnkey); jsonbmt.put( "PowerCore"、jsonPowercore); jsonbmt.put( "Powerisland"、jsonpowerisland); //上記のいくつかを最初のレイヤーオブジェクト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( "非empty-placeholder"); jsonobject2.put( "children"、list3); } listmap.add(jsonobject2); } else {break; }} //行の終了system.out.println(jsonmap); }}}Excel Toolクラスを読む、引用されているネチズンの読み方を参照してください。
Excelbean.java:
パッケージcom.siemens.util; java.io.fileinputStreamをインポートします。 java.io.filenotfoundexceptionをインポートします。 java.io.ioexceptionをインポートします。 java.io.inputStreamをインポートします。 org.apache.poi.hssf.usermodel.hssfworkbookをインポートします。 org.apache.poi.ss.s.usermodel.cellをインポートします。 org.apache.poi.ss.s.usermodel.dateutilをインポートします。 Import org.apache.poi.ss.s.usermodel.workbook; org.apache.poi.xssf.usermodel.xssfworkbookをインポートします。 Public Class Excelbean {// Excel Public Static Workbook readexcel(String filepath){Workbook wb = null; if(filepath == null){return null; } string extstring = filepath.substring(filepath.lastindexof( "。")); inputstream is = null; try {is = new FileInputStream(filepath); if( "。xls" .equals(extstring)){return wb = new 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(); } wbを返します。 } public staticオブジェクトgetCellFormatValue(Cellセル){object cellvalue = null; if(cell!= null){//裁判官Cell Type Switch(cell.getCellType()){case cell.cell_type_numeric:{cellvalue = string.valueof(cell.getNumericCellValue());壊す; } case cell.cell_type_formula:{//セルが日付形式であるかどうかを判断します(dateutil.iscelldateformatted(cell)){//日付形式に変換yyyy-mm-dd cellvalue = cell.getdatecellvalue(); } else {//番号CellValue = cell.getDateCellValue(); } else {//番号CellValue = String.ValueOf(cell.getNumericCellValue()); } 壊す; } case cell.cell_type_string:{cellvalue = cell.getRichStringCellValue()。getString();壊す; }デフォルト:cellvalue = ""; }} else {cellvalue = ""; } cellvalueを返します。 }}要約します
上記は、編集者によって導入されたJSON形式にExcelファイルを読んでいるJavaです。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!