简体   繁体   English

设定格式

[英]Aspose Formatting

What is the correct style property to use to format numbers in aspose(using C#). 在aspose中使用数字格式化格式的正确样式属性是什么(使用C#)。 I would like to do 2 things: 我想做两件事:

1) Format a five digit number as a zip code.(I'm not quite sure which Style property to use to get the custom excel zipcode format) 1)将五位数的数字设置为邮政编码。(我不太确定要使用哪种Style属性来获取自定义的excel邮政编码格式)

2) Format a number(double) so that it does not have any commas and only has 2 trailing decimal points. 2)格式化数字(双精度),使其没有任何逗号,并且只有2个尾随的小数点。 I've tried using "###0.00" as the custom style but it does not seem to be working. 我尝试使用“ ### 0.00”作为自定义样式,但它似乎无法正常工作。

Any help would be much appreciated. 任何帮助将非常感激。

Zip Code Code: 邮递区号:

    //zipcode code        
    Style zipcodeStyle = targetCells[1, 1].GetStyle();
    zipcodeStyle.Custom = "0####";
    targetCells[rowindex - 20, 16].PutValue("01234");//test zipcode
    targetCells[rowindex - 20, 16].SetStyle(zipcodeStyle);

Resulting Excel Value: 1234 结果Excel值:1234

Number Code: 编号代码:

    targetCells[rowindex - 20, 45].PutValue("1234.56");
    Style style = targetWs.Cells[rowindex - 20, 45].GetStyle();
    style.Custom = "###0.00";
    targetCells[rowindex - 20, 45].SetStyle(style);
    targetCells[rowindex - 20, 45].Copy(sourceCells[rowindex, 26]);
    //test value: 140,366.75

Resulting Excel Value: 140,366.75 结果Excel值:140,366.75

Figured it out. 弄清楚了。 you have to format the string data as text. 您必须将字符串数据格式化为文本。 The data from the source cells must be put inside the text formula. 来自源单元格的数据必须放在文本公式内。 For the zip code it should be: 邮政编码应该为:

=text(datavalue, "00000")  

All US zipcodes are 5-digits long so the leading zero from the above example will be preserved. 所有美国邮政编码均为5位数字,因此将保留上述示例中的前导零。 As for the number formatting, it also will be changed to text in order to preserve trailing zeros. 至于数字格式,它也将更改为文本,以保留尾随零。 For the number format, it should be: 对于数字格式,应为:

=text(datavalue, ".00")

The data value above needs to be purged of commas before you can use this method however. 但是,在使用此方法之前,必须清除逗号以上的数据值。 The result will be placed in a cell and you should be able to perform math operations on it as well. 结果将被放置在一个单元格中,您也应该能够对其执行数学运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM