JXL读取Excel精度问题

前段时间项目有需要要求读取excel中的数据,我就想到了用jxl类库来实现,jxl用起来挺简单的,原来也用过,我取值的时候是这样写的

sheet.getCell(17,i).getContents()

在根据得到的值来转换为各种类型,但是我有几个表格中的数据值是小数点后五位,怎么取都得到的是三位小数,网上找了一下资料说,excel的表格设置为文本框,还是一样的效果,没办法就看了下jxl这个以部分的源码

/**
   * The number format
   */
  private NumberFormat format;

  /**
   * The string format for the double value
   */
  private static final DecimalFormat defaultFormat =
    new DecimalFormat("#.###");
/**
   * Constructs this object from the raw data
   *
   * @param t the raw data
   * @param fr the available formats
   * @param si the sheet
   */
  public NumberRecord(Record t, FormattingRecords fr, SheetImpl si)
  {
    super(t, fr, si);
    byte[] data = getRecord().getData();

    value = DoubleHelper.getIEEEDouble(data, 6);

    // Now get the number format
    format = fr.getNumberFormat(getXFIndex());
    if (format == null)
    {
      format = defaultFormat;
    }
  }

  /**
   * Accessor for the value
   *
   * @return the value
   */
  public double getValue()
  {
    return value;
  }
/**
   * Returns the contents of this cell as a string
   *
   * @return the value formatted into a string
   */
  public String getContents()
  {
    return format.format(value);
  }



在源码中你会看到对于double类型的数据,当你用getContents()这个方法的时候,jxl会默认解析为三位小数的数据,找到原因就好解决了

NumberCell cell = (NumberCell) zyysSheet.getCell(18,i);
				fzwz = cell.getValue();
我们可以把对于是数据类型的数据强转为NumberCell的其实也就是NumberRecord这个类,然后在去直接取值就好了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值