"Java" extracts Excel to JSON

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.