We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to format number?

Sorry for the newbie question, as I am still trying to learn your great Aspose Cells product...

The following code gives the first attached picture.
If I double-click that cell, it becomes what the second picture looks like, which is what I want.

Could you help to comment what to do to get the number format as shown in the second picture ? Many thanks !

using System.IO;
using Aspose.Cells;

public class Program
{
public static void Main(string[] args)
{
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

worksheet.Cells.SetColumnWidth(0, 30);
Cell cell = worksheet.Cells["A1"];
cell.PutValue("-0.123456");
Style stl = cell.GetStyle();
// stl.Number = 2;
stl.Custom = "0.000000";
// stl.CultureCustom = "0.000000";
cell.SetStyle(stl);

workbook.Save("book1.xls");
}
}

Hi Jiaguo,

Thanks for your posting and using Aspose.Cells.

Actually you are entering string in cell A1. You should add a number so that it could be formatted in custom number format.

So either you should use this code to enter number with quotes

cell.PutValue(-0.123456);

or you should use second parameter as true so that Aspose.Cells automatically convert your string into number.

cell.PutValue("-0.123456", true);

Please see the following code for your reference.

C#


Workbook workbook = new Workbook();

Worksheet worksheet = workbook.Worksheets[0];


worksheet.Cells.SetColumnWidth(0, 30);

Cell cell = worksheet.Cells[“A1”];

cell.PutValue("-0.123456", true);


Style stl = cell.GetStyle();

// stl.Number = 2;

stl.Custom = “0.000000”;

// stl.CultureCustom = “0.000000”;

cell.SetStyle(stl);


workbook.Save(“book2.xls”);

Thank you for your help very much ! Your suggestion works great !

Hi Jiaguo,

Thanks for your posting and using Aspose.Cells.

It is good to know that your issue is sorted out with the above code change. Let us know if you encounter any other issue, we will be glad to look into it and help you further.