Tuesday, September 16, 2008

SAP Query output( User exit) Saving File .csv to appln Server
















Enhancement Text
SQUE0001 SAP Query: Private data file

User Exit Description
FUNCTION
EXIT_RSAQEXCE_001.
*"----------------------------------------------------------------------
*"*"Lokale
Schnittstelle:
*" IMPORTING
*" VALUE(LISTTEXT) TYPE C
*" VALUE(LIST_ID)
LIKE RSAQLDESC-LID
*" VALUE(PROGRAM) LIKE SY-REPID
*" TABLES
*"
DATATAB
*" LISTDESC STRUCTURE
RSAQLDESC
*"----------------------------------------------------------------------
*
Parameter description
* ---------------------
*
* LISTTEXT - Title of
delivered sublist
* LIST_ID - Id of delivered sublist
* PROGRAM - Program
that called this function module
* DATATAB - Data table
* LISTDESC -
Descrition table for the data table fields
*
* Parameter contents
*
------------------
* LIST_ID - G00 = Basic list
* Txx = Statistic xx
(01,02,...)
* Rxx = Ranking list xx (01,02,...)
*
* PROGRAM - AQbbmmqq
mit bb = User group name
* mm = Client (encoded)
* qq = Query
name
*
* DATATAB - contains the data of the delivered sublist
* (The
table structure is determined by the definition
* of the sublist.)
*
*
LISTDESC - contains the description of the data table fields
* The table
structure is always RSAQLDESC.
* Each line of this table contains information
about one
* field.
*
* important: The table LISTDESC always contains
the description for the
* fields of all query sublists. The DATATAB always
contains
* only the data of one sublist.
*
* Fields of structure
RSAQLDESC
* -----------------------------
*
* LID(3) TYPE C Sublist Id
(see LIST_ID)
* FPOS(2) TYPE N Position of field in sublist according to
LID
* FCONT(1) TYPE N Number of continuation line (0,1,...)
* (for basic
lists only)
* FCURPOS(3) TYPE N Start position of field in list line
*
(for numeric fields only)
* FGRAF(1) TYPE C X = Used as description text for
graphics
* (for non numeric fields only)
* FTYP(1) TYPE C Field type (ABAP
type: C,N,P,...)
* FLEN(3) TYPE N Field length
* FDEC(2) TYPE N Decimal
places
* FOLEN(3) TYPE N Field output length
* FCUR(1) TYPE C Currency
flag F = currency amount field
* W = currency unit field
* FADD(1) TYPE C
Internal field R = rank (ranking list)
* C = counter (Statistic)
* P =
percentage (Statistic)
* M = average (Statistic)
* FDESC(40) TYPE C Field
description (field text)
* FNAME(30) TYPE C Fieldname (source field
name)
* FCOL(30) TYPE C One-line column header

INCLUDE
ZXQUEU01.
ENDFUNCTION.

Include Program Code(ZXQUEU01.)

TYPES t_data(4096) TYPE c OCCURS 0.
DATA: outtab TYPE t_data.
DATA: outtab1(4096).
DATA: ltab(4096).
FIELD-SYMBOLS: ,,.
DATA: v_f(30) TYPE c.
LOOP AT datatab ASSIGNING .
*datatab-name1 = ''.
LOOP AT listdesc WHERE ftyp = 'C'.
v_f = listdesc-fname.
UNASSIGN .
ASSIGN COMPONENT v_f OF STRUCTURE TO .
IF CS ','.
CONCATENATE '"' '"' INTO .
ENDIF.
IF listdesc-fname CS 'LIFNR'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input =
IMPORTING
output = .
ENDIF.
ENDLOOP.
ENDLOOP.
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
EXPORTING
i_field_seperator = ','
* I_LINE_HEADER =
* i_filename = p_in_pc
* I_APPL_KEEP = ' '
TABLES
i_tab_sap_data = datatab
CHANGING
i_tab_converted_data = outtab
EXCEPTIONS
conversion_failed = 1
OTHERS = 2 .


LOOP AT outtab INTO outtab1.
REPLACE ALL OCCURENCES OF ';' IN outtab1 WITH ','.
MODIFY outtab FROM outtab1.
ENDLOOP.
DATA : lv_file_name LIKE rlgrap-filename.
TABLES: zkofax_file.
SELECT SINGLE * FROM zkofax_file WHERE intf = 'OPENTEXT'.
IF sy-subrc = 0.
*CONCATENATE '\\apacsap01d\Interfaces\Kofax\' listtext '_' sy-datum '.csv'
CONDENSE zkofax_file-filename.
* CONCATENATE zkofax_file-filename listtext '_' sy-datum '_' sy-uzeit(4) '.csv'
* INTO lv_file_name.
CONCATENATE zkofax_file-filename 'WAVE1_VIMVENDORS COCODE' '.csv'
INTO lv_file_name.
ELSE.
MESSAGE s000(zot) WITH 'No download Directory maintained in table ZKOFAX_FILE' .
ENDIF.
DATA: l_text(300).
OPEN DATASET lv_file_name FOR OUTPUT IN TEXT MODE ENCODING DEFAULT MESSAGE l_text.
IF sy-subrc = 0.
* transfer the Header
CLEAR ltab.
LOOP AT listdesc.
CONCATENATE ltab
* listdesc-fdesc
listdesc-fcol
',' INTO ltab.
ENDLOOP.
TRANSFER ltab TO lv_file_name.
*Transfer the data
CLEAR ltab.
LOOP AT outtab INTO ltab.
TRANSFER ltab TO lv_file_name.
ENDLOOP.
CLOSE DATASET lv_file_name.
MESSAGE s000(zot) WITH 'File Downloaded .' .
ELSE.
IF sy-batch = 'X'.
MESSAGE s000(zot) WITH 'Error in File Download.See Below msg'.
ENDIF.
MESSAGE s000(zot) WITH l_text.
ENDIF.

You have to choose Private File option .