POI import Excel data to Object

Source: Internet
Author: User

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

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.