* _______ _______ _______ _______ _______ _______ ______ _______ _______ ______ _______ * | _ | _ | _ | | | _ | || _ | _ | _ | | _ | * | |_| | |_| | |_| | _ | | |_| | _ | |_| | |_| | | || | |_| | * | | | | |_| | | | | | | | | |_||_| | * | | _ || | ___| _| | |_| | | _ || __ | | * | _ | |_| | _ | | | |_| _ | | _ | |_| | | | | _ | * |__| |__|_______|__| |__|___| |_______|__| |__|______||__| |__|_______|___| |_|__| |__| * www.abapcadabra.com * *------------------------------------------------------------------------------------------- * program : ZABAPCADABRA_REPORT_2_FILE * title : Run a report and store it's output in a file * functional area : Cross modules * environment : 4.7 * program Function : A simple add-on that can be used to store the output * of a report as a flat-file on either server or client. * Composed for use with a 32.000 page report, so it can * actually do a big job. * Documentation : Search for "report into a file" on AbapcadabrA.com * Previous version : This is the initial version * Developer name : Wim Maasdam * Development date : 23/12/2014 * Version : 0.1 *--------------------------------------------------------------------- * Change list: * Date Description * 23/12/2014 Initial release * 04/02/2015 Added "Append to file" parameter * 15/07/2015 Added "Convert to CSV" parameter *--------------------------------------------------------------------- report ZABAPCADABRA_REPORT_2_FILE line-size 1023. data: gt_abaplist type standard table of abaplist, gt_lines type standard table of char780, gw_line type char780, gv_dataset type string. *--------------------------------------------------------------------- * C L A S S D E F I N I T I O N *--------------------------------------------------------------------- class lcl_f4_processing definition. public section. class-methods: f4_presentation_file importing title type any changing filepath type any, f4_server_file changing filepath type any. endclass. "lcl_f4_processing DEFINITION *--------------------------------------------------------------------- * S E L E C T I O N - S C R E E N *--------------------------------------------------------------------- selection-screen begin of line. selection-screen comment 1(30) lbl_01 for field pa_repid. parameters: pa_repid type rsvar-report obligatory. selection-screen end of line. selection-screen begin of line. selection-screen comment 1(30) lbl_02 for field pa_varid. parameters: pa_varid type rsvar-variant obligatory. selection-screen end of line. selection-screen skip. selection-screen begin of line. parameters: pa_locl as checkbox. selection-screen comment 3(28) lbl_03 for field pa_filen. parameters: pa_filen type c length 100 lower case visible length 56. selection-screen end of line. selection-screen begin of line. parameters: pa_serv as checkbox. selection-screen comment 3(28) lbl_04 for field pa_file2. parameters: pa_file2 type c length 100 lower case visible length 56. selection-screen end of line. selection-screen skip. selection-screen begin of line. parameters: pa_appen as checkbox. selection-screen comment 3(28) lbl_05 for field pa_appen. selection-screen end of line. selection-screen begin of line. parameters: pa_ascsv as checkbox. selection-screen comment 3(35) lbl_06 for field pa_ascsv. selection-screen end of line. at selection-screen on value-request for pa_filen. lcl_f4_processing=>f4_presentation_file( exporting title = 'Choose file' changing filepath = pa_filen ). at selection-screen on value-request for pa_file2. lcl_f4_processing=>f4_server_file( changing filepath = pa_file2 ). *--------------------------------------------------------------------- * C L A S S I M P L E M E N T A T I O N *--------------------------------------------------------------------- class lcl_f4_processing implementation. method f4_presentation_file. data: lt_files type standard table of sdokpath, lw_file type sdokpath. call function 'TMP_GUI_FILE_OPEN_DIALOG' exporting window_title = title default_filename = filepath tables file_table = lt_files exceptions others = 4. if sy-subrc <> 0. message 'Front end file open error' type 'E'. else. read table lt_files index 1 into lw_file. filepath = lw_file-pathname. endif. endmethod. "f4_presentation_file method f4_server_file. data: lv_old_filepath type string. lv_old_filepath = filepath. call function '/SAPDMC/LSM_F4_SERVER_FILE' exporting directory = filepath filemask = ' ' importing serverfile = filepath exceptions canceled_by_user = 1 others = 2. case sy-subrc. when 1. filepath = lv_old_filepath. when 2. message 'Server file open error' type 'E'. endcase. endmethod. "f4_server_file endclass. "lcl_f4_processing IMPLEMENTATION *--------------------------------------------------------------------- * I N I T I A L I Z A T I O N *--------------------------------------------------------------------- initialization. * All texts for this report have been set up as hard-coded texts (no use * of the report text-pool). Reason: easy copying of report source code. lbl_01 = 'Report'. lbl_02 = 'Variant'. lbl_03 = 'Save on client (PC)'. lbl_04 = 'Save on server'. lbl_05 = 'Append to file'. lbl_06 = 'Convert to CSV (Query output)'. *--------------------------------------------------------------------- * A T S E L E C T I O N - S C R E E N O U T P U T *--------------------------------------------------------------------- at selection-screen output. if pa_filen is initial and sy-batch is initial. data: lv_filename type string. cl_gui_frontend_services=>directory_get_current( changing current_directory = lv_filename ). cl_gui_cfw=>flush( ). concatenate lv_filename 'REP2FILE_{S}_{D}_{T}.txt' into pa_filen separated by '\'. endif. if pa_file2 is initial. data: lw_pars type spfpflpar, lv_separator type c length 1. lw_pars-parname = 'DIR_HOME'. call function 'RSAN_SYSTEM_PARAMETER_READ' exporting i_name = lw_pars-parname importing e_value = lw_pars-pvalue exceptions others = 0. lv_separator = lw_pars-pvalue(1). if lv_separator <> '/'. lv_separator = '\'. endif. concatenate lw_pars-pvalue 'REP2FILE_{S}_{D}_{T}.txt' into pa_file2 separated by lv_separator. endif. *--------------------------------------------------------------------- * S T A R T - O F - S E L E C T I O N *--------------------------------------------------------------------- start-of-selection. * Clear the last list(s) from memory (to avoid picking up an old list) call function 'LIST_FREE_MEMORY'. * Run the report using the specified variant, return results submit (pa_repid) using selection-set pa_varid exporting list to memory and return. * List output from memory into ABAPLIST - transform into readable form call function 'LIST_FROM_MEMORY' tables listobject = gt_abaplist exceptions others = 4. if sy-subrc <> 0. message 'Submit failed - no output could be found' type 'S'. else. * Transform to more readable format call function 'LIST_TO_ASCI' exporting with_line_break = 'X' tables listasci = gt_lines listobject = gt_abaplist exceptions others = 0. * The option to process Query output into a .csv file: if pa_ascsv = abap_true. data: lw_line_fieldnames type char780. read table gt_lines index 2 into lw_line_fieldnames. loop at gt_lines into gw_line. if gw_line cs '-----'. delete gt_lines. elseif gw_line = lw_line_fieldnames. delete gt_lines. else. REPLACE ALL OCCURRENCES OF ';' IN gw_line WITH ','. REPLACE ALL OCCURRENCES OF '|' IN gw_line WITH ';'. modify gt_lines from gw_line. endif. endloop. * Now put the header line back again: REPLACE ALL OCCURRENCES OF '|' IN lw_line_fieldnames WITH ';'. insert lw_line_fieldnames into gt_lines index 1. endif. *--------------------------------------------------------------------- * Prepare path and filename data: lv_system type c length 15, lv_system_str type string. concatenate sy-sysid sy-mandt into lv_system separated by '_'. condense lv_system no-gaps. lv_system_str = lv_system. *--------------------------------------------------------------------- if pa_locl = 'X'. move pa_filen to gv_dataset. replace '{S}' with lv_system_str into gv_dataset. replace '{D}' with sy-datum into gv_dataset. replace '{T}' with sy-uzeit into gv_dataset. replace '{H}' with sy-uzeit(2) into gv_dataset. replace '{M}' with sy-uzeit+2(2) into gv_dataset. call function 'GUI_DOWNLOAD' exporting filename = gv_dataset filetype = 'ASC' append = pa_appen tables data_tab = gt_lines exceptions others = 4. if sy-subrc <> 0. message 'Save failed' type 'S'. endif. endif. *--------------------------------------------------------------------- if pa_serv = 'X'. move pa_file2 to gv_dataset. replace '{S}' with lv_system_str into gv_dataset. replace '{D}' with sy-datum into gv_dataset. replace '{T}' with sy-uzeit into gv_dataset. replace '{H}' with sy-uzeit(2) into gv_dataset. replace '{M}' with sy-uzeit+2(2) into gv_dataset. if pa_appen = space. OPEN DATASET gv_dataset FOR OUTPUT IN TEXT MODE ENCODING DEFAULT. else. OPEN DATASET gv_dataset FOR APPENDING IN TEXT MODE ENCODING DEFAULT. endif. if sy-subrc = 0. loop at gt_lines into gw_line. transfer gw_line to gv_dataset. endloop. close dataset gv_dataset. else. message 'Could not open server file' type 'S'. endif. endif. *--------------------------------------------------------------------- if pa_locl is initial and pa_serv is initial. "Dump on the screen loop at gt_lines into gw_line. write: / gw_line. endloop. endif. endif.