The main story is about the POI in Java reading Excel, and the version of Excel includes: 2003-2007 and 20,102 versions, that is, Excel has the suffix name: xls and xlsx
Read Excel and MySQL Related: Java POI Technology read Excel data to MySQL
You can also read and import excel in: Java's POI technology to learn how to write Excel information
Importing Excel using JXL technology: Java's JXL technology
The following is the project structure of this article:
========================================
The jar files required in the project:
==================================
The Excel data used (2003-2007,2010 are the same data )
===========================================================================
Operating effect:
=================================================
SOURCE section:
=================================================
/excel2010/src/com/b510/common/common.java
<textarea style="width: 697px; height: 500.8px; line-height: 1.5; font-family: Courier New; font-size: 12px;">/** * */package com.b510.common;/** * @author hongten * @created 2014-5-21 */public class Common {public static F inal String office_excel_2003_postfix = "xls"; public static final String Office_excel_2010_postfix = "xlsx"; public static final String EMPTY = ""; public static final String point = "."; public static final String Lib_path = "LIB"; public static final String Student_info_xls_path = Lib_path + "/student_info" + point + office_excel_2003_postfix; public static final String Student_info_xlsx_path = Lib_path + "/student_info" + point + office_excel_2010_postfix; public static final String not_excel_file = ": Not the EXCEL file!"; public static final String processing = "processing ...";}</textarea>
==============================================
=================================================
/excel2010/src/com/b510/excel/readexcel.java
/** * */package com.b510.excel;import java.io.fileinputstream;import java.io.ioexception;import java.io.InputStream; Import Java.util.arraylist;import Java.util.list;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfrow;import Org.apache.poi.hssf.usermodel.hssfsheet;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.xssf.usermodel.xssfcell;import Org.apache.poi.xssf.usermodel.xssfrow;import Org.apache.poi.xssf.usermodel.xssfsheet;import Org.apache.poi.xssf.usermodel.xssfworkbook;import Com.b510.common.common;import Com.b510.excel.util.Util;import com.b510.excel.vo.student;/** * @author Hongten * @created 2014-5-20 */public class Readexcel {/** * Read the Excel file * @param path the path of the Excel file * @return * @throws IOException */Public List<s Tudent> readexcel (String path) throws IOException {if (path = = NULL | | Common.EMPTY.equals (path)) {return null; } ELSE {String postfix = util.getpostfix (path); if (! Common.EMPTY.equals (Postfix)) {if (Common.OFFICE_EXCEL_2003_POSTFIX.equals (postfix)) { return Readxls (path); } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals (POSTFIX)) {return readxlsx (path); }} else {System.out.println (path + common.not_excel_file); }} return null; }/** * Read the Excel * @param path The path of the Excel file * @return * @throws IOException */Public list<student> readxlsx (String path) throws IOException {System.out.println (common.processing + path); InputStream is = new FileInputStream (path); Xssfworkbook Xssfworkbook = new Xssfworkbook (IS); Student Student = null; list<student> list = new arraylist<student> (); Read the Sheet for (int numsheet = 0; numSheet < Xssfworkbook.getnumberofsheets (); numsheet++) {Xssfsheet Xssfsheet = Xssfworkbook.getsheetat (Numsheet); if (Xssfsheet = = null) {continue; }//Read the Row for (int rowNum = 1; rowNum <= xssfsheet.getlastrownum (); rownum++) { Xssfrow Xssfrow = Xssfsheet.getrow (RowNum); if (Xssfrow! = null) {student = new student (); Xssfcell no = Xssfrow.getcell (0); Xssfcell name = Xssfrow.getcell (1); Xssfcell age = Xssfrow.getcell (2); Xssfcell score = Xssfrow.getcell (3); Student.setno (GetValue (no)); Student.setname (GetValue (name)); Student.setage (GetValue (age)); Student.setscore (Float.valueof (GetValue (score))); List.add (student); }}} return list; } /** * Read the Excel 2003-2007 * @param path The path of the Excel * @return * @throws IOException */PU Blic list<student> Readxls (String path) throws IOException {System.out.println (common.processing + path); InputStream is = new FileInputStream (path); Hssfworkbook Hssfworkbook = new Hssfworkbook (IS); Student Student = null; list<student> list = new arraylist<student> (); Read the Sheet for (int numsheet = 0; Numsheet < hssfworkbook.getnumberofsheets (); numsheet++) {H Ssfsheet Hssfsheet = Hssfworkbook.getsheetat (Numsheet); if (Hssfsheet = = null) {continue; }//Read the Row for (int rowNum = 1; rowNum <= hssfsheet.getlastrownum (); rownum++) { Hssfrow Hssfrow = Hssfsheet.getrow (RowNum); if (Hssfrow! = null) {student = new student (); Hssfcell No = HSSFROW.GEtcell (0); Hssfcell name = Hssfrow.getcell (1); Hssfcell age = Hssfrow.getcell (2); Hssfcell score = Hssfrow.getcell (3); Student.setno (GetValue (no)); Student.setname (GetValue (name)); Student.setage (GetValue (age)); Student.setscore (Float.valueof (GetValue (score))); List.add (student); }}} return list; } @SuppressWarnings ("Static-access") Private String GetValue (Xssfcell xssfrow) {if (xssfrow.getcelltype () = = Xssfrow.cell_type_boolean) {return string.valueof (Xssfrow.getbooleancellvalue ()); } else if (xssfrow.getcelltype () = = Xssfrow.cell_type_numeric) {return string.valueof (Xssfrow.getnumericcellva Lue ()); } else {return string.valueof (Xssfrow.getstringcellvalue ()); }} @SuppressWarnings ("Static-access") Private String GEtvalue (Hssfcell Hssfcell) {if (hssfcell.getcelltype () = = Hssfcell.cell_type_boolean) {return STRING.V Alueof (Hssfcell.getbooleancellvalue ()); } else if (hssfcell.getcelltype () = = Hssfcell.cell_type_numeric) {return string.valueof (hssfcell.getnumericcel LValue ()); } else {return string.valueof (Hssfcell.getstringcellvalue ()); } }}
=========================
=============================================================================
/excel2010/src/com/b510/excel/client/client.java
/** * */package com.b510.excel.client;import java.io.ioexception;import java.util.list;import Com.b510.common.Common Import Com.b510.excel.readexcel;import com.b510.excel.vo.student;/** * @author hongten * @created 2014-5-21 */public Class Client {public static void main (string[] args) throws IOException {String excel2003_2007 = Common.studen T_info_xls_path; String excel2010 = Common.student_info_xlsx_path; Read the 2003-2007 Excel list<student> List = new Readexcel (). Readexcel (excel2003_2007); if (list = null) {for (Student student:list) {System.out.println ("No.:" + Student.getno () + ", Name:" + student.getname () + ", Age:" + student.getage () + ", Score:" + Student.getscore ()); }} System.out.println ("======================================"); Read the Excel list<student> List1 = new Readexcel (). Readexcel (excel2010); if (list1! = null) { for (Student student:list1) {System.out.println ("No.:" + student.getno () + ", Name:" + stu Dent.getname () + ", Age:" + student.getage () + ", Score:" + Student.getscore ()); } } }}
===============================================-----------========================
====================
/excel2010/src/com/b510/excel/util/util.java
/** * */package com.b510.excel.util;import com.b510.common.common;/** * @author hongten * @created 2014-5-21 */public Class Util {/** * get postfix of the path * @param path * @return */public static String GETPOSTF IX (String path) {if (path = = NULL | | Common.EMPTY.equals (Path.trim ())) {return common.empty; } if (Path.contains (Common.point)) {return path.substring (Path.lastindexof (common.point) + 1, Path.leng th ()); } return common.empty; }}
===============================
===============================================================================
/excel2010/src/com/b510/excel/vo/student.java
/** * */package com.b510.excel.vo;/** * Student * * @author hongten * @created 2014-5-18 */public class Student {/** * ID */private Integer ID; /** * * School Number */private String No; /** * Name * * * Private String name; /** * College */private String age; /** * Results * */private float score; Public Integer GetId () {return id; } public void SetId (Integer id) {this.id = ID; Public String Getno () {return no; public void Setno (String no) {this.no = no; } public String GetName () {return name; } public void SetName (String name) {this.name = name; } public String Getage () {return age; public void Setage (String age) {this.age = age; } public float Getscore () {return score; } public void SetScore (float score) {this.score = score; }}
==============================================================================
++++++++++++++++++++++++++++++++++++++++++++++_____________________+++++++++++++++++
Java directly calls Micorosoft office (2003-2007 and 2010 versions) in the calculation function in Excel