1 ImportJXL. Cell;2 ImportJXL. Sheet;3 ImportJXL. Workbook;4 ImportNet.sf.json.JSONArray;5 ImportNet.sf.json.JSONObject;6 7 ImportJava.io.FileInputStream;8 ImportJava.io.InputStream;9 ImportJava.util.HashMap;Ten ImportJava.util.Map; One A Public classMain { - - Public Static voidMain (string[] args) { theString Excelpath = "E:\\workspace\\helloworld\\data\\fund.xls"; -Map<string,string> Fundmaps =Readexcel (excelpath); - Jsonbuild (fundmaps); - } + - Private Static voidjsonbuild (Map fundmaps) { + intSize =fundmaps.size (); A for(Object key:fundMaps.keySet ()) { atObject DisplayName =Fundmaps.get (key); -Jsonobject Jsonobject =NewJsonobject (); -Jsonarray Jsonarray =NewJsonarray (); -Jsonobject JsonInnerObject1 =NewJsonobject (); -Jsonobject JsonInnerObject2 =NewJsonobject (); -Jsoninnerobject1.put ("Keywordsinput", displayName); inJsoninnerobject1.put ("Weight", "10"); -Jsoninnerobject2.put ("Keywordsinput", key); toJsoninnerobject2.put ("Weight", "12"); + Jsonarray.add (jsonInnerObject1); - Jsonarray.add (JSONINNEROBJECT2); theJsonobject.put ("Keywordstype", "Product"); *Jsonobject.put ("Keywordsdisplay", displayname+ "" +key); $Jsonobject.put ("Array", Jsonarray);Panax NotoginsengSystem.out.println (jsonobject+ ","); - } the } + A Private StaticMap<string,string>Readexcel (String excelpath) { the Workbook Excelread; +Map Fundmaps =NewHashmap<string, string>(); - Try { $InputStream instream =NewFileInputStream (excelpath); $Excelread =Workbook.getworkbook (instream); -Sheet sheetread = Excelread.getsheet (0); - introws =sheetread.getrows (); the for(inti = 1; i < rows; i++) { -Cell cell0 = Sheetread.getcell (0, i);WuyiCell cell1 = Sheetread.getcell (1, i); the Fundmaps.put (Cell0.getcontents (), cell1.getcontents ()); - } Wu}Catch(Exception e) { -SYSTEM.OUT.PRINTLN ("Excel Read failed:" +e); About } $ returnFundmaps; - } -}
Attached 1:json format
1 {2Keywordstype: "Product",3Keywordsdisplay: "Fund 2 2",4 array:{5 {6Keywordsinput: "Fund 2",7Weight: "10"8 },9 { TenKeywordsinput: "2", OneWeight: "12" A } - } -}
2:jar Package Download
"1" JSON jar package Download:
Http://www.sojson.com/blog/20.html
Http://www.cnblogs.com/xihuanyuye/p/5984679.html
"2" Excel jar package Download:
Http://www.cnblogs.com/wuxinrui/archive/2011/03/20/1989326.html
"Java" extracts Excel to JSON