Free Support Forum - aspose.com

SaveFormat CSV and leading zeros

Hello,


I’ve got a problem I’m trying to solve for a long time.
I want to save my worksheet, with imported data table, as .csv file.
One of data table’s column has values with leading zeros. This column is type of string.
When I open created file in text editor it is all fine - leading zeros are there.
But when I do open this file in Excel, leading zeros are missing.

I guess it is because cells are ‘general’ type.
Unfortunately I can’t figure out how to change their type. Is that because it is .csv file? I was able to do it when save format was .xls or .xlsx.

Pieces of my code, I tried many variations of something like these, but nothing happens.

Style style = document.CreateStyle();
style.Number = 49;
//style.Custom = “@”;

document.Worksheets[0].Cells[“N13”].SetStyle(style1, new StyleFlag() { NumberFormat = true, CellShading = true });

Style s = document.Worksheets[0].Cells[“N13”].GetStyle();
s.Number = 49;
document.Worksheets[0].Cells[“N13”].SetStyle(style2, new StyleFlag() { NumberFormat = true, CellShading = true });

Also tried:
Workbook document = new Workbook(FileFormatType.CSV);
document.DefaultStyle.Custom = “@”;
//document.DefaultStyle.Number = 49;

Saving code:
TxtSaveOptions opts = new TxtSaveOptions();
opts.Separator = ‘;’;
opts.Encoding = System.Text.Encoding.UTF8;
document.Save(saveFileName, opts);



Can this be achieved at all for .csv saving format?

Thanks for reply!

Dawid
Hi,

Thanks for your query.

I think you cannot do it in CSV file format, numbers formattings applied in a CSV file is not saved as this is only simple text format. You may try to create a simple CSV file. Right-click on a cell (e.g A1) and click "Format Cells..." options, now select "Text" from the category. Enter a value i.e. "00009343". Now, save/resave the CSV file, you got the error, anyways kindly save it. Now close the file. Again re-open the file into MS Excel and you will see that the leading zeros are gone. So, this is not possible in MS Excel either. You have to choose other file formats e.g XLS/XLSX etc.

If you still think that this is possible in MS Excel, please create such a CSV file (having values with leading zeros) in MS Excel manually and post it here, we will check it soon.

Thank you.
Yes, you're right, it can be done manually, but unfortunately this is not an option for me :)

Thank you for answer, I will consider using .xls files.

Dawid