The jar to be introduced
<!--*.xls--> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.8</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactid>fastjson</ artifactid> <version>1.2.7</version> </dependency>
Test class
PackageCom.taobao.tair;ImportCom.alibaba.fastjson.JSONArray;ImportCom.alibaba.fastjson.JSONObject;ImportJXL. Cell;ImportJXL. Sheet;ImportJXL. Workbook;ImportJava.io.File; Public classExcel { Public Static voidMain (string[] args) {Sheet Sheet; Workbook Book; Cell cell1, Cell2, Cell3, Cell4, CELL5; Jsonarray Array=NewJsonarray (); Try { //is the Excel file name to readBook = Workbook.getworkbook (NewFile ("D://b.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); if("". Equals (Cell1.getcontents ())) {//if the data being read is empty Break; } Jsonobject Object=NewJsonobject (); Object.put ("ID", Cell1.getcontents ()); Object.put (Id, Cell2.getcontents ()); Object.put (Name, Cell3.getcontents ()); Object.put (Number, Cell4.getcontents ()); Object.put (Address, Cell5.getcontents ()); Array.add (object); } System.out.println (Array.tostring ()); Book.close (); } Catch(Exception e) {e.printstacktrace (); } }}
B.xls file:
Using Java code to read the Excel file code times the error is as follows:
Jxl.read.biff.BiffException:Unable to recognize OLE stream
At Jxl.read.biff.compoundfile.<init> (compoundfile.java:116)
At Jxl.read.biff.file.<init> (file.java:127)
At JXL. Workbook.getworkbook (workbook.java:268)
At JXL. Workbook.getworkbook (workbook.java:253)
Cause: Do not spend reading Excel 2007 files (*.xlsx). Only Excel 2003 (*.xls) is supported.
Converting B.xls to Version 2003 is possible.
[{"Name": "Zhang San", "Quantity": "100", "Address": "Hangzhou city, Zhejiang province", "id": "1", "Number": "1001"},{"name": "John Doe", "Quantity": "200", "Address": "Wuhan, Hubei Province", "id": "2", "Number": " 1002 "},{" name ":" Harry "," Quantity ":" 300 "," Address ":" Guangdong Province Shenzhen "," id ":" 3 "," Number ":" 1003 "},{" name ":" Zhao Liu "," Quantity ":" 400 "," Address ":" Hunan Province Changsha "," id ":" 4 ", "Number": "1004"}]
Java reads Excel content to Jsonarray