使用POI 3.10.1读取 Excel 2007格式

防止较长数字变更为 科学计数法:

http://blog.csdn.net/z69183787/article/details/47952005


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

解析date 工具类:

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);
    }
}



package com.wonders.task.excel.util;

import com.wonders.task.excel.model.SendBo;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: zhoushun
 * Date: 2014/12/6
 * Time: 20:25
 * To change this template use File | Settings | File Templates.
 */
public class PoiUtil {
    private static String excel2007 = "D:\\all.xlsx";

    private static void setCode(SendBo bo){
        Pattern pattern = Pattern.compile("(\\S+)\\((\\d+)\\)(\\d+)号");

        String sendId = bo.getSendId();
        if(sendId == null || sendId.length() == 0){
            bo.setCode1("");bo.setCode2("");bo.setCode3("");
        }else{
            Matcher matcher = pattern.matcher(sendId);
            if(matcher.find()){
                bo.setCode1(matcher.group(1));
                bo.setCode2(matcher.group(2));
                bo.setCode3(matcher.group(3));
            }
        }
    }

    public static List<SendBo> readExcel2007(){
        List<SendBo> list = new ArrayList<SendBo>();
        try{
            File excelFile = new File(excel2007);
            FileInputStream is = new FileInputStream(excelFile);// 获取文件输入流
            XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2003文件对象
            XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0
            // 开始循环遍历行,表头不处理,从1开始
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                XSSFRow row = sheet.getRow(i);// 获取行对象
                if (row == null) {// 如果为空,不处理
                    continue;
                }
                SendBo bo = new SendBo();
                // 循环遍历单元格
                for (int j = 0; j < row.getLastCellNum(); j++) {

                    XSSFCell cell = row.getCell(j);// 获取单元格对象
                    switch(j){
                        case 0 :
                            bo.setSendDept(getStringCellValue(cell).trim());
                            break;
                        case 1 :
                            bo.setSendId(getStringCellValue(cell).trim());
                            break;
                        case 2 :
                            bo.setSendDate(getStringCellValue(cell).trim());
                            setCode(bo);
                            break;
                        case 3 :
                            bo.setTitle(getStringCellValue(cell).trim());
                            break;
                        case 4 :
                            bo.setSendMain(getStringCellValue(cell).trim());
                            break;
                        case 5 :
                            break;
                        case 6 :
                            break;
                        default:
                            break;
                    }

                }
                list.add(bo);
            }


        }catch(Exception e){}



        return list;
    }


    /**
     * 获取单元格数据内容为字符串类型的数据
     *
     * @param cell Excel单元格
     * @return String 单元格数据内容
     */
    private static String getStringCellValue(XSSFCell cell) {
        String strCell = "";
        switch (cell.getCellType()) {
            case XSSFCell.CELL_TYPE_STRING:
                strCell = cell.getStringCellValue();
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                if (XSSFDateUtil.isCellDateFormatted(cell)) {
                    //  如果是date类型则 ,获取该cell的date值
                    strCell = new SimpleDateFormat("yyyy-MM-dd").format(XSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
                } else { // 纯数字
                    strCell = String.valueOf(cell.getNumericCellValue());
                }
                    break;
            case XSSFCell.CELL_TYPE_BOOLEAN:
                strCell = String.valueOf(cell.getBooleanCellValue());
                break;
            case XSSFCell.CELL_TYPE_BLANK:
                strCell = "";
                break;
            default:
                strCell = "";
                break;
        }
        if (strCell.equals("") || strCell == null) {
            return "";
        }
        if (cell == null) {
            return "";
        }
        return strCell;
    }


    public static void main(String[] args){
        String s = "沪地铁(2014)22号";
        Pattern pattern = Pattern.compile("(\\S+)\\((\\d+)\\)(\\d+)号");
        Matcher matcher = pattern.matcher(s);
        if(matcher.find()){
            int gc = matcher.groupCount();
            for(int i = 0; i <= gc; i++)
                System.out.println("group " + i + " :" + matcher.group(i));
        }
        //readExcel2007();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值