質問

What is the correct style property to use to format numbers in aspose(using 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)

2) Format a number(double) so that it does not have any commas and only has 2 trailing decimal points. I've tried using "###0.00" as the custom style but it does not seem to be working.

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

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

役に立ちましたか?

解決

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. 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帰属
所属していません StackOverflow
scroll top