日期:2014-05-18  浏览次数:20889 次

jxl导出excel的时候怎么把文本类型转换为数字
jxl导出excel的时候怎么把文本类型转换为数字,知道的告诉下。

------解决方案--------------------
我现在也遇到这个情况,用jxl.write.Number number = new jxl.write.Number(mm, j + 1,value,format2);可以设置为数值类型,但是我得到的value是String型的,必须先转化为数值型(int,float,double..),才能wsheet.addCell(number);这样会导致数据不一致,例如数字2364.12,导出到Excel中显示的是2364.1201171875,这样肯定是不行的。
------解决方案--------------------

//Forces numbers to be interpreted as text
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false);
DisplayFormat displayFormat = NumberFormats.TEXT;
WritableCellFormat format = new WritableCellFormat(wf,displayFormat);
format.setAlignment(jxl.format.Alignment.LEFT);
format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.NONE); 

用的时候jxl.write.Number number = new jxl.write.Number(1, 1, Double.parseDouble(value),format);
wsheet.addCell(number);就OK了。
附jxl中设置数值类型的部分源码如下:

 // The available built in number formats
  // First describe the fairly bog standard formats

  /***
   * The default format.  This is equivalent to a number format of '#'
   */
  public static final DisplayFormat DEFAULT = new BuiltInFormat(0x0, "#");
  /***
   * Formatting for an integer number.  This is equivalent to a DecimalFormat
   * of "0"
   */
  public static final DisplayFormat INTEGER = new BuiltInFormat(0x1, "0");

  /***
   * Formatting for a float.  This formats number to two decimal places.  It
   * is equivalent to a DecimalFormat of "0.00"
   */
  public static final DisplayFormat FLOAT = new BuiltInFormat(0x2, "0.00");