‎2006 May 10 2:09 PM
Hi,
I've written some program using the function 'REUSE_ALV_LIST_DISPLAY' and make use of the standard function provided by the ALV table create by this function to export the data to Excel.
However, I noticed that the export function will automatically reshuffle the columns so that all the text columns comes first and then followed by the columns with numberic field.
Would be grateful if you guys have any idea how I can keep the columns in the exported excel file in the same sequence as what is displayed in the alv table.
Many thanks in advance,
Francis
‎2006 May 10 2:14 PM
hii
try this one
REPORT zupload_excel_to_itab.
TYPE-POOLS: truxs.
PARAMETERS: p_file TYPE rlgrap-filename.
TYPES: BEGIN OF t_datatab,
col1(30) TYPE c,
col2(30) TYPE c,
col3(30) TYPE c,
END OF t_datatab.
DATA: it_datatab type standard table of t_datatab,
wa_datatab type t_datatab.
DATA: it_raw TYPE truxs_t_text_data.
At selection screen
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
***********************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = it_raw " WORK TABLE
i_filename = p_file
TABLES
i_tab_converted_data = it_datatab[] "ACTUAL DATA
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
***********************************************************************
END-OF-SELECTION.
END-OF-SELECTION.
LOOP AT it_datatab INTO wa_datatab.
WRITE:/ wa_datatab-col1,
wa_datatab-col2,
wa_datatab-col3.
ENDLOOP.
hope this helps
Thanks n Regards
Naresh
‎2006 May 10 2:13 PM
Francis,
You will have do that manually using the GUI_DOWNLOAD method of CL_GUI_FRONTEND_SERVICES. Add a button and you can do that in there. You can hide the standard one
The standard will reshuffle the columns like you said.
Regards,
Ravi
Note : please mark the helpful answers
‎2006 May 10 2:14 PM
hii
try this one
REPORT zupload_excel_to_itab.
TYPE-POOLS: truxs.
PARAMETERS: p_file TYPE rlgrap-filename.
TYPES: BEGIN OF t_datatab,
col1(30) TYPE c,
col2(30) TYPE c,
col3(30) TYPE c,
END OF t_datatab.
DATA: it_datatab type standard table of t_datatab,
wa_datatab type t_datatab.
DATA: it_raw TYPE truxs_t_text_data.
At selection screen
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
***********************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = it_raw " WORK TABLE
i_filename = p_file
TABLES
i_tab_converted_data = it_datatab[] "ACTUAL DATA
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
***********************************************************************
END-OF-SELECTION.
END-OF-SELECTION.
LOOP AT it_datatab INTO wa_datatab.
WRITE:/ wa_datatab-col1,
wa_datatab-col2,
wa_datatab-col3.
ENDLOOP.
hope this helps
Thanks n Regards
Naresh
‎2006 May 10 2:21 PM
Hi,
if you want always download then you can use GUI_DOWNLOAD before calling the ALV FM. or else you can place one button and call gui_download when you press that.
this will not alter your file.
Regards
vijay
‎2006 May 10 2:34 PM
Hey Francis,
You put the data to be downloaded to the excel file into an internal table , say , itab.
And then call function module GUI_Download.
Sample Code :
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE =
filename = <b>'D:\test2.xls'</b>
filetype = 'ASC'
APPEND = ' '
WRITE_FIELD_SEPARATOR = 'I'
HEADER = '00'
trunc_trailing_blanks = 'X'
WRITE_LF = 'X'
COL_SELECT = ' '
COL_SELECT_MASK = ' '
DAT_MODE = ' '
CONFIRM_OVERWRITE = ' '
NO_AUTH_CHECK = ' '
CODEPAGE = ' '
IGNORE_CERR = ABAP_TRUE
replacement = 'E'
WRITE_BOM = ' '
TRUNC_TRAILING_BLANKS_EOL = 'X'
IMPORTING
FILELENGTH =
TABLES
data_tab = itab
FIELDNAMES =
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22
.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
Regards,
Kunal.
‎2006 May 11 2:38 AM
Now I encounter another problem.
Instead of using the function provided from ALV, I now call GUI_DOWNLOAD to download the file, however, the column heading cannot be exported as in the case when I'm exporting from the ALV.
Any means to export the content of the internat table, containing both text and numeric columns, to an excel file with column heading? I want to keep the number column as numeric, not text, in the excel file.
Many many thanks for your help in advance,
Francis
‎2006 May 11 3:04 AM
Franis,
Unless you manually add a line to the table for headings, there is not much we can do about it using GUI_DOWNLOAD.
A work around would be to try and use the DOI / OLE methods and see if its possible.
Regards,
Ravi
‎2006 May 11 3:08 AM
Hi Ravi,
Would it be possible if you can share some code sample showing me how to use the DOI/OLE methods.
Thanks,
Francis
‎2006 May 11 3:26 AM
Hi,
I have not done this myself, so I am pointing you to couple of blogs.
/people/rich.heilman2/blog/2005/09/12/manipulate-excel-with-ole-abap
/people/sap.user72/blog/2006/02/07/downloading-data-into-excel-with-format-options
Take a look and see if they are of any use to you.
Regards,
Ravi
Note : Please mark the helpful answers
Message was edited by: Ravikumar Allampallam
‎2006 May 11 6:42 AM
Hi Francis,
copy the code given below and run it. I have used the fm 'EXCEL_OLE_STANDARD_DAT' to download data and its heading into the excel file . The d/b table i have used is table Zkunal1 with fields MANDT , FNAME , LNAME and PLACE.
&----
*& Report ZKUN_FILE4 *
*& *
&----
*& *
*& *
&----
REPORT ZKUN_FILE4 .
TABLES: USR03,DD02L,zkunal1.
DATA: ZX030L LIKE X030L.
DATA BEGIN OF ZDFIES OCCURS 0.
INCLUDE STRUCTURE DFIES.
DATA END OF ZDFIES.
DATA: BEGIN OF FLDITAB OCCURS 0,
FLDNAME(11) TYPE C,
END OF FLDITAB.
*DATA ITABUSR03 LIKE USR03 OCCURS 0 WITH HEADER LINE.
DATA ITABUSR03 LIKE zkunal1 OCCURS 0 WITH HEADER LINE.
DATA TNAME LIKE DD02L-TABNAME.
*SELECT * FROM USR03 INTO TABLE ITABUSR03.
SELECT * FROM zkunal1 INTO TABLE ITABUSR03.
*TNAME = 'USR03'.
TNAME = 'zkunal1'.
PERFORM GETFIELEDS.
PERFORM SHOW123.
********************************************
FORM GETFIELEDS.
CALL FUNCTION 'GET_FIELDTAB'
EXPORTING
LANGU = SY-LANGU
ONLY = SPACE
TABNAME = TNAME
WITHTEXT = 'X'
IMPORTING
HEADER = ZX030L
TABLES
FIELDTAB = ZDFIES
EXCEPTIONS
INTERNAL_ERROR = 01
NO_TEXTS_FOUND = 02
TABLE_HAS_NO_FIELDS = 03
TABLE_NOT_ACTIV = 04.
CASE SY-SUBRC.
WHEN 0.
LOOP AT ZDFIES.
FLDITAB-FLDNAME = ZDFIES-FIELDNAME.
APPEND FLDITAB.
ENDLOOP.
WHEN OTHERS.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
with SY-SUBRC.
ENDCASE.
ENDFORM.
***********************************
FORM SHOW123.
CALL FUNCTION 'EXCEL_OLE_STANDARD_DAT'
EXPORTING
FILE_NAME = 'C:\USR03.XLS'
DATA_SHEET_NAME = 'USER LIST'
TABLES
DATA_TAB = ITABUSR03
FIELDNAMES = FLDITAB
EXCEPTIONS
FILE_NOT_EXIST = 1
FILENAME_EXPECTED = 2
COMMUNICATION_ERROR = 3
OLE_OBJECT_METHOD_ERROR = 4
OLE_OBJECT_PROPERTY_ERROR = 5
INVALID_FILENAME = 6
INVALID_PIVOT_FIELDS = 7
DOWNLOAD_PROBLEM = 8
OTHERS = 9.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM.
Regards,
Kunal.