有关 poi 读 excel 中的一些问题

来源:互联网 发布:linux shell cut 编辑:程序博客网 时间:2024/05/22 04:34

有关 poi 读 excel 中的一些问题 :

读取 excel  2003-2007 和 2007以后版本 兼容问题 解决单元格日期格式读取 处理方式 

读取 excel 2003-2007 和 2007以后版本 兼容问题 解决

目前操作方式如下:

try {        org.apache.poi.hssf.record.crypto.Biff8EncryptionKey        .setCurrentUserPassword(password);        inp = new FileInputStream(path); //这个地方必须new一个新的         否则出现exception 流就会被强制关闭        workbook = WorkbookFactory.create(inp);    }catch(Exception e){        inp = new FileInputStream(path);//这个地方必须new一个新的         否则出现exception 流就会被强制关闭        POIFSFileSystem pfs = new POIFSFileSystem(inp);        EncryptionInfo encInfo = new EncryptionInfo(pfs);        Decryptor decryptor = Decryptor.getInstance(encInfo);        decryptor.verifyPassword(password);    workbook = new XSSFWorkbook(decryptor.getDataStream(pfs));    }finally{        if(inp != null){        inp.close();        }    }

单元格日期格式读取 处理方式 :

处理如下:

1 处理XSSFCell

if (DateUtil.isCellDateFormatted(cell)) { //Excel Date类型处理    Date date = DateUtil.getJavaDate(cell.getNumericCellValue());    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    return sdf.format(date);}else{    cell.setCellType(1);    return cell.getStringCellValue();}

2 处理HSSFCell

if (HSSFDateUtil.isCellDateFormatted(cell)) { //Excel Date类型处理    Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    return sdf.format(date);}else{    cell.setCellType(1);    return cell.getStringCellValue();}

后续再做更新 以上为本人实践 如有不当之处 欢迎指正 欢迎交流!

0 0