POI import Excel data to Object
Package Org.rui.xls;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import java.io.IOException ; Import Java.lang.reflect.field;import Java.text.decimalformat;import Java.util.arraylist;import Java.util.iterator;import Java.util.linkedlist;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.rui.bean.user;public class Exportxls{public static void Main (string[] args) throws FileNotFoundException, ioexception{list<user> List = new linkedlist<user> (); String file = "c:/users/lenovo/downloads/marketing spatial Data import template. xls";//create a reference to an Excel workbook file Hssfworkbook workbook = new Hssfworkbook ( New FileInputStream (file);//This example is referred to by name (let us assume that the table has the default name "Sheet1")//Hssfsheet sheet = workbook.getsheet ("Sheet1"); Hssfsheet sheet = workbook.getsheetat (0);//Read row//Hssfrow row = Sheet.getrow (0);//It reads rows iterator
Previous examples, so look better understand
Read the data//if (hssfcell.cell_type_numeric = = Ce.getcelltype ()) {/////** in Excel, the date is also a number, here to Judge *///if ( Hssfdateutil.iscelldateformatted (CE)) {//dateformat format = new SimpleDateFormat (//"Yyyy/mm/dd HH:mm:ss");// System.out.println ("Date:"//+ Format.format (Ce.getdatecellvalue ()));/} else {//system.out.println ("Numeric:"//+ Ce.getnumericcellvalue () + "");//}//} else if (hssfcell.cell_type_string = = Ce.getcelltype ()) {//system.out.println ("x : "+ ce.getstringcellvalue ());//
Package Org.rui.bean;import Java.lang.reflect.field;import Java.util.date;public class user{private Integer ID; Private String name;private Double price;private Date date;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Double GetPrice () {return price;} public void Setprice (Double price) {this.price = Price;} Public Date getDate () {return date;} public void SetDate (date date) {this.date = date;} public static void Main (string[] args) {Class clz = User.class; field[] fa = Clz.getdeclaredfields (); for (int i = 0; i < fa.length; i++) {System.out.println (Fa[i].getname ());}}}
POI import Excel data to Object