Groups | Blog | Home
all groups > visual studio .net general > august 2006 >

visual studio .net general : leading zeroes in export to CSV


Optimo
8/9/2006 6:16:01 AM
In a procedure I export the contence of a datagrid to a CSV file. No problems
there. Only two of the columns contain numbers with leading zeroes. Those
leading zeroes disapear in the CSV file, but they must be retained.
I tried to add a single quote (as one could do in an Excel worksheet to save
a number as text), but that doesn't work.
Adding a space in front doesn't change a thing and I cannot add any readable
character that is not part of the original number.
So how can I retain the leading zeroes during export to a CSV file?
The datatype of those two columns in the grid is nvarchar (string).

Simon Whale
8/10/2006 12:03:34 AM
Hi Optimo,

how are you opening that CSV file? are you opening it in excel, if so you
will lose the leading zero. have a look at the file in notepad or wordpad

Simon

[quoted text, click to view]

Optimo
8/10/2006 5:12:02 AM
Thanks Simon,
Indeed in Wordpad the leading zeroes are there. That is what I said to the
users but they don't accept that: they want to see them in Excel. So I'm
trying to find a way to do it anyway.
Solutions like placing [] or {} around it they don't accept neither...
--



[quoted text, click to view]
Optimo
8/10/2006 6:09:02 AM
Thanks Simon

But that was one of the first things I did: it doesn't work.
When you open the CSV file in Excel the quote is visible in the cells.
You have to click every single cell separately, click in the formulebar and
then Enter.
Users are not prepared to do that, because they sometimes export over 10000
lines.
Of course they also could make use of Excel functions like =TEXT(), but that
is no option for them.
I even tryed to export that Excel-function but there the parameter
separators mess everything up.
--
ICT


[quoted text, click to view]
Simon Whale
8/10/2006 1:29:17 PM
if you want to use CSV files, try putting a single quote before the number.

e.g. '01234567

Hope it Helps
Simon

[quoted text, click to view]

Steve Barnett
8/11/2006 12:00:00 AM
How do they feel about macros? Assuming your numbers are in columns A and B,
the following macro will put the leading zeros back in:

Sub Macro1()
Columns("A:B").Select
Selection.NumberFormat = "00000"
End Sub

It's not exactly rocket science, but it could solve your problem without the
users needing to do much work (and you could always code it in an Auto_open
macro if they won't run it for you).

HTH
Steve


[quoted text, click to view]

AddThis Social Bookmark Button