CSV File Layout and issue with Leading zeros in excel

I created a SQL view and tied that to a file layout with csv format, double quote as qualifier and comma as delimiter.
In the peoplecode to generate the csv file I just used WriteRecord method of File class.

The problem I faced was for all the character fields with Leading Zeros,
when I open the file in notepad it was ok
when I open the file in excel it was removing the leading zeros

So I used a workaround.
Excel treats ="001020" as 001020 and "001020" as 1020 while displaying the cell content(!!)

Written following logic instead of Writerecord, I used Writeline and an array with any data type.

Code snippet: In this example, Position number, department, jobcode and location were having issues with leading zeros.


&sqlobject = CreateSQL("SELECT * FROM (view with 44 fields)");
&array_AAny = CreateArrayAny();

&FileObject = GetFile(&FileName, "W", %FilePath_Absolute);
&FileObject.WriteLine("Employee ID" | "," | "Current Month" | "," | "Current Month + 1" | "," | "Current Month + 2" | "," | "Current Month + 3" | "," | "Current Month + 4" | "," | "Current Month + 5" | "," | "Current Month + 6" | "," | "Current Month + 7" | "," | "Current Month + 8" | "," | "Current Month + 9" | "," | "Current Month + 10" | "," | "Current Month + 11" | "," | "Home Phone" | "," | "Cell Phone" | "," | "Empl First Name" | "," | "Empl Last Name" | "," | "AD username" | "," | "Empl email" | "," | "Employment Type" | "," | "Empl Status" | "," | "Position number" | "," | "Position status" | "," | "Vendor ID" | "," | "Vendor Name" | "," | "Onsite/Offshore" | "," | "Hire Date" | "," | "Term Date" | "," | "Dept" | "," | "Dept Name" | "," | "Company" | "," | "Location" | "," | "Job Code" | "," | "Job Title" | "," | "Address 1" | "," | "Address 2" | "," | "City" | "," | "State" | "," | "Postal" | "," | "Country" | "," | "Supervisor ID" | "," | "Supervisor First Name" | "," | "Supervisor Last Name" | "," | "Supervisor Email");
If &FileObject.IsOpen Then
If &FileObject.SetFileLayout(FileLayout.(view with 44 fields)) Then
While &sqlobject.Fetch(&array_AAny)
&FileObject.writeline("""" | &array_AAny [1] | """,""" | &array_AAny [2] | """,""" | &array_AAny [3] | """,""" | &array_AAny [4] | """,""" | &array_AAny [5] | """,""" | &array_AAny [6] | """,""" | &array_AAny [7] | """,""" | &array_AAny [8] | """,""" | &array_AAny [9] | """,""" | &array_AAny [10] | """,""" | &array_AAny [11] | """,""" | &array_AAny [12] | """,""" | &array_AAny [13] | """,""" | &array_AAny [14] | """,""" | &array_AAny [15] | """,""" | &array_AAny [16] | """,""" | &array_AAny [17] | """,""" | &array_AAny [18] | """,""" | &array_AAny [19] | """,""" | &array_AAny [20] | """,""" | &array_AAny [21] | """," | "=" | """" | &array_AAny [22] | """,""" | &array_AAny [23] | """,""" | &array_AAny [24] | """,""" | &array_AAny [25] | """,""" | &array_AAny [26] | """,""" | &array_AAny [27] | """,""" | &array_AAny [28] | """," | "=" | """" | &array_AAny [29] | """,""" | &array_AAny [30] | """,""" | &array_AAny [31] | """," | "=" | """" | &array_AAny [32] | """," | "=" | """" | &array_AAny [33] | """,""" | &array_AAny [34] | """,""" | &array_AAny [35] | """,""" | &array_AAny [36] | """,""" | &array_AAny [37] | """,""" | &array_AAny [38] | """,""" | &array_AAny [39] | """,""" | &array_AAny [40] | """,""" | &array_AAny [41] | """,""" | &array_AAny [42] | """,""" | &array_AAny [43] | """,""" | &array_AAny [44] | """");
End-While;
End-If;
End-If;

This worked well for the users!!

1 comment: