poi 导入excel 数据转对象
来源:互联网 发布:软件售后服务条款 编辑:程序博客网 时间:2024/06/05 04:50
poi 导入excel 数据转对象
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/营销空间数据导入模板.xls";// 创建对Excel工作簿文件的引用HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));// 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")// HSSFSheet sheet = workbook.getSheet("Sheet1");HSSFSheet sheet = workbook.getSheetAt(0);// 读取一行// HSSFRow row = sheet.getRow(0);// it读取行Iterator<HSSFRow> itRow = sheet.rowIterator();int j = 0;while (itRow.hasNext()) {HSSFRow row = itRow.next();// 读行格Iterator<HSSFCell> it = row.cellIterator();int cellIndex = 0;User user = new User();while (it.hasNext()) {HSSFCell ce = it.next();// 检查是否合法if (j == 0) {String titleName = ce.getStringCellValue();Class clz = user.getClass();if (!isOk(clz, titleName)) {System.out.println("表格格式不符合导入的数据格式!");return;}} else {switch (cellIndex){case 0:// 第一格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(ce.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());}}/** * 检查表格是否和对象一致 * * @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:粘地 1.0 Fri Oct 10 00:00:00 CST 20142 name:小夺 555.0 Fri Oct 10 00:00:00 CST 20143 name:无可奈何花落去 66.0 Fri Oct 10 00:00:00 CST 20144 name:夺 88.88 Fri Oct 10 00:00:00 CST 20145 name:魂牵梦萦 55.0 Fri Oct 10 00:00:00 CST 2014 * ***/
//之前例子,这样看比较好理解
// 读取数据//if (HSSFCell.CELL_TYPE_NUMERIC == ce.getCellType()) {/////** 在excel里,日期也是数字,在此要进行判断 *///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());}}}
1 0
- poi 导入excel 数据转对象
- poi 导入excel 数据转对象
- POI数据导入Excel
- POI 导入excel数据自动封装成model对象--介绍
- POI 导入excel数据自动封装成model对象--代码
- 使用poi导入excel数据
- 利用POI将数据库数据导入Excel
- 利用poi把excel数据导入mysql
- 使用POI导入Excel数据到系统
- 使用Java POI导入导出Excel数据
- poi sax 方式 导入excel海量数据
- struts2中使用poi导入excel数据
- poi sax 方式 导入excel海量数据
- POI导入Excel并处理数据
- POI导入excel表内数据
- 使用POI导入Excel文件数据
- Apache POI实现数据的Excel导入
- SpringMvc+POI处理excel表数据导入
- 《年轻可以一无所有》读书笔记 (一)
- 国内银行结算账户类型
- MAC下安装git
- Compare Numbers
- 黑马程序员-JAVASE入门(数组)
- poi 导入excel 数据转对象
- 2015-4-7C语言学习日记
- java多线程——同步的前提
- java jar文件的深入认识
- Sicily 14261. Generating Words
- 控制器常见的创建方式
- java 可运行jar的设置介绍
- unity的协程的用途
- pandas 做图显示中文标签