简体   繁体   中英

How to convert the column number format of a excel sheet in c# dynamically?

I am exporting some fields to an excel sheet in c#. All but one column are coming correctly, but in that column I need to export the value 0 to 0.00 format.

When my code behind code is running I am appending the value to a stringBuilder object as 0.00 format, but when I opened that exported document it shows the value as 0. Then I must change the column number format in the excel sheet and change the number format by selecting the type 12.3323.

I don't want to do it like this. I want to define the column number format dynamically in c# code behind file.

How can I do this? Please suggest me if you know.

I think C# does right what it does. The issue is that you need to specify the "column format" in excel in order for it to show that way. Just add the format mask.

You can use Format function to convert your value when writing into cell. Like :

Format(Convert.ToDouble(data.ToString()), "0.00")

You can use Range.NumberFormat = "0.00" (English version) or "0,00" (for the German version of Office); eg ws.get_Range("A1", "C2").NumberFormat = "0.00";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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