poi 导入excel 数据转对象


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package org.rui.xls;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.lang.reflect.Field;  
  7. import java.text.DecimalFormat;  
  8. import java.util.ArrayList;  
  9. import java.util.Iterator;  
  10. import java.util.LinkedList;  
  11. import java.util.List;  
  12.   
  13. import org.apache.poi.hssf.usermodel.HSSFCell;  
  14. import org.apache.poi.hssf.usermodel.HSSFRow;  
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  17. import org.rui.bean.User;  
  18.   
  19. public class ExportXls  
  20. {  
  21.     public static void main(String[] args) throws FileNotFoundException,  
  22.             IOException  
  23.     {  
  24.   
  25.         List<User> list = new LinkedList<User>();  
  26.   
  27.         String file = "C:/Users/lenovo/Downloads/营销空间数据导入模板.xls";  
  28.         // 创建对Excel工作簿文件的引用  
  29.         HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));  
  30.         // 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")  
  31.         // HSSFSheet sheet = workbook.getSheet("Sheet1");  
  32.         HSSFSheet sheet = workbook.getSheetAt(0);  
  33.         // 读取一行  
  34.         // HSSFRow row = sheet.getRow(0);  
  35.         // it读取行  
  36.         Iterator<HSSFRow> itRow = sheet.rowIterator();  
  37.           
  38.         int j = 0;  
  39.         while (itRow.hasNext()) {  
  40.             HSSFRow row = itRow.next();  
  41.             // 读行格  
  42.             Iterator<HSSFCell> it = row.cellIterator();  
  43.             int cellIndex = 0;  
  44.             User user = new User();  
  45.             while (it.hasNext()) {  
  46.                 HSSFCell ce = it.next();  
  47.                 // 检查是否合法  
  48.                 if (j == 0) {  
  49.                     String titleName = ce.getStringCellValue();  
  50.                     Class clz = user.getClass();  
  51.                     if (!isOk(clz, titleName)) {  
  52.                         System.out.println("表格格式不符合导入的数据格式!");  
  53.                         return;  
  54.                     }  
  55.   
  56.                 } else {  
  57.   
  58.                     switch (cellIndex)  
  59.                     {  
  60.                     case 0:// 第一格  
  61.                         Double d = ce.getNumericCellValue();  
  62.                         Integer id = Integer.parseInt(new DecimalFormat("0")  
  63.                                 .format(d));  
  64.                         user.setId(id);  
  65.                         break;  
  66.                     case 1:  
  67.                         user.setName(ce.getStringCellValue());  
  68.                         break;  
  69.                     case 2:  
  70.                         // DecimalFormat df = new DecimalFormat("#.00");  
  71.                         // String Stringd = df.format(ce.getNumericCellValue());  
  72.                         user.setPrice(ce.getNumericCellValue());  
  73.                         break;  
  74.                     case 3:  
  75.                         user.setDate(ce.getDateCellValue());  
  76.                         break;  
  77.                     default:  
  78.                         break;  
  79.                     }  
  80.   
  81.                 }  
  82.                 cellIndex++;  
  83.   
  84.             }  
  85.             if (j != 0) {  
  86.                 list.add(user);  
  87.             }  
  88.   
  89.             j++;  
  90.         }  
  91.   
  92.         System.out.println("=============================================");  
  93.         for (User u : list) {  
  94.             System.out.println(u.getId() + " \t " + "name:" + u.getName()  
  95.                     + " \t " + u.getPrice() + " \t " + u.getDate());  
  96.         }  
  97.     }  
  98.   
  99.     /** 
  100.      * 检查表格是否和对象一致 
  101.      *  
  102.      * @param clz 
  103.      * @param titleName 
  104.      * @return 
  105.      */  
  106.     public static boolean isOk(Class clz, String titleName)  
  107.     {  
  108.         boolean isExist = false;  
  109.         Field[] fa = clz.getDeclaredFields();  
  110.         for (int i = 0; i < fa.length; i++) {  
  111.             // System.out.println(fa[i].getName());  
  112.             if (titleName.equals(fa[i].getName())) {  
  113.                 isExist = true;  
  114.                 break;  
  115.             }  
  116.         }  
  117.   
  118.         return isExist;  
  119.     }  
  120. }  
  121. /** 
  122.  * output: 
  123.  * ============================================= 
  124. 1    name:粘地     1.0     Fri Oct 10 00:00:00 CST 2014 
  125. 2    name:小夺     555.0   Fri Oct 10 00:00:00 CST 2014 
  126. 3    name:无可奈何花落去    66.0    Fri Oct 10 00:00:00 CST 2014 
  127. 4    name:夺      88.88   Fri Oct 10 00:00:00 CST 2014 
  128. 5    name:魂牵梦萦       55.0    Fri Oct 10 00:00:00 CST 2014 
  129.  * ***/  


//之前例子,这样看比较好理解

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.                     // 读取数据  
  2. //                  if (HSSFCell.CELL_TYPE_NUMERIC == ce.getCellType()) {  
  3. //  
  4. //                      /** 在excel里,日期也是数字,在此要进行判断 */  
  5. //                      if (HSSFDateUtil.isCellDateFormatted(ce)) {  
  6. //                          DateFormat format = new SimpleDateFormat(  
  7. //                                  "yyyy/MM/dd HH:mm:ss");  
  8. //                          System.out.println("date:"  
  9. //                                  + format.format(ce.getDateCellValue()));  
  10. //                      } else {  
  11. //                          System.out.println("numeric:"  
  12. //                                  + ce.getNumericCellValue() + "");  
  13. //                      }  
  14. //                  } else if (HSSFCell.CELL_TYPE_STRING == ce.getCellType()) {  
  15. //                      System.out.println("x:" + ce.getStringCellValue());  
  16. //        



[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package org.rui.bean;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.util.Date;  
  5.   
  6. public class User  
  7. {  
  8.   
  9.     private Integer id;  
  10.     private String name;  
  11.     private Double price;  
  12.     private Date date;  
  13.       
  14.   
  15.     public Integer getId()  
  16.     {  
  17.         return id;  
  18.     }  
  19.   
  20.     public void setId(Integer id)  
  21.     {  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public String getName()  
  26.     {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setName(String name)  
  31.     {  
  32.         this.name = name;  
  33.     }  
  34.   
  35.     public Double getPrice()  
  36.     {  
  37.         return price;  
  38.     }  
  39.   
  40.     public void setPrice(Double price)  
  41.     {  
  42.         this.price = price;  
  43.     }  
  44.       
  45.       
  46.   
  47.     public Date getDate()  
  48.     {  
  49.         return date;  
  50.     }  
  51.   
  52.     public void setDate(Date date)  
  53.     {  
  54.         this.date = date;  
  55.     }  
  56.   
  57.     public static void main(String[] args)  
  58.     {  
  59.         Class clz = User.class;  
  60.   
  61.         Field[] fa = clz.getDeclaredFields();  
  62.   
  63.         for (int i = 0; i < fa.length; i++) {  
  64.             System.out.println(fa[i].getName());  
  65.         }  
  66.   
  67.     }  
  68.   
  69. }  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值