Apache POI HSSF 解析Excel时的细节.

1. POI解析的时候比较吃内存. 因为一旦解析,估计就是整个workbook load的内存.
    /**
     * given a POI POIFSFileSystem object, read in its Workbook and populate the high and
     * low level models.  If you're reading in a workbook...start here.
     *
     * @param fs the POI filesystem that contains the Workbook stream.
     * @param preserveNodes whether to preseve other nodes, such as
     *        macros.  This takes more memory, so only say yes if you
     *        need to. If set, will store all of the POIFSFileSystem
     *        in memory
     * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
     * @exception IOException if the stream cannot be read
     */
    public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)
            throws IOException
    {
        this(fs.getRoot(), fs, preserveNodes);
    }
set preserveNodes = false可能会节省点.

2. HSSFCell的值有这些类型
    /** Numeric Cell type (0) @see #setCellType(int) @see #getCellType() */
    public final static int CELL_TYPE_NUMERIC = 0;
    /** String  Cell type (1) @see #setCellType(int) @see #getCellType() */
    public final static int CELL_TYPE_STRING  = 1;
    /** Formula Cell type (2) @see #setCellType(int) @see #getCellType() */
    public final static int CELL_TYPE_FORMULA = 2;
    /** Blank   Cell type (3) @see #setCellType(int) @see #getCellType() */
    public final static int CELL_TYPE_BLANK   = 3;
    /** Boolean Cell type (4) @see #setCellType(int) @see #getCellType() */
    public final static int CELL_TYPE_BOOLEAN = 4;
    /** Error   Cell type (5) @see #setCellType(int) @see #getCellType() */
    public final static int CELL_TYPE_ERROR   = 5;

其实不得不说一下的就是Number, 它不管你是Integer还是浮点,返回的都是double.
而且把Date数据类型会组成一个奇怪的数字. 需要而外判断

if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            double dValue = cell.getNumericCellValue();
            if ( HSSFDateUtil.isCellDateFormatted(cell)) { //日期
                return DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(cell.getDateCellValue());
            }
            else if (this.doubleIsInteger(dValue)) {
                return String.valueOf((int) dValue);
            }
            else {
                return String.valueOf(cell.getNumericCellValue());
            }
        }

3. 从HSSFSheet拿出来的HSSFRow, 从HSSFRow拿出来的HSSFCell都有可能是Null, 注意判断.
4. 用户编辑很多Excel的时候会拉伸出很多空的行和列,注意过滤.

能免费使用都蛮好了的, Excel的内容乱七八糟的时候POI解析容易抛异常,旧的包有不少的bug, 最新release3.2要稳定些.
有bug就到apache的bugzilla记下吧,
.....


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值