Excel remove leading zero, Excel missing leading zero

Are you experiencing missing leading zero in excel ? Here is a short summary about excel and leading zero

When you export a file as excel file in SAP using function module GUI_DOWNLOAD, actually it is downloading as a tab delimited format which you can open with Excel or a notepad. If you export as a plain text file, the leading zero will be maintained but if you open with Excel, the leading zero is not maintained because Excel has it own formatting.  By default, Excel set a cell  as ‘General’ and this formatting will remove any leading zero of a value.

There are three methods how to maintain the leading zero in excel.

Method 1 – use DBF file type
Method 2 – set value into excel’s formula
Method 3 – use function module XXL_FULL_API

Method 1 
Use function module gui_download with the filetype set to ‘DBF’

call method cl_gui_frontend_services=>gui_download
  exporting
    filename                = ‘H:/PD Video/test.xls’
    filetype                = ‘DBF’
    write_field_separator   = ‘X’
  changing
    data_tab                = iexcel.

According do the function module documentation

‘DBF’ :

Data is downloaded in dBase format. Since in this format the data types
of the individual columns are stored as well, you can often avoid import
problems, for example, into Microsoft Excel, especially when
interpreting numeric values.

Method 2

A simple workaround for this is to set the value in the cell’s formula.

I believe you may use any excel formula. In my example I used CONCATENATE.

CONCATENATE ‘=CONCATENATE(“‘xt001-bukrs'”)’ into wa_t001-field_a
wa_t001-field_b = xt001-bukrs.
wa_t001-field_c = xt001-description.

Image

Method 3 
Use FM XXL_FULL_API, where you can define the cell formatting to ‘Text’ instead of ‘General’.
With ‘Text’ formatting , the leading zero of a value will be maintained.