Java reads Excel into JSON string and then translates to Java object

Source: Internet
Author: User

Jar Package

Excel data

Java code
 PackageExceltojson;ImportJXL. Cell;ImportJXL. Sheet;ImportJXL. Workbook;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;ImportJava.io.File; Public classExcel { Public Static voidMain (string[] args) {Sheet Sheet;        Workbook Book;        Cell cell1, Cell2, Cell3, Cell4, Cell5,cell6,cell7,cell8; Jsonarray Array=NewJsonarray (); Try {            //is the Excel file name to readBook = Workbook.getworkbook (NewFile ("F://a.xls")); //get the first sheet object (sheet's number in Ecxel starts with 0, 0,1,2,3,....)Sheet = book.getsheet (0);  for(inti = 1; I < sheet.getrows (); i++) {                //get the cells for each rowCell1 = Sheet.getcell (0, I);//(column, row)Cell2 = Sheet.getcell (1, i); Cell3= Sheet.getcell (2, i); CELL4= Sheet.getcell (3, i); CELL5= Sheet.getcell (4, i); CELL5= Sheet.getcell (5, i); CELL5= Sheet.getcell (6, i); CELL5= Sheet.getcell (7, i); if("". Equals (Cell1.getcontents ())) {//if the data being read is empty                     Break; } Jsonobject Object=NewJsonobject (); Object.put ("The problem is dry", Cell1.getcontents ()); Object.put (Type, Cell2.getcontents ()); Object.put ("A option", Cell3.getcontents ()); Object.put ("B Option", Cell4.getcontents ()); Object.put ("C Option", Cell5.getcontents ()); Object.put ("D option", Cell5.getcontents ()); Object.put (Answer, Cell5.getcontents ()); Object.put (Resolution, Cell5.getcontents ());            Array.add (object);            } System.out.println (Array.tostring ());        Book.close (); } Catch(Exception e) {e.printstacktrace (); }    }}

[{"Dry": "1 Questions dry", "Type:" select "," A Option ":" Low voltage distribution cabinet "," b option ":" Production "," C Option ":" Low voltage distribution Cabinet "," D option ":" Low voltage distribution Cabinet "," Answer ":" Low voltage distribution Cabinet "," Resolution ":" Low voltage distribution Cabinet "},{ "Dry": "2 questions dry", "Type:" select "," A Option ":" Low voltage distribution cabinet "," b option ":" Production "," C Option ":" Low voltage distribution Cabinet "," D option ":" Low voltage distribution Cabinet "," Answer ":" Low voltage distribution Cabinet "," Resolution ":" Low voltage distribution Cabinet "},{" Dry ":" 3 questions dry "," Type: "select", "A Option": "Low voltage distribution cabinet", "b option": "Outsourcing", "C Option": "Low voltage distribution Cabinet", "D option": "Low voltage distribution Cabinet", "Answer": "Low voltage distribution Cabinet", "Resolution": "Low voltage Distribution Cabinet"}]

Java reads Excel into JSON string and then translates to Java object

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.