SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Download table contents to PC local file or app.server



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP Dictionary -> Migration
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Oct 06, 2007 11:43 pm    Post subject: Download table contents to PC local file or app.server Reply with quote

*
These programs are very useful for testing or programming and
sometimes very helpful than transaction ‘SE11’ or ‘SE16 (N)’.
*

Code:
*&---------------------------------------------------------------------*
* Created  on  2007.02.15 by Y.SAITA  TOKYO,JAPAN
*&---------------------------------------------------------------------*
* You can do anything to this report without my permission.
* (modify,fix bugs,sell,republish,etc)
* This program is a use-at-your-own-risk-basis-program,
* So use at your own risk.
*
* In no event will the author be liable for indirect, special,
* Incidental, or consequental damages (if any) arising out of
* the use of this program.
* The person, who implements this program,
* have to be sure that such damage does not happen.
*
*&---------------------------------------------------------------------*
*&  Download table contents to PC local file or app.server
*&---------------------------------------------------------------------*
*1:[Description]
* This program downloads table contents to PC file(Excel sheet or text)
* or App.server.
* Choose a table you want to download.
* You can use 'Dynamic selection' by pressing 'Dynamic selection'.
* You can save the downloaded data as Excel or Text (Tab).
* See the attached PDF.file.
*
*2:[NOTE]
*   You may need to change the Japanese yen mark to backslash.
*
*3:[Security Notes]
*  If you want that only you can run this program,
*  You can use macro M_AUTH_CHK,M_AUTH_CHK2.
*  If you don't need them,delete them.
*
*  Tested on 'Enterprise'(other version may not work) and UNIX and
*  Excel 2003.
*  Some codes are not needed with Excel 2007.
*  Any feedback will be welcome. E-mail is [email protected]
*&---------------------------------------------------------------------*
*[Selection Texts]
"I002     Table
"I005     Choose table for download
"I006     Save as Excel or Text(Tab)
"I007     Microsoft Excel book
"I008     Text (Tab)
"I009     Table ID
"I015     Local PC  file
"I016     Server file
"I017     Download to
"I018     Local file path & file
"I019     Directory path & file

*[Text Symbols]
"SP_APSV          Server
"SP_DIR           Directory
"SP_EXCEL         Excel
"SP_FILE          file
"SP_LCPC          lcpc
"SP_TAB           Table Id
"SP_TEXT          text


REPORT Y_ZSAITA_400   MESSAGE-ID 00.

*----------------------------------------------------------------------*
* TABLESs
*----------------------------------------------------------------------*
TABLES: SSCRFIELDS,  "Fields on selection screens
        DD02T.       "table text


*----------------------------------------------------------------------*
* TYPE
*----------------------------------------------------------------------*
*
TYPE-POOLS: KCDE,                   "EC-EIS
            OLE2,                   "OLE
            RSDS.                   "type group for dynamic selection

TYPES: TY_VALUE(256) TYPE C.        "value

*----------------------------------------------------------------------*
* CONSTANTS
*----------------------------------------------------------------------*
CONSTANTS:
   COS_FALSE(5)     TYPE C        VALUE 'False',"False
   COS_WRITE_ROWS   TYPE I        VALUE 500,    "EXCEL row
   COS_DWNLOAD_SIZE TYPE I        VALUE 1024,   "internal table lines
   COS_DOT_XLS(4)   TYPE C        VALUE '.xls', "file extention forEXCEL
   COS_DOT_TXT(4)   TYPE C        VALUE '.txt', "file extention forTXT
   COS_PERIOD(1)    TYPE C        VALUE '.',    "period
   COS_PATH         TYPE C        VALUE '\',    "filepath for pc "<--backslash??
   COS_MAXLINE      TYPE I        VALUE 65536,  "EXCEL max line

   COS_LOCAL        TYPE AS4LOCAL VALUE 'A',    "Activation status
   COS_VERS         TYPE AS4VERS  VALUE '0000', "version
   COS_YES(4)       TYPE C        VALUE 'YES',  "for pop-up
   COS_NO(6)        TYPE C        VALUE 'NO',   "for pop-up

*Tab
   COS_TAB(1)       TYPE C        VALUE
                    CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

*
*CONSTANTS FOR VisualBasic
CONSTANTS:
* Class / Function / Property
   COS_XLAPPLI(17)  TYPE C        VALUE 'Excel.Application',
   COS_DEFLTPATH    TYPE SYCHAR32 VALUE 'DefaultFilePath',
   COS_WORKBOOK     TYPE SYCHAR32 VALUE 'Workbooks',
   COS_ADD          TYPE SYCHAR32 VALUE 'Add',
   COS_ACTSHEET     TYPE SYCHAR32 VALUE 'ActiveSheet',
   COS_CELLS        TYPE SYCHAR32 VALUE 'Cells',
   COS_RANGE        TYPE SYCHAR32 VALUE 'RANGE',
   COS_COLUMNS      TYPE SYCHAR32 VALUE 'Columns',
   COS_VISIBLE      TYPE SYCHAR32 VALUE 'Visible',
   COS_ALERTS       TYPE SYCHAR32 VALUE 'DisplayAlerts',
   COS_REFSTYLE     TYPE SYCHAR32 VALUE 'ReferenceStyle',
   COS_PASTE        TYPE SYCHAR32 VALUE 'Paste',
   COS_VALUE        TYPE SYCHAR32 VALUE 'Value',
   COS_FONT         TYPE SYCHAR32 VALUE 'Font',
   COS_BOLD         TYPE SYCHAR32 VALUE 'Bold',
   COS_ALIGNMENT    TYPE SYCHAR32 VALUE 'HorizontalAlignment',
   COS_IGNOREBLANK  TYPE SYCHAR32 VALUE 'IgnoreBlank',
   COS_VALIDATION   TYPE SYCHAR32 VALUE 'Validation',
   COS_DROPDOWN     TYPE SYCHAR32 VALUE 'InCellDropdown',
   COS_ERRMESSAGE   TYPE SYCHAR32 VALUE 'ErrorMessage',
   COS_NUMFORMAT    TYPE SYCHAR32 VALUE 'NumberFormat',
   COS_BORDERS      TYPE SYCHAR32 VALUE 'Borders',
   COS_LINESTYLE    TYPE SYCHAR32 VALUE 'LineStyle',
   COS_BORDERARND   TYPE SYCHAR32 VALUE 'BorderAround',
   COS_INTERIOR     TYPE SYCHAR32 VALUE 'Interior',
   COS_COLORINDEX   TYPE SYCHAR32 VALUE 'ColorIndex',
   COS_AUTOFIT      TYPE SYCHAR32 VALUE 'AutoFit',
   COS_SELECT       TYPE SYCHAR32 VALUE 'Select',
   COS_COLUMNWIDTH  TYPE SYCHAR32 VALUE 'ColumnWidth',
   COS_WINDOWS      TYPE SYCHAR32 VALUE 'Windows',
   COS_FREEZEPANES  TYPE SYCHAR32 VALUE 'FreezePanes',
   COS_NEWWORKBOOK  TYPE SYCHAR32 VALUE 'SheetsInNewWorkbook',
   COS_NAME         TYPE SYCHAR32 VALUE 'Name',
   COS_SAVEAS       TYPE SYCHAR32 VALUE 'SaveAs',
   COS_CLOSE        TYPE SYCHAR32 VALUE 'Close'.
*
CONSTANTS:
* Const
   COS_XLSTYLE_A1   TYPE I        VALUE 1,      "xlA1
   COS_XLSTYLE_R1C1 TYPE I        VALUE -4150,  "xlR1C
   COS_XLCOLOR_HEAD TYPE I        VALUE 6,      "header color(yello)
   COS_XLCOLOR_KEY  TYPE I        VALUE 20,     "key color(light-blue)
   COS_XLSTYLE      TYPE I        VALUE 1,      "ruled line style
   COS_XLMEDIUM     TYPE I        VALUE -4138,  "ruled line-thick-middle
   COS_XLBOTTOM     TYPE I        VALUE 9,      "under line
   COS_XLDOUBLE     TYPE I        VALUE -4119,  "double line
   COS_XLVALIDCSTM  TYPE I        VALUE 7,      "input(user)
   COS_XLVALIDNUM   TYPE I        VALUE 1,      "input(integer)
   COS_XLVALIDDEC   TYPE I        VALUE 2,      "input(decimal)
   COS_XLVALIDLIST  TYPE I        VALUE 3,      "input(list)
   COS_XLVALIDSTOP  TYPE I        VALUE 1,      "error style(stop)
   COS_XLEQUAL      TYPE I        VALUE 3,      "operator(=)
   COS_XLBETWEEN    TYPE I        VALUE 1,      "operator(range)
   COS_XLWORKBOOK   TYPE I        VALUE -4143,  "*.xls
   COS_XLHALIGNLEFT TYPE I        VALUE -4131.  "left


*----------------------------------------------------------------------*
* DATA
*----------------------------------------------------------------------*
* object
DATA: H_EXCEL       TYPE OLE2_OBJECT,           "EXCELD
      H_WORKBOOKS   TYPE OLE2_OBJECT,           "WORKBOOKS
      H_WORKBOOK    TYPE OLE2_OBJECT,           "WORKBOOK
      H_WORKSHEET   TYPE OLE2_OBJECT,           "WORKSHEET
      H_RANGE       TYPE OLE2_OBJECT,           "RANGE
      H_WINDOW      TYPE OLE2_OBJECT,           "WINDOW
      H_CELL        TYPE OLE2_OBJECT,           "CELL
      H_CELL1       TYPE OLE2_OBJECT,           "CELL(RANGE)
      H_FONT        TYPE OLE2_OBJECT,           "FONT
      H_INTERIOR    TYPE OLE2_OBJECT,           "INTERIOR
      H_BORDER      TYPE OLE2_OBJECT,           "BORDER
      H_VALIDATION  TYPE OLE2_OBJECT.           "VALIDATION

* Reference
DATA: REF_TAB       TYPE REF TO DATA,           "table
      REF_LINE      TYPE REF TO DATA.           "record


* FIELD-SYMBOLS
FIELD-SYMBOLS:
      <FS_TAB>      TYPE TABLE,                 "table
      <FS_LINE>,                                "record
      <FS_FIELD>.                               "field

* internal tables
DATA: IT_DFIES      LIKE TABLE OF DFIES,        "Table Fields
      IT_EXCEL      TYPE          KCDE_SENDER,  "EXCEL data(all)
      IT_EXCEL_PART TYPE          KCDE_SENDER,  "EXCEL data(part)
      IT_TABLES     TYPE TABLE OF RSDSTABS,     "for dynamic selections
      IT_WHERE      TYPE          RSDS_TWHERE,  "or dynamic (WHERE)
      IT_EXPRESS    TYPE          RSDS_TEXPR,   "initial selections
      IT_FIELDS_TAB TYPE TABLE OF RSDSFIELDS.   "Selected fields for
                                                "dynamic selections

* work area
DATA: W_DFIES       LIKE LINE OF IT_DFIES,      "table info
      W_DD02T       TYPE DD02T,                 "table text
      W_TABNAME     TYPE DDOBJNAME,             "table name
      W_DDOBJTYPE   TYPE TABCLASS,              "table category

      W_FILEPATH    TYPE STRING,                "filepath
      W_DEFLTPATH   TYPE STRING,                "default path
      W_PATHLEN     TYPE I,                     "length of filepath
      W_RESULT      TYPE ABAP_BOOL,             "flag for file exist
      W_CLIPBOARD   TYPE I,              "return code for clipboard
      W_QUESTION    TYPE STRING,         "pop-up message
      W_ANSWER,                          "pop-up answer
      W_VALUE       TYPE TY_VALUE,              "EXCEL value

      W_EXCEL       LIKE LINE OF IT_EXCEL,  "EXCEL data(value)
      W_XLHEAD      LIKE LINE OF IT_EXCEL,  "EXCEL data(header)
      W_XLHEAD2     LIKE LINE OF IT_EXCEL,  "EXCEL data(header2)
      W_XLHEAD3     LIKE LINE OF IT_EXCEL,  "EXCEL data(header3)

      W_EXTENSION(4),                     "FILE EXTENSION
      W_BOLD        TYPE I,               "Excel line(bold)ON/OFF
      W_ROW_TOP     TYPE I,               "EXCEL start lien

      W_COL         TYPE I,                     "EXCEL colum
      W_ROW         TYPE I,                     "EXCEL row
      W_COL_END     TYPE I,                     "EXCEL colum end
      W_ROW_END     TYPE I,                     "EXCEL rol end
      W_COL_KEY     TYPE I,                     "EXCEL key

      W_HEAD1(100),                  "EXCEL first  row first text
      W_HEAD2(100),                  "EXCEL second row first text

      W_NUMFORMAT(50),                      "cell format
      W_TYPE        TYPE I,                 "input type
      W_DCML_OFFSET TYPE I,                 "decimal offset

      W_FORMULA1(100),                        "input format1
      W_FORMULA2(100),                        "input format2
      W_ERRORMSG(100),                        "input error message
      W_TEXT(50),                              "text

*for dynamic selection
      W_SELECT_ID   TYPE RSDYNSEL-SELID,    "dynamic selection ID
      W_TABLES_TAB  TYPE RSDSTABS,   "field names for dynamic selections
      W_WHERE_CLS   LIKE LINE OF IT_WHERE,  "field selection(all)
      W_WHERE       TYPE RSDSWHERE,         "WHERE clause
      W_TOTAL_WHERE TYPE STRING,            "WHERE clause(all)
      W_DLG_TITLE   TYPE SY-TITLE,          "dialog titel

      W_TABLE_LINES TYPE I,                "internal table lines
      W_TBL_LN(10)  TYPE C,                "internal table lines(output)
      W_LOOP_START  TYPE I,                "loop start
      W_LOOP_END    TYPE I,                "loop end
      W_DBCNT       TYPE I,                "output count
      F_APPEND(1)   TYPE C,                "flag for append
      F_INIT_FLG(1) TYPE C.                "flag for selection ID
*
 DATA :
      W_ABENDFLAG    LIKE DXFIELDS-ABENDFLAG,
      W_FIELD        LIKE DYNPREAD-FIELDNAME,
      W_PATHI        LIKE DXFIELDS-LONGPATH,
      W_PATH         LIKE DXFIELDS-LONGPATH,
      W_SERVER_NAME  LIKE MSXXLIST-NAME,
      W_DELIMIT      TYPE C.      "Delimiter

*
*----------------------------------------------------------------------*
* macro for fm FLUSH
*----------------------------------------------------------------------*
DEFINE M_MESSAGE_U.
  CASE SY-SUBRC.
    WHEN 0.
    WHEN 1.
      MESSAGE S121(14) WITH 'FLUSH' SY-SUBRC.
      "function module   & error  SY-SUBRC=&
      STOP.
    WHEN OTHERS.
      IF SY-SUBRC <> 0.
        MESSAGE S121(14) WITH 'FLUSH' SY-SUBRC.
        "function module   & error : SY-SUBRC=&
        FREE OBJECT H_CELL.
        FREE OBJECT H_WORKBOOK.
        FREE OBJECT H_WORKBOOKS.
        FREE OBJECT H_EXCEL.
        STOP.
      ENDIF.
  ENDCASE.
END-OF-DEFINITION.

*----------------------------------------------------------------------*
* macro for 'POPUP_TO_CONFIRM'
*----------------------------------------------------------------------*
DEFINE M_POPUP_TO_CONFIRM.
  CALL FUNCTION 'POPUP_TO_CONFIRM'
    EXPORTING
      TITLEBAR                    = 'Confirm'
*     DIAGNOSE_OBJECT             = ' '
      TEXT_QUESTION               = &1
      TEXT_BUTTON_1               = COS_YES
*     ICON_BUTTON_1               = ' '
      TEXT_BUTTON_2               = COS_NO
    IMPORTING
      ANSWER                      = W_ANSWER.
*
  IF W_ANSWER <> '1'.
    MESSAGE S398 WITH 'Canceled'.
    STOP.
  ENDIF.

END-OF-DEFINITION.

*Security check1  SY-UNAME
DEFINE M_AUTH_CHK.
*
 IF SY-UNAME <> 'xxxxx'.
    MESSAGE E398(00) WITH
            'You do not have authorization!'.
    EXIT.
 ENDIF.
END-OF-DEFINITION.

*Security check2  SY-DATUM
DEFINE M_AUTH_CHK2.
*
 IF SY-DATUM > '20070401'.
     MESSAGE E398(00) WITH
           'You do not have authorization!'.
 ENDIF.
*
END-OF-DEFINITION.


*----------------------------------------------------------------------*
* PARAMETERS, SELECTION-OPTIONS
*----------------------------------------------------------------------*
* Download to local(PC)  or App.server
SELECTION-SCREEN BEGIN OF BLOCK BL6 WITH FRAME TITLE TEXT-017.
* local(PC)
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(11) TEXT-015 FOR FIELD P_LCPC.
SELECTION-SCREEN POSITION 23.
PARAMETERS P_LCPC RADIOBUTTON GROUP R4 USER-COMMAND P DEFAULT 'X'.
SELECTION-SCREEN COMMENT 35(22) TEXT-018 FOR FIELD P_FILE.
SELECTION-SCREEN POSITION 58.
PARAMETERS P_FILE LIKE RLGRAP-FILENAME MODIF ID APS MEMORY ID GR8.
SELECTION-SCREEN END OF LINE.

* Server file
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(10) TEXT-016 FOR FIELD P_APSV.
SELECTION-SCREEN POSITION 23.
PARAMETERS P_APSV RADIOBUTTON GROUP R4.
SELECTION-SCREEN COMMENT 35(22) TEXT-019 FOR FIELD P_DIR.
SELECTION-SCREEN POSITION 58.
PARAMETERS P_DIR TYPE SDBA_DIR MODIF ID LPC MEMORY ID GR9.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BL6.

* Save as  Excel or Text(Tab)
SELECTION-SCREEN BEGIN OF BLOCK BL5 WITH FRAME TITLE TEXT-006.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(22) TEXT-007 FOR FIELD P_EXCEL.
SELECTION-SCREEN POSITION 23.

PARAMETERS P_EXCEL RADIOBUTTON GROUP R2 MODIF ID APS DEFAULT 'X'.
SELECTION-SCREEN COMMENT 35(22) TEXT-008 FOR FIELD P_TEXT.
SELECTION-SCREEN POSITION 58.

PARAMETERS P_TEXT RADIOBUTTON GROUP R2 MODIF ID APS .
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN COMMENT 1(70) P_COM1 MODIF ID COM.
SELECTION-SCREEN END OF BLOCK BL5.

* Choose table
SELECTION-SCREEN BEGIN OF BLOCK BL1 WITH FRAME TITLE TEXT-005.

* TABLE
SELECTION-SCREEN BEGIN OF BLOCK BL2 WITH FRAME TITLE TEXT-002.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(10) TEXT-009.
SELECTION-SCREEN POSITION 12.
PARAMETERS P_TAB TYPE TABNAME16 MEMORY ID DTB MODIF ID DNL.
SELECTION-SCREEN PUSHBUTTON 37(20) P_SEL USER-COMMAND ZSEL
                                         MODIF ID DNL.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BL2.
SELECTION-SCREEN SKIP.

*LANGUAGE
PARAMETERS:
       P_LANGU   LIKE DOKIL-LANGU  DEFAULT SY-LANGU.

SELECTION-SCREEN END OF BLOCK BL1.


*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
*
  M_AUTH_CHK.  "Delete if you don't need
  M_AUTH_CHK2. "Delete if you don't need

*
  P_SEL  = 'Dynamic selection'.
  P_COM1 = '(Excel cannot handle more than 65,534 records)'.
  P_LANGU = 'EN'.

*
  IMPORT IT_EXPRESS IT_FIELDS_TAB FROM MEMORY ID 'ZSEL'.
  REFRESH IT_EXCEL.
*
  IF SY-OPSYS = 'WINDOWS NT'.
    W_DELIMIT = '\'.  "<--backslash???
  ELSE.
    W_DELIMIT = '/'.
  ENDIF.
*


*----------------------------------------------------------------------*
* AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.

*  INTENSIFIED -remark for save as excel
    IF SCREEN-GROUP1 = 'COM'.
       SCREEN-INTENSIFIED = '1'.
       MODIFY SCREEN.
    ENDIF.
*
    IF P_APSV = 'X'.
      P_EXCEL = ' '.
      P_TEXT  = 'X'.
    ENDIF.

*
    CHECK ( SCREEN-GROUP1 = 'APS'  AND  P_APSV   = 'X' )
       OR ( SCREEN-GROUP1 = 'LPC'  AND  P_LCPC   = 'X' ).

    SCREEN-INPUT = 0.

    MODIFY SCREEN.

  ENDLOOP.

*
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
*
  CASE SSCRFIELDS-UCOMM.
    WHEN 'ONLI'.
      PERFORM SELSCR_ONLI.

      PERFORM DOWNLOAD_COMMON_PROCESS.

    WHEN 'ZSEL'.
      PERFORM SELSCR_ONLI.

      PERFORM SELSCR_ZSEL.
  ENDCASE.

*----------------------------------------------------------------------*
*  Explore PC file
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
*
  CHECK P_LCPC = 'X'.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
*     PROGRAM_NAME        = SYST-CPROG
*     DYNPRO_NUMBER       = SYST-DYNNR
      FIELD_NAME          = 'P_FILE'
    IMPORTING
      FILE_NAME           = P_FILE.

*----------------------------------------------------------------------*
*  Explore server file
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_DIR.
*
  PERFORM HELP_SERVER_FILE.

*
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*
  IF SY-BATCH = 'X'.
     MESSAGE S001 WITH 'This program  doesnot run in background'.
     STOP.
  ENDIF.

* Does Pc file exist?
  IF P_LCPC = 'X' AND  P_FILE <> SPACE.
     PERFORM CHECK_WRITE_PCFILE_EXIST.
  ENDIF.

* Does server file exist?
  IF P_APSV = 'X'  AND  P_DIR  <> SPACE.
     PERFORM CHECK_WRITE_SVRFILE_EXIST.
  ENDIF.
*
  PERFORM SELECT_DD02T.  "table texts

*
  CASE 'X'.
    WHEN P_LCPC.      " Local PC
      PERFORM DOWNLOAD_TO_LOCAL.

    WHEN P_APSV.      " SERVER FILE
      PERFORM DOWNLOAD_TO_SERVER.

  ENDCASE.
*


**********
* FORMS  *
**********
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_TO_LOCAL
*&---------------------------------------------------------------------*
*    Download table contents to Pc file or Excel
*----------------------------------------------------------------------*
FORM DOWNLOAD_TO_LOCAL.
*
  PERFORM SELECT_DD02T. "table texts

*
  PERFORM DOWNLOAD_COMMON_PROCESS_2.

* Pc file or Excel
  CASE 'X'.
    WHEN P_TEXT.
         PERFORM DOWNLOAD_BY_TEXT.
    WHEN P_EXCEL.
         PERFORM DOWNLOAD_BY_EXCEL.
  ENDCASE.
*
  MESSAGE S398(00) WITH 'Data downloaded successfully  to  '
                         W_FILEPATH
                        'Number of records is'
                        W_DBCNT.
*
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  SELECT_DD02T
*&---------------------------------------------------------------------*
*    Read   table texts
*----------------------------------------------------------------------*
FORM SELECT_DD02T .
*
  CLEAR W_DD02T-DDTEXT.
*
  SELECT SINGLE DDTEXT
         INTO W_DD02T-DDTEXT
         FROM DD02T
*
    WHERE TABNAME    = W_TABNAME
     AND  DDLANGUAGE = P_LANGU
     AND AS4LOCAL    = COS_LOCAL
     AND AS4VERS     = COS_VERS.

*
ENDFORM.


*&---------------------------------------------------------------------*
*       Save as text(Tab)
*----------------------------------------------------------------------*
FORM DOWNLOAD_BY_TEXT .

* Edit header
  PERFORM SET_TEXT_HEADER.

* Edit data
  PERFORM CONVERT_DATA_TO_FILE.

* Download table contents to Pc local file
  PERFORM DOWNLOAD_TABLE_DATA.

ENDFORM.


*&---------------------------------------------------------------------*
*&   Form  DOWNLOAD_TO_SERVER
*&---------------------------------------------------------------------*
*    Download table contents to server file
*----------------------------------------------------------------------*
FORM DOWNLOAD_TO_SERVER .
*
  PERFORM DOWNLOAD_COMMON_PROCESS_2.

* Open file
  OPEN DATASET P_DIR FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
  IF SY-SUBRC <> 0.
    MESSAGE S398 WITH 'FILE COULD NOT BE OPENED' P_DIR '' ''.
    STOP.
  ENDIF.

* Edit header
  PERFORM SET_TEXT_HEADER.

* Edit table contents
  PERFORM CONVERT_DATA_TO_FILE.

* Write table contents to file
  LOOP AT IT_EXCEL INTO W_EXCEL.
    TRANSFER W_EXCEL TO P_DIR.
  ENDLOOP.

* Close file
  CLOSE DATASET P_DIR.
*
  MESSAGE S398(00) WITH 'Data downloaded successfully  to  '
                         P_DIR
                        'Number of records is'
                        W_DBCNT.

*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  ASSIGN_TABF
*&---------------------------------------------------------------------*
*       Interface to read text on tables
*----------------------------------------------------------------------*
FORM ASSIGN_TAB.
* Create internal table
  CREATE DATA REF_LINE TYPE (W_TABNAME).
  ASSIGN REF_LINE->* TO <FS_LINE>.

*
  CREATE DATA REF_TAB LIKE STANDARD TABLE OF <FS_LINE>.
  ASSIGN REF_TAB->* TO <FS_TAB>.
*
ENDFORM.
*

*&---------------------------------------------------------------------*
*&      Form  FM_DDIF_FIELDINFO_GET
*&---------------------------------------------------------------------*
*       DOWNLOAD_COMMON_PROCESS
*----------------------------------------------------------------------*
FORM DOWNLOAD_COMMON_PROCESS .

* Create internal table
  PERFORM ASSIGN_TAB.

* Read table contents
  PERFORM TABLE_SELECT.

ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_COMMON_PROCESS
*&---------------------------------------------------------------------*
*       Confirm for download
*----------------------------------------------------------------------*
FORM DOWNLOAD_COMMON_PROCESS_2 .

* Confirm for download
  DESCRIBE TABLE <FS_TAB> LINES W_TABLE_LINES.
*
  WRITE W_TABLE_LINES TO W_TBL_LN NO-GAP.

  CONCATENATE 'Download?'
              P_TAB
              '-'
              W_DD02T-DDTEXT
              '(records is '
              W_TBL_LN
         INTO W_QUESTION.
*
  CONDENSE W_QUESTION NO-GAPS.
*
  M_POPUP_TO_CONFIRM W_QUESTION.
*
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  TABLE_SELECT
*&---------------------------------------------------------------------*
*    Read data from table
*----------------------------------------------------------------------*
FORM TABLE_SELECT .
*
  DELETE IT_WHERE WHERE TABLENAME <> P_TAB.

* Generate WHERE clause
  LOOP AT IT_WHERE INTO W_WHERE_CLS.
    LOOP AT W_WHERE_CLS-WHERE_TAB INTO W_WHERE.
      CONCATENATE W_TOTAL_WHERE W_WHERE INTO W_TOTAL_WHERE
                  SEPARATED BY SPACE.
    ENDLOOP.
  ENDLOOP.

  IF SY-SUBRC = 0.
     EXPORT IT_EXPRESS IT_FIELDS_TAB TO MEMORY ID 'ZSEL'.
  ELSE.
    FREE MEMORY ID  'ZSEL'.
  ENDIF.

* Read data from table
  TRY.
      SELECT * FROM (P_TAB) INTO TABLE <FS_TAB>
          WHERE (W_TOTAL_WHERE)
          ORDER BY PRIMARY KEY.

    CATCH CX_ROOT.
      MESSAGE S398 WITH 'Table' P_TAB
              'Error occured during selection of data'.
      STOP.
  ENDTRY.

* No hits
  IF  SY-DBCNT = 0.
      MESSAGE S398 WITH
      'No data selected for this search criteria' .
      EXIT.
  ENDIF.
*
  W_DBCNT  =  SY-DBCNT.

* EXCEL 2003 rows limit  check
*   EXCEL 2003 can handle only 65536 rows,
*   output data has 3 header lines,
*   so data must be within 65534.
*(In the futrue,this code will not be needed)
DATA  W_DBCNT2  LIKE SY-DBCNT.

  CHECK P_EXCEL = 'X'.

   W_DBCNT2 =  W_DBCNT + 3.
*
  CHECK W_DBCNT2 >= COS_MAXLINE.
*
  MESSAGE S001 WITH
    'You cannnot save data as Excel'
    'since output data has more than 65,534 records'.
  STOP.
*
ENDFORM.



*&---------------------------------------------------------------------*
*&   Form  DOWNLOAD_BY_EXCEL
*&---------------------------------------------------------------------*
*    Download as Excel
*----------------------------------------------------------------------*
FORM DOWNLOAD_BY_EXCEL .

* Open excel file
  PERFORM INIT_EXCEL_FILE.

* Edit second,third column
  PERFORM SET_FIELDNAME_HEADER.

* Get max colum
  W_COL_END = W_COL.
  DESCRIBE TABLE <FS_TAB> LINES W_ROW_END.
  ADD 3 TO W_ROW_END.

* Edit table data for output text
  PERFORM CONVERT_DATA_TO_FILE.

* Set format for colums,rows
  PERFORM SET_FORMAT.

* Write internal table to Excel
  PERFORM WRITE_DATA_TO_EXCEL.

*
  GET PROPERTY OF H_WORKSHEET COS_COLUMNS = H_RANGE  NO FLUSH.
  CALL METHOD OF H_RANGE COS_AUTOFIT NO FLUSH.
  FREE OBJECT H_RANGE  NO FLUSH.

* Edit first colum
  PERFORM SET_TABLENAME_HEADER.

* Fix window
  W_COL = W_COL_KEY + 1.
  CALL METHOD OF H_WORKSHEET COS_CELLS = H_RANGE NO FLUSH
    EXPORTING
      #1 = 4
      #2 = W_COL.

  CALL METHOD OF H_RANGE COS_SELECT NO FLUSH.
  FREE OBJECT H_RANGE  NO FLUSH.
  CALL METHOD OF H_WORKBOOK COS_WINDOWS = H_WINDOW NO FLUSH
    EXPORTING
      #1 = 1.
*
  SET PROPERTY OF H_WINDOW COS_FREEZEPANES = 1  NO FLUSH.
  FREE OBJECT H_WINDOW  NO FLUSH.

* xlR1C1  -->xlA1
  SET PROPERTY OF H_EXCEL COS_REFSTYLE = COS_XLSTYLE_A1 NO FLUSH.

* Save
  PERFORM SAVE_FILE.
*
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  WRITE_DATA_TO_EXCEL
*&---------------------------------------------------------------------*
*    Write internal table to Excel
*----------------------------------------------------------------------*
FORM WRITE_DATA_TO_EXCEL .
*
  CHECK IT_EXCEL[] IS NOT INITIAL.

* Write internal table to Excel
  DO.
    W_ROW_TOP = ( SY-INDEX - 1 ) * COS_WRITE_ROWS + 4.

    IF W_ROW_TOP > W_ROW_END.
      RETURN.
    ENDIF.

    CALL METHOD OF H_WORKSHEET COS_CELLS = H_RANGE NO FLUSH
      EXPORTING
        #1 = W_ROW_TOP
        #2 = 2.

    REFRESH: IT_EXCEL_PART.

    W_LOOP_START = ( SY-INDEX - 1 ) * COS_WRITE_ROWS + 1.
    W_LOOP_END = SY-INDEX *  COS_WRITE_ROWS.

*   Export to clipboard
    LOOP AT IT_EXCEL INTO W_EXCEL FROM W_LOOP_START TO W_LOOP_END.
      APPEND W_EXCEL TO IT_EXCEL_PART.
    ENDLOOP.
    CALL METHOD CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_EXPORT
      IMPORTING
        DATA                 = IT_EXCEL_PART[]
      CHANGING
        RC                   = W_CLIPBOARD
      EXCEPTIONS
        CNTL_ERROR           = 1
        ERROR_NO_GUI         = 2
        NOT_SUPPORTED_BY_GUI = 3
        OTHERS               = 4.
    IF SY-SUBRC <> 0.
      CALL METHOD OF H_WORKBOOK COS_CLOSE
        EXPORTING
          #1 = COS_FALSE.
      FREE OBJECT H_RANGE.
      FREE OBJECT H_WORKSHEET.
      FREE OBJECT H_WORKBOOKS.
      FREE OBJECT H_WORKBOOK.
      FREE OBJECT H_EXCEL.
      MESSAGE S398 WITH 'failed to export to clipboard'
                        'SY-SUBRC=' SY-SUBRC.
      STOP.
    ENDIF.

*   PasteE
    CALL METHOD OF H_WORKSHEET COS_PASTE NO FLUSH
      EXPORTING
        #1 = H_RANGE.

    FREE OBJECT H_RANGE  NO FLUSH.

  ENDDO.

ENDFORM.

*---------------------------------------------------------------------*
*&   Form  CONVERT_DATA_TO_FILE
*&---------------------------------------------------------------------*
*    Edit table data for output text
*----------------------------------------------------------------------*
FORM CONVERT_DATA_TO_FILE .
* Edit data
  LOOP AT <FS_TAB> ASSIGNING <FS_LINE>.

    W_ROW = SY-TABIX + 3.
    W_COL = 1.

    CLEAR W_EXCEL.

    LOOP AT IT_DFIES INTO W_DFIES WHERE DATATYPE <> 'CLNT'.

      ASSIGN COMPONENT W_DFIES-FIELDNAME
             OF STRUCTURE <FS_LINE> TO <FS_FIELD>.

      CASE W_DFIES-DATATYPE.
        WHEN 'CURR' OR 'QUAN' OR 'DEC'
          OR 'INT1' OR 'INT2' OR 'INT4'.  " numeric
          WRITE <FS_FIELD> TO W_VALUE.
          SHIFT W_VALUE RIGHT CIRCULAR.
          CONDENSE W_VALUE NO-GAPS.

        WHEN 'DATS'.  " date
          CONCATENATE <FS_FIELD>(4) <FS_FIELD>+4(2) <FS_FIELD>+6(2)
                      INTO W_VALUE SEPARATED BY '/'.

        WHEN OTHERS.  " OTHERS
          WRITE <FS_FIELD> TO W_VALUE.
      ENDCASE.

      IF W_COL = 1.
        W_EXCEL = W_VALUE.
      ELSE.
        CONCATENATE W_EXCEL W_VALUE INTO W_EXCEL SEPARATED BY COS_TAB.
      ENDIF.

      ADD 1 TO W_COL.

    ENDLOOP.
*
    IF P_TEXT = 'X'.
      CONCATENATE COS_TAB W_EXCEL INTO W_EXCEL.
    ENDIF.

    APPEND W_EXCEL TO IT_EXCEL.

  ENDLOOP.

ENDFORM.

*---------------------------------------------------------------------*
*&   Form  SET_TEXT_HEADER
*&---------------------------------------------------------------------*
*    Set header for texst
*----------------------------------------------------------------------*
FORM SET_TEXT_HEADER .
* first line
  CONCATENATE '* TABLE-' W_TABNAME INTO W_HEAD1 SEPARATED BY SPACE.
*
  CONCATENATE W_TABNAME '-' W_DD02T-DDTEXT INTO W_HEAD2.
  CONCATENATE W_HEAD1 W_HEAD2 INTO W_XLHEAD SEPARATED BY COS_TAB.
  APPEND W_XLHEAD TO IT_EXCEL.

* second,third
  CONCATENATE COS_TAB W_XLHEAD2 INTO W_EXCEL.
  CONCATENATE COS_TAB W_XLHEAD3 INTO W_EXCEL.

  LOOP AT IT_DFIES INTO W_DFIES.

    CHECK W_DFIES-DATATYPE <> 'CLNT'.
    CONCATENATE W_XLHEAD2 COS_TAB W_DFIES-FIELDNAME INTO W_XLHEAD2.
    IF W_DFIES-SCRTEXT_M <> SPACE.
      CONCATENATE W_XLHEAD3 COS_TAB W_DFIES-SCRTEXT_M INTO W_XLHEAD3.
    ELSE.
      CONCATENATE W_XLHEAD3 COS_TAB W_DFIES-FIELDTEXT INTO W_XLHEAD3.
    ENDIF.

  ENDLOOP.
*
  APPEND W_XLHEAD2 TO IT_EXCEL.
  APPEND W_XLHEAD3 TO IT_EXCEL.
*
ENDFORM.

*---------------------------------------------------------------------*
*&      Form  DOWNLOAD_TABLE_DATA
*&---------------------------------------------------------------------*
*    Download table contents to Pc local file
*----------------------------------------------------------------------*
FORM DOWNLOAD_TABLE_DATA .
*
  CLEAR: F_APPEND.

* count internal table lines
  DESCRIBE TABLE IT_EXCEL LINES W_TABLE_LINES.
*
  DO.
    W_LOOP_START = ( SY-INDEX - 1 ) * COS_DWNLOAD_SIZE + 1.
    W_LOOP_END = SY-INDEX * COS_DWNLOAD_SIZE.

    REFRESH IT_EXCEL_PART.

    LOOP AT IT_EXCEL INTO W_EXCEL FROM W_LOOP_START TO W_LOOP_END.

      APPEND W_EXCEL TO IT_EXCEL_PART.

    ENDLOOP.

*
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
*       BIN_FILESIZE                  =
        FILENAME                      = W_FILEPATH
        FILETYPE                      = 'ASC'
        APPEND                        = F_APPEND
*       WRITE_FIELD_SEPARATOR         = ' '
*       HEADER                        = '00'
*       TRUNC_TRAILING_BLANKS         = ' '
*       WRITE_LF                      = 'X'
*       COL_SELECT                    = ' '
*       COL_SELECT_MASK               = ' '
*       DAT_MODE                      = ' '
*       CONFIRM_OVERWRITE             = ' '
*       NO_AUTH_CHECK                 = ' '
*       CODEPAGE                      = ' '
*       IGNORE_CERR                   = ABAP_TRUE
*       REPLACEMENT                   = '#'
*       WRITE_BOM                     = ' '
*     IMPORTING
*       FILELENGTH                    =
      TABLES
        DATA_TAB                      = IT_EXCEL_PART
      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 S121(14) WITH 'GUI_DOWNLOAD' SY-SUBRC.
      STOP.
    ENDIF.

    F_APPEND = 'X'.

    IF W_TABLE_LINES <= W_LOOP_END.
      EXIT.
    ENDIF.

  ENDDO.
*

*
ENDFORM.

*---------------------------------------------------------------------*
*&   Form  INIT_EXCEL_FILE
*&---------------------------------------------------------------------*
*    Open excel file
*----------------------------------------------------------------------*
FORM INIT_EXCEL_FILE .
* File open
  CREATE OBJECT H_EXCEL COS_XLAPPLI NO FLUSH.
  SET PROPERTY OF H_EXCEL COS_VISIBLE = 0  NO FLUSH.
  SET PROPERTY OF H_EXCEL COS_ALERTS = 0  NO FLUSH.
  SET PROPERTY OF H_EXCEL COS_REFSTYLE = COS_XLSTYLE_R1C1 NO FLUSH.
  SET PROPERTY OF H_EXCEL COS_NEWWORKBOOK = 1  NO FLUSH.
*
  CALL METHOD OF H_EXCEL COS_WORKBOOK = H_WORKBOOKS NO FLUSH.
  CALL METHOD OF H_WORKBOOKS COS_ADD = H_WORKBOOK NO FLUSH.
  GET PROPERTY OF H_WORKBOOK COS_ACTSHEET = H_WORKSHEET  NO FLUSH.
  SET PROPERTY OF H_WORKSHEET COS_NAME = W_TABNAME  NO FLUSH.

ENDFORM.

*---------------------------------------------------------------------*
*&   Form  FILL_CELL
*&---------------------------------------------------------------------*
*    Set data to cell
*----------------------------------------------------------------------*
FORM FILL_CELL USING    FP_ROW   TYPE I
                        FP_COL   TYPE I
                        FP_VALUE TYPE ANY.
* Set value
  CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL NO FLUSH
    EXPORTING
      #1 = FP_ROW
      #2 = FP_COL.
*
  SET PROPERTY OF H_CELL COS_VALUE = FP_VALUE  NO FLUSH.
  GET PROPERTY OF H_CELL COS_FONT = H_FONT  NO FLUSH.
  SET PROPERTY OF H_FONT COS_BOLD = W_BOLD  NO FLUSH.
  FREE OBJECT H_FONT  NO FLUSH.
*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_FIELDNAME_HEADER
*&---------------------------------------------------------------------*
*    Second,third ros (header for field name)
*----------------------------------------------------------------------*
FORM SET_FIELDNAME_HEADER .
*
  W_COL = 1.

  LOOP AT IT_DFIES INTO W_DFIES.
    CHECK W_DFIES-DATATYPE <> 'CLNT'.
    ADD 1 TO W_COL.
    W_BOLD = 0.
    PERFORM FILL_CELL USING 2 W_COL W_DFIES-FIELDNAME.

    FREE OBJECT H_CELL  NO FLUSH.
    W_BOLD = 1.
    IF W_DFIES-SCRTEXT_M <> SPACE.
      PERFORM FILL_CELL USING 3 W_COL W_DFIES-SCRTEXT_M.
    ELSE.
      PERFORM FILL_CELL USING 3 W_COL W_DFIES-FIELDTEXT.
    ENDIF.

    FREE OBJECT H_CELL  NO FLUSH.
  ENDLOOP.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_TABLENAME_HEADERF
*&---------------------------------------------------------------------*
*    First row (header for table name)
*----------------------------------------------------------------------*
FORM SET_TABLENAME_HEADER .

* first row
  CONCATENATE '* TABLE:' W_TABNAME INTO W_HEAD1 SEPARATED BY SPACE.
  PERFORM FILL_CELL USING 1 1 W_HEAD1.
  PERFORM SET_VALIDATION_HEADER USING W_HEAD1.
  SET PROPERTY OF H_CELL COS_COLUMNWIDTH = 1.
  FREE OBJECT H_CELL  NO FLUSH.
*
  CONCATENATE W_TABNAME '-' W_DD02T-DDTEXT
              INTO W_HEAD2.
*
  W_BOLD = 1.
  PERFORM FILL_CELL USING 1 2 W_HEAD2.

  FREE OBJECT H_CELL  NO FLUSH.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_FORMAT
*&---------------------------------------------------------------------*
*     Set format for colums,rows
*----------------------------------------------------------------------*
FORM SET_FORMAT.
*
  W_COL = 1.

*
  LOOP AT IT_DFIES INTO W_DFIES.
    CHECK W_DFIES-DATATYPE <> 'CLNT'.
    ADD 1 TO W_COL.
    CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL NO FLUSH
      EXPORTING
        #1 = 4
        #2 = W_COL.
    CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL1 NO FLUSH
      EXPORTING
        #1 = W_ROW_END
        #2 = W_COL.
    CALL METHOD OF H_WORKSHEET COS_RANGE = H_RANGE NO FLUSH
      EXPORTING
        #1 = H_CELL
        #2 = H_CELL1.

    FREE OBJECT:H_CELL  NO FLUSH,
                H_CELL1 NO FLUSH.
*
    PERFORM SET_NUMBERFORMAT.

*
    PERFORM SET_VALIDATION_ITEM.

*
    IF W_DFIES-KEYFLAG = 'X'.
      W_COL_KEY = W_COL.
    ENDIF.
    FREE OBJECT H_RANGE  NO FLUSH.
  ENDLOOP.

*
  PERFORM SET_LINE.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_NUMBERFORMAT
*&---------------------------------------------------------------------*
*    Set format for cell
*----------------------------------------------------------------------*
FORM SET_NUMBERFORMAT.
*
  CASE W_DFIES-DATATYPE.
    WHEN 'CHAR' OR 'UNIT' OR 'LANG'.
      W_NUMFORMAT = '@'.
      SET PROPERTY OF H_RANGE COS_ALIGNMENT = COS_XLHALIGNLEFT
          NO FLUSH.
    WHEN 'DATS'.
      W_NUMFORMAT = 'yyyy/mm/dd'.
      SET PROPERTY OF H_RANGE COS_ALIGNMENT = COS_XLHALIGNLEFT
          NO FLUSH.
    WHEN 'TIMS'.
      W_NUMFORMAT = 'hh:mm:ss'.
      SET PROPERTY OF H_RANGE COS_ALIGNMENT = COS_XLHALIGNLEFT
          NO FLUSH.
    WHEN 'CURR' OR 'QUAN' OR 'DEC'  OR 'NUMC'
                OR 'INT1' OR 'INT2' OR 'INT4'.
      IF W_DFIES-DECIMALS = 0.
        W_NUMFORMAT = '#,##0'.
      ELSE.
        W_NUMFORMAT = '#,##0.'.
        DO W_DFIES-DECIMALS TIMES.
          CONCATENATE W_NUMFORMAT '0' INTO W_NUMFORMAT.
        ENDDO.
      ENDIF.
    WHEN OTHERS.
      EXIT.
  ENDCASE.
*
  SET PROPERTY OF H_RANGE COS_NUMFORMAT = W_NUMFORMAT  NO FLUSH.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_VALIDATION_ITEM
*&---------------------------------------------------------------------*
*    Set input format to cell
*----------------------------------------------------------------------*
FORM SET_VALIDATION_ITEM.

* Set input format for data type
  CASE W_DFIES-DATATYPE.
    WHEN 'CHAR' OR 'UNIT' OR 'LANG'.
      W_TYPE = COS_XLVALIDCSTM.
      CONCATENATE '=AND(LENB(RC)<=' W_DFIES-INTLEN ',TYPE(RC)=2)'
                 INTO W_FORMULA1.
      CLEAR W_FORMULA2.
      WRITE W_DFIES-INTLEN TO W_TEXT NO-ZERO.
      CONDENSE W_TEXT NO-GAPS.
      CONCATENATE 'length is' W_TEXT 'bytes over'
                 INTO W_ERRORMSG.

    WHEN 'CURR' OR 'QUAN' OR 'DEC'.
      W_TYPE = COS_XLVALIDDEC.
      CLEAR W_FORMULA2.
      CLEAR W_FORMULA2(W_DFIES-LENG) WITH '9'.
      IF W_DFIES-DECIMALS <> 0.
        W_DCML_OFFSET = W_DFIES-LENG - W_DFIES-DECIMALS.
        CONCATENATE '.' W_FORMULA2+W_DCML_OFFSET
               INTO W_FORMULA2+W_DCML_OFFSET.
      ENDIF.
      CONCATENATE '-' W_FORMULA2  INTO W_FORMULA1.
      CONCATENATE 'input between'  W_FORMULA1 '~' W_FORMULA2
                  INTO W_ERRORMSG.

    WHEN 'NUMC'.
      W_TYPE = COS_XLVALIDNUM.
      W_FORMULA1 = '0'.
      CLEAR W_FORMULA2.
      CLEAR W_FORMULA2(W_DFIES-INTLEN) WITH '9'.
      WRITE W_DFIES-INTLEN TO W_TEXT NO-ZERO.
      CONDENSE W_TEXT NO-GAPS.
      CONCATENATE 'Input ' W_TEXT 'bytes numeric'
            INTO W_ERRORMSG.

    WHEN 'INT1'.
      W_TYPE = COS_XLVALIDNUM.
      W_FORMULA1 = '0'.
      W_FORMULA2 = '255'.
      W_ERRORMSG = 'Input between 0~255 numeric'.

    WHEN 'INT2'.
      W_TYPE = COS_XLVALIDNUM.
      W_FORMULA1 = '-32768'.
      W_FORMULA2 = '32767'.
      W_ERRORMSG = 'Input between -32768~32767 numeric'.

    WHEN 'INT4'.
      W_TYPE = COS_XLVALIDNUM.
      W_FORMULA1 = '-2147483648'.
      W_FORMULA2 = '2147483647'.
      W_ERRORMSG =
        'Input between -2147483648~2147483647 numeric'.

    WHEN OTHERS.
      EXIT.
  ENDCASE.

* set input format
  GET PROPERTY OF H_RANGE COS_VALIDATION = H_VALIDATION  NO FLUSH.

  CALL METHOD OF H_VALIDATION COS_ADD NO FLUSH
    EXPORTING
      #1 = W_TYPE
      #2 = COS_XLVALIDSTOP
      #3 = COS_XLBETWEEN
      #4 = W_FORMULA1
      #5 = W_FORMULA2.
  SET PROPERTY OF H_VALIDATION COS_IGNOREBLANK = 1  NO FLUSH.
  SET PROPERTY OF H_VALIDATION COS_ERRMESSAGE = W_ERRORMSG
      NO FLUSH.

  FREE OBJECT H_VALIDATION  NO FLUSH.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_LINE
*&---------------------------------------------------------------------*
*    Set lines for display
*----------------------------------------------------------------------*
FORM SET_LINE.

* outer line/inner line
  CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL NO FLUSH
    EXPORTING
      #1 = 3
      #2 = 2.
  CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL1 NO FLUSH
    EXPORTING
      #1 = W_ROW_END
      #2 = W_COL_END.
  CALL METHOD OF H_WORKSHEET COS_RANGE = H_RANGE NO FLUSH
    EXPORTING
      #1 = H_CELL
      #2 = H_CELL1.
  FREE OBJECT:H_CELL  NO FLUSH,
              H_CELL1 NO FLUSH.
  GET PROPERTY OF H_RANGE COS_BORDERS = H_BORDER  NO FLUSH.
  SET PROPERTY OF H_BORDER COS_LINESTYLE = COS_XLSTYLE  NO FLUSH.
  FREE OBJECT H_BORDER  NO FLUSH.
  CALL METHOD OF H_RANGE COS_BORDERARND NO FLUSH
    EXPORTING
      #1 = COS_XLSTYLE
      #2 = COS_XLMEDIUM.
  FREE OBJECT H_RANGE  NO FLUSH.

* header
  CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL NO FLUSH
    EXPORTING
      #1 = 3
      #2 = 2.
  CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL1 NO FLUSH
    EXPORTING
      #1 = 3
      #2 = W_COL_END.
  CALL METHOD OF H_WORKSHEET COS_RANGE = H_RANGE NO FLUSH
    EXPORTING
      #1 = H_CELL
      #2 = H_CELL1.
  FREE OBJECT:H_CELL  NO FLUSH,
              H_CELL1 NO FLUSH.
  CALL METHOD OF H_RANGE COS_BORDERS = H_BORDER NO FLUSH
    EXPORTING
      #1 = COS_XLBOTTOM.
*
  SET PROPERTY OF H_BORDER COS_LINESTYLE = COS_XLDOUBLE  NO FLUSH.
  FREE OBJECT H_BORDER  NO FLUSH.
  GET PROPERTY OF H_RANGE COS_INTERIOR = H_INTERIOR  NO FLUSH.
  FREE OBJECT H_RANGE  NO FLUSH.
  SET PROPERTY OF H_INTERIOR COS_COLORINDEX = COS_XLCOLOR_HEAD
      NO FLUSH.
  FREE OBJECT H_INTERIOR  NO FLUSH.

* key
  IF W_ROW_END >= 4.
    CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL NO FLUSH
      EXPORTING
        #1 = 4
        #2 = 2.

    CALL METHOD OF H_WORKSHEET COS_CELLS = H_CELL1 NO FLUSH
      EXPORTING
        #1 = W_ROW_END
        #2 = W_COL_KEY.

    CALL METHOD OF H_WORKSHEET COS_RANGE = H_RANGE NO FLUSH
      EXPORTING
        #1 = H_CELL
        #2 = H_CELL1.
*
    FREE OBJECT:H_CELL  NO FLUSH,
                H_CELL1 NO FLUSH.
    GET PROPERTY OF H_RANGE COS_INTERIOR = H_INTERIOR  NO FLUSH.
    FREE OBJECT H_RANGE  NO FLUSH.
    SET PROPERTY OF H_INTERIOR COS_COLORINDEX = COS_XLCOLOR_KEY
        NO FLUSH.
    FREE OBJECT H_INTERIOR  NO FLUSH.
  ENDIF.
*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SET_VALIDATION_HEADER
*&---------------------------------------------------------------------*
*       excel header input
*----------------------------------------------------------------------*
FORM SET_VALIDATION_HEADER USING FP_VALUE TYPE ANY.
*
  GET PROPERTY OF H_CELL COS_VALIDATION = H_VALIDATION  NO FLUSH.
  CALL METHOD OF H_VALIDATION 'Add' NO FLUSH
    EXPORTING
      #1 = COS_XLVALIDLIST
      #2 = COS_XLVALIDSTOP
      #3 = COS_XLEQUAL
      #4 = FP_VALUE.
*
  SET PROPERTY OF H_VALIDATION COS_IGNOREBLANK = 0  NO FLUSH.
  SET PROPERTY OF H_VALIDATION COS_DROPDOWN = 0  NO FLUSH.
  SET PROPERTY OF H_VALIDATION COS_ERRMESSAGE =
                  'cannot header filed' NO FLUSH.
  FREE OBJECT H_VALIDATION  NO FLUSH.
*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SAVE_FILE
*&---------------------------------------------------------------------*
*    Save as EXCEL file
*----------------------------------------------------------------------*
FORM SAVE_FILE .
*
  CALL FUNCTION 'FLUSH'
    EXCEPTIONS
      CNTL_SYSTEM_ERROR = 1
      CNTL_ERROR        = 2
      OTHERS            = 3.

  IF SY-SUBRC = 0.
*   save
    CALL METHOD OF H_WORKBOOK COS_SAVEAS
      EXPORTING
        #1 = P_FILE
        #2 = COS_XLWORKBOOK.
  ENDIF.

  IF SY-SUBRC <> 0.
    MESSAGE S001 WITH
       'Error occurred during download'.
  ENDIF.

  CALL METHOD OF H_WORKBOOK COS_CLOSE
    EXPORTING
      #1 = COS_FALSE.
*
  FREE OBJECT H_WORKSHEET.
  FREE OBJECT H_WORKBOOKS.
  FREE OBJECT H_WORKBOOK.
  FREE OBJECT H_EXCEL.
*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SELSCR_ONLI
*&---------------------------------------------------------------------*
*    Check  input
*----------------------------------------------------------------------*
FORM SELSCR_ONLI .

* Check  input file
  IF P_FILE = SPACE AND P_LCPC = 'X'.
    SET CURSOR FIELD 'P_FILE'.
    MESSAGE E001 WITH 'Enter a pc local file'.

  ELSEIF P_DIR = SPACE AND P_APSV = 'X'.
    SET CURSOR FIELD 'P_DIR'.
    MESSAGE E001 WITH 'Enter an app.svr file'.
  ENDIF.
*
  IF P_TAB = SPACE.
    SET CURSOR FIELD 'P_TAB'.
    MESSAGE E001 WITH 'Enter a Table name'.
  ENDIF.
*
  CONDENSE P_TAB NO-GAPS.
  W_TABNAME = P_TAB.

* Call fm 'DDIF_FIELDINFO_GET'
  PERFORM FM_DDIF_FIELDINFO_GET.
*
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  FM_DDIF_FIELDINFO_GET
*&---------------------------------------------------------------------*
*    Read table info
*----------------------------------------------------------------------*
FORM FM_DDIF_FIELDINFO_GET .
*
  CALL FUNCTION 'DDIF_FIELDINFO_GET'
    EXPORTING
      TABNAME              = W_TABNAME
*     FIELDNAME            = ' '
      LANGU                = P_LANGU
*     LFIELDNAME           = ' '
*     ALL_TYPES            = ' '
    IMPORTING
*     X030L_WA             =
      DDOBJTYPE            = W_DDOBJTYPE
*     DFIES_WA             =
*     LINES_DESCR          =
    TABLES
      DFIES_TAB            = IT_DFIES
*     FIXED_VALUES         =
    EXCEPTIONS
      NOT_FOUND            = 1
      INTERNAL_ERROR       = 2
      OTHERS               = 3.
*
  IF SY-SUBRC <> 0.
     SET CURSOR FIELD 'P_TAB'.
     MESSAGE E398 WITH 'Table' W_TABNAME ' does not exist'.

  ELSEIF W_DDOBJTYPE <> 'TRANSP'.
    SET CURSOR FIELD 'P_TAB'.
    MESSAGE E398 WITH 'Table' W_TABNAME
                      'is not a Transparent table'.
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*   For dynamic selection
*       Call  fm 'FREE_SELECTIONS_INIT'
*       Call  fm 'FREE_SELECTIONS_DIALOG'
*----------------------------------------------------------------------*
FORM SELSCR_ZSEL .
*
* initial check
  CLEAR F_INIT_FLG.
  READ TABLE IT_TABLES INTO W_TABLES_TAB INDEX 1.
  IF SY-SUBRC = 0.
    IF W_TABLES_TAB-PRIM_TAB <> P_TAB.
      F_INIT_FLG = 'X'.
      W_TABLES_TAB-PRIM_TAB = P_TAB.
      MODIFY IT_TABLES INDEX 1 FROM W_TABLES_TAB.
    ENDIF.
  ELSE.
    F_INIT_FLG = 'X'.
    W_TABLES_TAB-PRIM_TAB = P_TAB.
    APPEND W_TABLES_TAB TO IT_TABLES.
  ENDIF.

* Initialize dynamic selection
  IF F_INIT_FLG = 'X'.
     DELETE IT_EXPRESS WHERE TABLENAME <> P_TAB.
     DELETE IT_FIELDS_TAB  WHERE TABLENAME <> P_TAB.

*
    CALL FUNCTION 'FREE_SELECTIONS_INIT'
     EXPORTING
       KIND                           = 'T'
       EXPRESSIONS                    = IT_EXPRESS[]
*      FIELD_GROUPS_KEY               =
*      RESTRICTION                    =
*      ALV                            =
*      CURR_QUAN_PROG                 = SY-CPROG
*      CURR_QUAN_RELATION             =
     IMPORTING
       SELECTION_ID                   = W_SELECT_ID
*      WHERE_CLAUSES                  =
*      EXPRESSIONS                    =
*      FIELD_RANGES                   =
*      NUMBER_OF_ACTIVE_FIELDS        =
     TABLES
       TABLES_TAB                     = IT_TABLES
       FIELDS_TAB                     = IT_FIELDS_TAB
*      FIELD_DESC                     =
*      FIELD_TEXTS                    =
*      EVENTS                         =
*      EVENT_FIELDS                   =
*      FIELDS_NOT_SELECTED            =
     EXCEPTIONS
       FIELDS_INCOMPLETE              = 1
       FIELDS_NO_JOIN                 = 2
       FIELD_NOT_FOUND                = 3
       NO_TABLES                      = 4
       TABLE_NOT_FOUND                = 5
       EXPRESSION_NOT_SUPPORTED       = 6
       INCORRECT_EXPRESSION           = 7
       ILLEGAL_KIND                   = 8
       AREA_NOT_FOUND                 = 9
       INCONSISTENT_AREA              = 10
       KIND_F_NO_FIELDS_LEFT          = 11
       KIND_F_NO_FIELDS               = 12
       TOO_MANY_FIELDS                = 13
       DUP_FIELD                      = 14
       FIELD_NO_TYPE                  = 15
       FIELD_ILL_TYPE                 = 16
       DUP_EVENT_FIELD                = 17
       NODE_NOT_IN_LDB                = 18
       AREA_NO_FIELD                  = 19
       OTHERS                         = 20.
*
    IF SY-SUBRC <> 0.
      MESSAGE S121(14) WITH 'FREE_SELECTIONS_INIT' SY-SUBRC.
      STOP.
    ENDIF.
  ENDIF.

* Dialog for entering dynamic selection conditions
  CONCATENATE P_TAB 'selection' INTO W_DLG_TITLE SEPARATED BY SPACE.
*
  CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
    EXPORTING
      SELECTION_ID                  = W_SELECT_ID
      TITLE                         = W_DLG_TITLE
*     FRAME_TEXT                    = ' '
*     STATUS                        =
      AS_WINDOW                     = 'X'
*     START_ROW                     = 2
*     START_COL                     = 2
*     NO_INTERVALS                  = ' '
*     JUST_DISPLAY                  = ' '
*     PFKEY                         =
*     ALV                           = ' '
      TREE_VISIBLE                  = 'X'
*     DIAG_TEXT_1                   =
*     DIAG_TEXT_2                   =
*     WARNING_TITLE                 =
*     AS_SUBSCREEN                  = ' '
*     NO_FRAME                      =
    IMPORTING
      WHERE_CLAUSES                 = IT_WHERE[]
      EXPRESSIONS                   = IT_EXPRESS[]
*      FIELD_RANGES                  = T_FIELD_RANGES[]
*     NUMBER_OF_ACTIVE_FIELDS       =
    TABLES
      FIELDS_TAB                    = IT_FIELDS_TAB
*     FCODE_TAB                     =
*     FIELDS_NOT_SELECTED           =
    EXCEPTIONS
      INTERNAL_ERROR                = 1
      NO_ACTION                     = 2
      SELID_NOT_FOUND               = 3
      ILLEGAL_STATUS                = 4
      OTHERS                        = 5.
*
  IF SY-SUBRC <> 0  AND  SY-SUBRC <> 2.
    MESSAGE S121(14) WITH 'FREE_SELECTIONS_DIALOG' SY-SUBRC.
    STOP.
  ENDIF.

ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  CHECK_WRITE_PCFILE_EXIST
*&---------------------------------------------------------------------*
*       Check pcfile exists?
*----------------------------------------------------------------------*
FORM CHECK_WRITE_PCFILE_EXIST .
* Create filepath
  PERFORM CREATE_FILEPATH.

* Check pcfile exists
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
    EXPORTING
      FILE                 = W_FILEPATH
    RECEIVING
      RESULT               = W_RESULT
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      WRONG_PARAMETER      = 3
      NOT_SUPPORTED_BY_GUI = 4.
*
  IF SY-SUBRC <> 0.
    MESSAGE S398 WITH 'CL_GUI_FRONTEND_SERVICES=>FILE_EXIST error'
                      'SY-SUBRC=' SY-SUBRC.
    STOP.
  ENDIF.
*
  IF W_RESULT = ABAP_TRUE.    "????
    CONCATENATE P_FILE 'exists. Overwrite?'
        INTO W_QUESTION SEPARATED BY SPACE.
    M_POPUP_TO_CONFIRM W_QUESTION.
  ENDIF.
*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  CREATE_FILEPATH
*&---------------------------------------------------------------------*
*       Create filepath
*----------------------------------------------------------------------*
FORM CREATE_FILEPATH .
* File extention
  CASE 'X'.
    WHEN P_EXCEL.
      W_EXTENSION = COS_DOT_XLS.
    WHEN P_TEXT.
      W_EXTENSION = COS_DOT_TXT.
  ENDCASE.

* Create filepath
  W_FILEPATH = P_FILE.
  W_PATHLEN = STRLEN( W_FILEPATH ) - 5.
  IF W_PATHLEN > 0.
    IF W_FILEPATH+W_PATHLEN NA COS_PERIOD.
      CONCATENATE W_FILEPATH W_EXTENSION INTO W_FILEPATH.
    ENDIF.
  ELSE.
    IF W_FILEPATH NA COS_PATH.
      CREATE OBJECT H_EXCEL COS_XLAPPLI.
      GET PROPERTY OF H_EXCEL COS_DEFLTPATH = W_DEFLTPATH.
      FREE OBJECT H_EXCEL.
      IF W_FILEPATH CA COS_PERIOD.
        CONCATENATE W_DEFLTPATH COS_PATH W_FILEPATH INTO W_FILEPATH.
      ELSE.
        CONCATENATE W_DEFLTPATH COS_PATH W_FILEPATH W_EXTENSION
               INTO W_FILEPATH.
      ENDIF.
    ELSEIF W_FILEPATH NA COS_PERIOD.
      CONCATENATE W_FILEPATH W_EXTENSION INTO W_FILEPATH.
    ENDIF.
  ENDIF.
*
  P_FILE = W_FILEPATH.
*
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  CHECK_WRITE_SVRFILE_EXIST
*&---------------------------------------------------------------------*
*       Check server file exists?
*----------------------------------------------------------------------*
FORM CHECK_WRITE_SVRFILE_EXIST .
*
  OPEN DATASET P_DIR FOR INPUT IN TEXT MODE ENCODING DEFAULT.
*
  CHECK SY-SUBRC = 0.
*
  CLOSE DATASET P_DIR.

  CONCATENATE P_DIR 'already exists. - overwrite the file?'
              INTO W_QUESTION SEPARATED BY SPACE.
*
  M_POPUP_TO_CONFIRM W_QUESTION.
*
ENDFORM.

*----------------------------------------------------------------------*
*&      Form  HELP_SERVER_FILE
*&---------------------------------------------------------------------*
*    Explore server file  using pop-up list
*----------------------------------------------------------------------*
FORM HELP_SERVER_FILE.
*
  W_FIELD = 'P_DIR'.
*
  PERFORM DYNP_READ_FIELD USING W_FIELD   CHANGING W_PATHI.

  CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
        EXPORTING
             I_LOCATION_FLAG = 'A'       "UNIX
             I_SERVER        = W_SERVER_NAME
             I_PATH          = W_PATHI
*            filemask        = p_mask
             FILEOPERATION   = 'R'
        IMPORTING
*             o_location_flag = w_flag
*             o_server        = w_server
             O_PATH          = W_PATH
              ABEND_FLAG      = W_ABENDFLAG
        EXCEPTIONS
             RFC_ERROR       = 1
             OTHERS          = 2.

* May not work on 'NT' ?( Somebody test this code on NT!)
  IF SY-SUBRC <> 0.
      MESSAGE 'Select a valid server' TYPE 'E'.
  ELSE.
    IF W_PATH(1) = W_DELIMIT  AND
       W_PATH+1(1) = W_DELIMIT.
      SHIFT W_PATH LEFT.
    ENDIF.
*
    IF W_ABENDFLAG <> 'X'.
      P_DIR  = W_PATH.
    ENDIF.
  ENDIF.

ENDFORM.
*

*&---------------------------------------------------------------------*
*&      Form  DYNP_READ_FIELD
*&---------------------------------------------------------------------*
*    Read parameter  on server file
*----------------------------------------------------------------------*
FORM DYNP_READ_FIELD USING I_FIELD CHANGING O_VALUE.
*
  DATA: W_VALUE TYPE DYNPREAD-FIELDVALUE.
*
  CALL FUNCTION 'C14Z_DYNP_READ_FIELD'
       EXPORTING
            I_PROGRAM      = SY-CPROG
            I_DYNPRO       = SY-DYNNR
            I_FIELDNAME    = I_FIELD
            I_FLG_STEPLOOP = SPACE
       CHANGING
            E_VALUE        = W_VALUE.
*
  CHECK SY-SUBRC = 0.
  O_VALUE = W_VALUE.
*
ENDFORM.


*end-of-program----------------------*
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP Dictionary -> Migration All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.