使用POI3读取Excel2007格式

相关jar包,自行去poi官网下载,其中包含了对于date类型的处理,其中该列必须在excel中设置为日期类型。

解析date 工具类:

  1. package com.wonders.task.excel.util;  
  2.   
  3. import org.apache.poi.ss.usermodel.DateUtil;  
  4.   
  5. import java.util.Calendar;  
  6.   
  7. /** 
  8.  * Created with IntelliJ IDEA. 
  9.  * User: zhoushun 
  10.  * Date: 2014/12/7 
  11.  * Time: 0:36 
  12.  * To change this template use File | Settings | File Templates. 
  13.  */  
  14. public class XSSFDateUtil extends DateUtil {  
  15.     protected static int absoluteDay(Calendar cal, boolean use1904windowing) {  
  16.         return DateUtil.absoluteDay(cal, use1904windowing);  
  17.     }  
  18. }  
package com.wonders.task.excel.util;

import org.apache.poi.ss.usermodel.DateUtil;

import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * User: zhoushun
 * Date: 2014/12/7
 * Time: 0:36
 * To change this template use File | Settings | File Templates.
 */
public class XSSFDateUtil extends DateUtil {
    protected static int absoluteDay(Calendar cal, boolean use1904windowing) {
        return DateUtil.absoluteDay(cal, use1904windowing);
    }
}



  1. package com.wonders.task.excel.util;  
  2.   
  3. import com.wonders.task.excel.model.SendBo;  
  4. import org.apache.poi.xssf.usermodel.XSSFCell;  
  5. import org.apache.poi.xssf.usermodel.XSSFRow;  
  6. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  7. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  8.   
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.text.SimpleDateFormat;  
  12. import java.util.ArrayList;  
  13. import java.util.List;  
  14. import java.util.regex.Matcher;  
  15. import java.util.regex.Pattern;  
  16.   
  17. /** 
  18.  * Created with IntelliJ IDEA. 
  19.  * User: zhoushun 
  20.  * Date: 2014/12/6 
  21.  * Time: 20:25 
  22.  * To change this template use File | Settings | File Templates. 
  23.  */  
  24. public class PoiUtil {  
  25.     private static String excel2007 = "D:\\all.xlsx";  
  26.   
  27.     private static void setCode(SendBo bo){  
  28.         Pattern pattern = Pattern.compile("(\\S+)\\((\\d+)\\)(\\d+)号");  
  29.   
  30.         String sendId = bo.getSendId();  
  31.         if(sendId == null || sendId.length() == 0){  
  32.             bo.setCode1("");bo.setCode2("");bo.setCode3("");  
  33.         }else{  
  34.             Matcher matcher = pattern.matcher(sendId);  
  35.             if(matcher.find()){  
  36.                 bo.setCode1(matcher.group(1));  
  37.                 bo.setCode2(matcher.group(2));  
  38.                 bo.setCode3(matcher.group(3));  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     public static List<SendBo> readExcel2007(){  
  44.         List<SendBo> list = new ArrayList<SendBo>();  
  45.         try{  
  46.             File excelFile = new File(excel2007);  
  47.             FileInputStream is = new FileInputStream(excelFile);// 获取文件输入流  
  48.             XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2003文件对象  
  49.             XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0  
  50.             // 开始循环遍历行,表头不处理,从1开始  
  51.             for (int i = 1; i <= sheet.getLastRowNum(); i++) {  
  52.                 XSSFRow row = sheet.getRow(i);// 获取行对象  
  53.                 if (row == null) {// 如果为空,不处理  
  54.                     continue;  
  55.                 }  
  56.                 SendBo bo = new SendBo();  
  57.                 // 循环遍历单元格  
  58.                 for (int j = 0; j < row.getLastCellNum(); j++) {  
  59.   
  60.                     XSSFCell cell = row.getCell(j);// 获取单元格对象  
  61.                     switch(j){  
  62.                         case 0 :  
  63.                             bo.setSendDept(getStringCellValue(cell).trim());  
  64.                             break;  
  65.                         case 1 :  
  66.                             bo.setSendId(getStringCellValue(cell).trim());  
  67.                             break;  
  68.                         case 2 :  
  69.                             bo.setSendDate(getStringCellValue(cell).trim());  
  70.                             setCode(bo);  
  71.                             break;  
  72.                         case 3 :  
  73.                             bo.setTitle(getStringCellValue(cell).trim());  
  74.                             break;  
  75.                         case 4 :  
  76.                             bo.setSendMain(getStringCellValue(cell).trim());  
  77.                             break;  
  78.                         case 5 :  
  79.                             break;  
  80.                         case 6 :  
  81.                             break;  
  82.                         default:  
  83.                             break;  
  84.                     }  
  85.   
  86.                 }  
  87.                 list.add(bo);  
  88.             }  
  89.   
  90.   
  91.         }catch(Exception e){}  
  92.   
  93.   
  94.   
  95.         return list;  
  96.     }  
  97.   
  98.   
  99.     /** 
  100.      * 获取单元格数据内容为字符串类型的数据 
  101.      * 
  102.      * @param cell Excel单元格 
  103.      * @return String 单元格数据内容 
  104.      */  
  105.     private static String getStringCellValue(XSSFCell cell) {  
  106.         String strCell = "";  
  107.         switch (cell.getCellType()) {  
  108.             case XSSFCell.CELL_TYPE_STRING:  
  109.                 strCell = cell.getStringCellValue();  
  110.                 break;  
  111.             case XSSFCell.CELL_TYPE_NUMERIC:  
  112.                 if (XSSFDateUtil.isCellDateFormatted(cell)) {  
  113.                     //  如果是date类型则 ,获取该cell的date值  
  114.                     strCell = new SimpleDateFormat("yyyy-MM-dd").format(XSSFDateUtil.getJavaDate(cell.getNumericCellValue()));  
  115.                 } else { // 纯数字  
  116.                     strCell = String.valueOf(cell.getNumericCellValue());  
  117.                 }  
  118.                     break;  
  119.             case XSSFCell.CELL_TYPE_BOOLEAN:  
  120.                 strCell = String.valueOf(cell.getBooleanCellValue());  
  121.                 break;  
  122.             case XSSFCell.CELL_TYPE_BLANK:  
  123.                 strCell = "";  
  124.                 break;  
  125.             default:  
  126.                 strCell = "";  
  127.                 break;  
  128.         }  
  129.         if (strCell.equals("") || strCell == null) {  
  130.             return "";  
  131.         }  
  132.         if (cell == null) {  
  133.             return "";  
  134.         }  
  135.         return strCell;  
  136.     }  
  137.   
  138.   
  139.     public static void main(String[] args){  
  140.         String s = "沪地铁(2014)22号";  
  141.         Pattern pattern = Pattern.compile("(\\S+)\\((\\d+)\\)(\\d+)号");  
  142.         Matcher matcher = pattern.matcher(s);  
  143.         if(matcher.find()){  
  144.             int gc = matcher.groupCount();  
  145.             for(int i = 0; i <= gc; i++)  
  146.                 System.out.println("group " + i + " :" + matcher.group(i));  
  147.         }  
  148.         //readExcel2007();  
  149.     }  
  150. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值