jxl导出excel的时候怎么把文本类型转换为数字

傅小司 2012-12-10 02:59:17
jxl导出excel的时候怎么把文本类型转换为数字,知道的告诉下。
...全文
1495 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nijinlin666 2013-09-23
  • 打赏
  • 举报
回复
不错啊。。。学习了
MrSoda 2013-01-30
  • 打赏
  • 举报
回复
感谢楼上柯南君!
colan0320 2012-12-28
  • 打赏
  • 举报
回复

//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");

  /***
   * Formatting for an integer that has a thousands separator.
   * Equivalent to a DecimalFormat of "#,##0"
   */
  public static final DisplayFormat THOUSANDS_INTEGER =
                                       new BuiltInFormat(0x3, "#,##0");

  /***
   * Formatting for a float that has a thousands separator.
   * Equivalent to a DecimalFormat of "#,##0.00"
   */
  public static final DisplayFormat THOUSANDS_FLOAT =
                                        new BuiltInFormat(0x4, "#,##0.00");

  /***
   * Formatting for an integer which is presented in accounting format
   * (ie. deficits appear in parentheses)
   * Equivalent to a DecimalFormat of "$#,##0;($#,##0)"
   */
  public static final DisplayFormat ACCOUNTING_INTEGER =
                                   new BuiltInFormat(0x5, "$#,##0;($#,##0)");

  /***
   * As ACCOUNTING_INTEGER except that deficits appear coloured red
   */
  public static final DisplayFormat ACCOUNTING_RED_INTEGER =
                                    new BuiltInFormat(0x6, "$#,##0;($#,##0)");

  /***
   * Formatting for an integer which is presented in accounting format
   * (ie. deficits appear in parentheses)
   * Equivalent to a DecimalFormat of  "$#,##0;($#,##0)"
   */
  public static final DisplayFormat ACCOUNTING_FLOAT =
                                     new BuiltInFormat(0x7, "$#,##0;($#,##0)");

  /***
   * As ACCOUNTING_FLOAT except that deficits appear coloured red
   */
  public static final DisplayFormat ACCOUNTING_RED_FLOAT =
                                   new BuiltInFormat(0x8, "$#,##0;($#,##0)");

  /***
   * Formatting for an integer presented as a percentage
   * Equivalent to a DecimalFormat of "0%"
   */
  public static final DisplayFormat PERCENT_INTEGER =
    new BuiltInFormat(0x9, "0%");

  /***
   * Formatting for a float percentage
   * Equivalent to a DecimalFormat "0.00%"
   */
  public static final DisplayFormat PERCENT_FLOAT =
    new BuiltInFormat(0xa, "0.00%");

  /***
   * Formatting for exponential or scientific notation
   * Equivalent to a DecimalFormat "0.00E00"
   */
  public static final DisplayFormat EXPONENTIAL =
    new BuiltInFormat(0xb, "0.00E00");

  /*** 
   * Formatting for one digit fractions
   */
  public static final DisplayFormat FRACTION_ONE_DIGIT =
    new BuiltInFormat(0xc,"?/?");

  /*** 
   * Formatting for two digit fractions
   */
  public static final DisplayFormat FRACTION_TWO_DIGITS =
    new BuiltInFormat(0xd,"??/??");

  // Now describe the more obscure formats

  /***
   * Equivalent to a DecimalFormat "#,##0;(#,##0)"
   */
  public static final DisplayFormat FORMAT1 =
                                      new BuiltInFormat(0x25, "#,##0;(#,##0)");

  /***
   * Equivalent to FORMAT1 except deficits are coloured red
   */
  public static final DisplayFormat FORMAT2 =
                                  new BuiltInFormat(0x26, "#,##0;(#,##0)");

  /***
   * Equivalent to DecimalFormat "#,##0.00;(#,##0.00)"
   */
  public static final DisplayFormat FORMAT3 =
                               new BuiltInFormat(0x27, "#,##0.00;(#,##0.00)");

  /***
   * Equivalent to FORMAT3 except deficits are coloured red
   */
  public static final DisplayFormat FORMAT4 =
                                new BuiltInFormat(0x28, "#,##0.00;(#,##0.00)");

  /***
   * Equivalent to DecimalFormat "#,##0;(#,##0)"
   */
  public static final DisplayFormat FORMAT5 =
                                    new BuiltInFormat(0x29, "#,##0;(#,##0)");

  /***
   * Equivalent to FORMAT5 except deficits are coloured red
   */
  public static final DisplayFormat FORMAT6 =
                                   new BuiltInFormat(0x2a, "#,##0;(#,##0)");

  /***
   * Equivalent to DecimalFormat "#,##0.00;(#,##0.00)"
   */
  public static final DisplayFormat FORMAT7 =
                               new BuiltInFormat(0x2b, "#,##0.00;(#,##0.00)");

  /***
   * Equivalent to FORMAT7 except deficits are coloured red
   */
  public static final DisplayFormat FORMAT8 =
                                 new BuiltInFormat(0x2c, "#,##0.00;(#,##0.00)");

  /***
   * Equivalent to FORMAT7
   */
  public static final DisplayFormat FORMAT9 =
                                new BuiltInFormat(0x2e, "#,##0.00;(#,##0.00)");

  /***
   * Equivalent to DecimalFormat "##0.0E0"
   */
  public static final DisplayFormat FORMAT10 =
    new BuiltInFormat(0x30, "##0.0E0");

  /***
   * Forces numbers to be interpreted as text
   */
  public static final DisplayFormat TEXT = new BuiltInFormat(0x31, "@");
colan0320 2012-12-27
  • 打赏
  • 举报
回复
我现在也遇到这个情况,用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,这样肯定是不行的。
meichen8050753 2012-12-27
  • 打赏
  • 举报
回复
sell.setCellType(Cell.StringValue); 好像是这个
郡麟天下 2012-12-10
  • 打赏
  • 举报
回复
把excel魔板相应列设置为数字类型最简单实用。
傅小司 2012-12-10
  • 打赏
  • 举报
回复
引用 1 楼 anybyb 的回复:
既然是导出:加入是字符串的123 倒出来显示就是123吧转他做什么!或者用java的一些方法来转吧
导出excel成数字格式,做计算的时候很方便
阿诺 2012-12-10
  • 打赏
  • 举报
回复
既然是导出:加入是字符串的123 倒出来显示就是123吧转他做什么!或者用java的一些方法来转吧

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧