Poi import excel Data to object
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. using list; 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
List = new external list
(); String file = "C:/Users/lenovo/Downloads/marketing space data import template.xls "; // create a reference to the Excel workbook file HSSFWorkbook workbook = new HSSFWorkbook (new FileInputStream (file )); // In this example, reference by name (let's assume that the table has the default name "Sheet1") // HSSFSheet sheet = workbook. getSheet ("Sheet1"); HSSFSheet sheet = workbook. getSheetAt (0); // read a row // HSSFRow row = sheet. getRow (0); // it reads row Iterator
ItRow = sheet. rowIterator (); int j = 0; while (itRow. hasNext () {HSSFRow row = itRow. next (); // read row lattice Iterator
It = row. cellIterator (); int cellIndex = 0; User user = new User (); while (it. hasNext () {HSSFCell ce = it. next (); // check whether it is legal if (j = 0) {String titleName = ce. getStringCellValue (); Class clz = user. getClass (); if (! IsOk (clz, titleName) {System. out. println ("the table format does not match the imported data format! "); Return ;}} else {switch (cellIndex) {case 0: // The first Double d = ce. getNumericCellValue (); Integer id = Integer. parseInt (new DecimalFormat ("0 "). format (d); user. setId (id); break; case 1: user. setName (ce. getStringCellValue (); break; case 2: // DecimalFormat df = new DecimalFormat ("#. 00 "); // String Stringd = df. format (ce. getNumericCellValue (); user. setPrice (ce. getNumericCellValue (); break; case 3: user. setDate (c E. getDateCellValue (); break; default: break;} cellIndex ++;} if (j! = 0) {list. add (user) ;}j ++;} System. out. println ("========================================== ======== "); for (User u: list) {System. out. println (u. getId () + "\ t" + "name:" + u. getName () + "\ t" + u. getPrice () + "\ t" + u. getDate () ;}}/*** check whether the table is consistent with the object ** @ param clz * @ param titleName * @ return */public static boolean isOk (Class clz, string titleName) {boolean isExist = false; Field [] fa = clz. getDeclaredFields (); for (int I = 0; I <fa. length; I ++) {// System. out. println (fa [I]. getName (); if (titleName. equals (fa [I]. getName () {isExist = true; break ;}} return isExist ;}}/*** output: * ===================================================== ====== 1 name: stick to 1.0 Fri Oct 10 00:00:00 CST 20142 name: Small Wins 555.0 Fri Oct 10 00:00:00 CST 20143 name: helpless flowers to 66.0 Fri Oct 10 00:00:00 CST 20144 name: get 88.88 Fri Oct 10 00:00:00 CST 20145 name: Ghost dream 55.0 Fri Oct 10 00:00:00 CST 2014 ****/
// Previous example, which is easy to understand
// Read data // if (HSSFCell. CELL_TYPE_NUMERIC = ce. getCellType () {// ** in excel, the date is also a number. In this case, you need to determine * // 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());}}}