Formatting number to Text

Hi.. I am trying to format number as Text in my excel, but I am unable to do the same. could you please help?

Hi,


Thanks for your query.

Please see the document on how you may specify different numbers formats including “Text” for your complete reference:

Also, kindly see the following sample code for your reference, the code just set a range’s display numbers formats as Text.
e.g
Sample code:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
Range range = cells.CreateRange(“A1”, “C3”);
range[0,0].PutValue(“01-01-01”);
range[0, 2].PutValue(“01-10-03”);
range[2, 2].PutValue(“11-03-04”);
Style style = workbook.CreateStyle();
style.Custom = “@”; //Set the display format as Text/string
StyleFlag flag = new StyleFlag();
flag.NumberFormat = true; //Make the numbers formatting on

range.ApplyStyle(style, flag);
workbook.Save(“e:\test2\ntextstyle.xls”);

Hope, this helps a bit.

Thank you.


Thanks for replying back.


I am doing the same thing, but its not working for me.
I want to export my data in CSV. Do I need to change anything?

Hi Ishan,

The code snippet provided in our previous post works equally good if you are save the result in CSV format. Please check the following code snippet and it’s resultant file.

C#

Workbook workbook = new Workbook();

Worksheet worksheet = workbook.Worksheets[0];

var cells = worksheet.Cells;

Range range = cells.CreateRange("A1", "B3");

range[0, 0].PutValue("01-01-2001");

range[1, 0].PutValue("01-10-2003");

range[2, 0].PutValue("11-03-2004");

range[0, 1].PutValue("01-01-2001");

range[1, 1].PutValue("01-10-2003");

range[2, 1].PutValue("11-03-2004");

Style style = workbook.CreateStyle();

style.Custom = "@"; //Set the display format as Text/string

StyleFlag flag = new StyleFlag();

flag.NumberFormat = true; //Make the numbers formatting on

range.ApplyStyle(style, flag);

workbook.Save("C:/temp/output.csv");

That said, please confirm where are you loading the CSV to inspect the result. If you are loading it in Excel then it may format the values according to your locale, where the actual values are different therefore please use notepad to check the result.