martedì 19 gennaio 2016

Esportare un file di Excel complesso

*----------------------------------------------------------------------*
* DECLARATIONS                                                         *
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_out1,
         ltext  TYPE LTEXT,
         waers  TYPE WAERS_CURC,
         wday01 TYPE P DECIMALS 5,
         wday02 TYPE P DECIMALS 5,
         wday03 TYPE P DECIMALS 5,
         wday04 TYPE P DECIMALS 5,
         wday05 TYPE P DECIMALS 5,
         wday06 TYPE P DECIMALS 5,
         wday07 TYPE P DECIMALS 5,
         wday08 TYPE P DECIMALS 5,
         wday09 TYPE P DECIMALS 5,
         wday10 TYPE P DECIMALS 5,
         wday11 TYPE P DECIMALS 5,
         wday12 TYPE P DECIMALS 5,
         wday13 TYPE P DECIMALS 5,
         wday14 TYPE P DECIMALS 5,
         wday15 TYPE P DECIMALS 5,
         wday16 TYPE P DECIMALS 5,
         wday17 TYPE P DECIMALS 5,
         wday18 TYPE P DECIMALS 5,
         wday19 TYPE P DECIMALS 5,
         wday20 TYPE P DECIMALS 5,
         wday21 TYPE P DECIMALS 5,
         wday22 TYPE P DECIMALS 5,
         wday23 TYPE P DECIMALS 5,
         montha TYPE P DECIMALS 5,
       END OF ty_out1.
DATA: gt_out1 TYPE STANDARD TABLE OF ty_out1,
      gs_out1 LIKE LINE OF gt_out1.

TYPES: BEGIN OF ty_out2,
         ltext  TYPE LTEXT,
         waers  TYPE WAERS_CURC,
         month01 TYPE P DECIMALS 5,
         month02 TYPE P DECIMALS 5,
         month03 TYPE P DECIMALS 5,
         month04 TYPE P DECIMALS 5,
         month05 TYPE P DECIMALS 5,
         month06 TYPE P DECIMALS 5,
         month07 TYPE P DECIMALS 5,
         month08 TYPE P DECIMALS 5,
         month09 TYPE P DECIMALS 5,
         month10 TYPE P DECIMALS 5,
         month11 TYPE P DECIMALS 5,
         month12 TYPE P DECIMALS 5,
         montha TYPE P DECIMALS 5,
       END OF ty_out2.
DATA: gt_out2 TYPE STANDARD TABLE OF ty_out2,
      gs_out2 LIKE LINE OF gt_out2.

TYPES: BEGIN OF ty_out3,
         ltext  TYPE LTEXT,
         waers  TYPE WAERS_CURC,
         month01 TYPE P DECIMALS 5,
         month02 TYPE P DECIMALS 5,
         month03 TYPE P DECIMALS 5,
         month04 TYPE P DECIMALS 5,
         month05 TYPE P DECIMALS 5,
         month06 TYPE P DECIMALS 5,
         month07 TYPE P DECIMALS 5,
         month08 TYPE P DECIMALS 5,
         month09 TYPE P DECIMALS 5,
         month10 TYPE P DECIMALS 5,
         month11 TYPE P DECIMALS 5,
         month12 TYPE P DECIMALS 5,
         montha TYPE P DECIMALS 5,
       END OF ty_out3.
DATA: gt_out3 TYPE STANDARD TABLE OF ty_out3,
      gs_out3 LIKE LINE OF gt_out3.

TYPES: BEGIN OF ty_out4,
         ltext  TYPE LTEXT,
         waers  TYPE WAERS_CURC,
         vseur  TYPE P DECIMALS 5,
         vsitl  TYPE P DECIMALS 5,
       END OF ty_out4.
DATA: gt_out4 TYPE STANDARD TABLE OF ty_out4,
      gs_out4 LIKE LINE OF gt_out4.

DATA: gt_fieldcat1 TYPE LVC_T_FCAT.

DATA: gv_mese      TYPE FCMNR,
      gv_anno(4)   TYPE N,
      gv_url       TYPE C LENGTH 256.

*----------------------------------------------------------------------*
* FORM OUTPUT_XLS                                                      *
*----------------------------------------------------------------------*
FORM output_xls.

  FIELD-SYMBOLS: <lf_campo> TYPE ANY.

  DATA: lo_container   TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
        li_control     TYPE REF TO I_OI_CONTAINER_CONTROL,
        li_document    TYPE REF TO I_OI_DOCUMENT_PROXY,
        li_spreadsheet TYPE REF TO I_OI_SPREADSHEET,
        li_error       TYPE REF TO I_OI_ERROR,
        lt_cell_format TYPE soi_cell_table,
        ls_cell_format LIKE LINE OF lt_cell_format,
        lt_rfc_fields  TYPE TABLE OF rfc_fields,
        lv_lines       TYPE I,
        lv_col         TYPE I,
        lv_first       TYPE I,
        lv_last        TYPE I,
        ls_fieldcat    TYPE lvc_s_fcat,
        lv_mesei       TYPE FCMNR,
        lv_mesec(9)    TYPE C.

  TYPES: BEGIN OF ty_title,
           ltext  TYPE LTEXT,
         END OF ty_title.
  DATA: lt_title TYPE STANDARD TABLE OF ty_title,
        ls_title LIKE LINE OF lt_title.

  TYPES: BEGIN OF ty_header1,
           ltext1  TYPE LTEXT,
           ltext2  TYPE LTEXT,
           ltext3  TYPE LTEXT,
           ltext4  TYPE LTEXT,
           ltext5  TYPE LTEXT,
           ltext6  TYPE LTEXT,
           ltext7  TYPE LTEXT,
           ltext8  TYPE LTEXT,
           ltext9  TYPE LTEXT,
           ltext10 TYPE LTEXT,
           ltext11 TYPE LTEXT,
           ltext12 TYPE LTEXT,
           ltext13 TYPE LTEXT,
           ltext14 TYPE LTEXT,
           ltext15 TYPE LTEXT,
           ltext16 TYPE LTEXT,
           ltext17 TYPE LTEXT,
           ltext18 TYPE LTEXT,
           ltext19 TYPE LTEXT,
           ltext20 TYPE LTEXT,
           ltext21 TYPE LTEXT,
           ltext22 TYPE LTEXT,
           ltext23 TYPE LTEXT,
           ltext24 TYPE LTEXT,
           ltext25 TYPE LTEXT,
           ltext26 TYPE LTEXT,
         END OF ty_header1.
  DATA: lt_header1 TYPE STANDARD TABLE OF ty_header1,
        ls_header1 LIKE LINE OF lt_header1.

  TYPES: BEGIN OF ty_header2,
           ltext1  TYPE LTEXT,
           ltext2  TYPE LTEXT,
           ltext3  TYPE LTEXT,
           ltext4  TYPE LTEXT,
           ltext5  TYPE LTEXT,
           ltext6  TYPE LTEXT,
           ltext7  TYPE LTEXT,
           ltext8  TYPE LTEXT,
           ltext9  TYPE LTEXT,
           ltext10 TYPE LTEXT,
           ltext11 TYPE LTEXT,
           ltext12 TYPE LTEXT,
           ltext13 TYPE LTEXT,
           ltext14 TYPE LTEXT,
           ltext15 TYPE LTEXT,
         END OF ty_header2.
  DATA: lt_header2 TYPE STANDARD TABLE OF ty_header2,
        ls_header2 LIKE LINE OF lt_header2.

  TYPES: BEGIN OF ty_header4,
           ltext1  TYPE LTEXT,
           ltext2  TYPE LTEXT,
           ltext3  TYPE LTEXT,
           ltext4  TYPE LTEXT,
         END OF ty_header4.
  DATA: lt_header4 TYPE STANDARD TABLE OF ty_header4,
        ls_header4 LIKE LINE OF lt_header4.

  C_OI_CONTAINER_CONTROL_CREATOR=>GET_CONTAINER_CONTROL(
     IMPORTING
       CONTROL = li_control
       ERROR   = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  CREATE OBJECT lo_container
    EXPORTING
      CONTAINER_NAME              = 'CONT'
    EXCEPTIONS
      CNTL_ERROR                  = 1
      CNTL_SYSTEM_ERROR           = 2
      CREATE_ERROR                = 3
      LIFETIME_ERROR              = 4
      LIFETIME_DYNPRO_DYNPRO_LINK = 5
      OTHERS                      = 6.
  IF SY-SUBRC <> 0.
    MESSAGE E001(00) WITH 'Error while creating container'.
  ENDIF.

  li_control->init_control(
    EXPORTING
      INPLACE_ENABLED      = 'X'
      R3_APPLICATION_NAME  = 'EXCEL CONTAINER'
      PARENT               = lo_container
    IMPORTING
      ERROR                = li_error
    EXCEPTIONS
      JAVABEANNOTSUPPORTED = 1
      OTHERS               = 2 ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_control->get_document_proxy(
    EXPORTING
      DOCUMENT_TYPE  = SOI_DOCTYPE_EXCEL_SHEET
    IMPORTING
      DOCUMENT_PROXY = li_document
      ERROR          = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_document->create_document(
    EXPORTING
      DOCUMENT_TITLE = 'Excel'
    IMPORTING
      ERROR          = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_document->get_spreadsheet_interface(
    EXPORTING
      NO_FLUSH        = ' '
    IMPORTING
      SHEET_INTERFACE = li_spreadsheet
      ERROR           = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

********************************TITOLO*******************************
  li_spreadsheet->add_sheet(
    EXPORTING
      NAME     = 'Parita fisse'
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->select_sheet(
    EXPORTING
      NAME      = 'Parita fisse'
      NO_FLUSH  = ' '
    IMPORTING
      ERROR     = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  ls_title-ltext = 'DIVISE IN PARITÀ FISSA'.
  APPEND ls_title TO lt_title.
  li_spreadsheet->set_selection(
    EXPORTING
      TOP      = 2
      LEFT     = 1
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->insert_range(
    EXPORTING
      NAME     = 'rTitolo'
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
    TABLES
      DATA   = lt_title[]
      FIELDS = lt_rfc_fields.

  li_spreadsheet->insert_one_table(
    EXPORTING
      DATA_TABLE   = lt_title[]
      FIELDS_TABLE = lt_rfc_fields
      RANGENAME    = 'rTitolo'
      WHOLETABLE   = 'X'
      NO_FLUSH     = ' '
    IMPORTING
      ERROR        = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

********************************HEADER*******************************
  ls_header4-ltext1 = 'DIVISA'.
  ls_header4-ltext2 = 'SIGLA'.
  ls_header4-ltext3 = 'PARITÀ vs EUR'.
  ls_header4-ltext4 = 'CROSS vs ITL'.
  APPEND ls_header4 TO lt_header4.
  li_spreadsheet->set_selection(
    EXPORTING
      TOP      = 4
      LEFT     = 1
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->insert_range(
    EXPORTING
      NAME     = 'rHeader4'
      ROWS     = 1
      COLUMNS  = 4
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
    TABLES
      DATA   = lt_header4[]
      FIELDS = lt_rfc_fields.

  li_spreadsheet->insert_one_table(
    EXPORTING
      DATA_TABLE   = lt_header4[]
      FIELDS_TABLE = lt_rfc_fields
      RANGENAME    = 'rHeader4'
      WHOLETABLE   = 'X'
      NO_FLUSH     = ' '
    IMPORTING
      ERROR        = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

*********************************BODY********************************
  li_spreadsheet->set_selection(
    EXPORTING
      TOP      = 6
      LEFT     = 1
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->insert_range(
    EXPORTING
      NAME     = 'rBody4'
      ROWS     = 12
      COLUMNS  = 4
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  PERFORM get_data4.
  CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
    TABLES
      DATA   = gt_out4[]
      FIELDS = lt_rfc_fields.

  li_spreadsheet->insert_one_table(
    EXPORTING
      DATA_TABLE   = gt_out4[]
      FIELDS_TABLE = lt_rfc_fields
      RANGENAME    = 'rBody4'
      WHOLETABLE   = 'X'
      NO_FLUSH     = ' '
    IMPORTING
      ERROR        = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->fit_widest(
    EXPORTING
      NAME     = 'rHeader4'
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

*TITOLO
  CLEAR ls_cell_format.
  ls_cell_format-top        = 2.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 1.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Arial'.
  ls_cell_format-size       = 12.
  ls_cell_format-bold       = 1.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = -1.
  ls_cell_format-framecolor = -1.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN1
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 13.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN2
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 2.
  ls_cell_format-rows       = 13.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = 1.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN3
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 3.
  ls_cell_format-rows       = 13.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = 2.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  ls_cell_format-number     = 1.
  ls_cell_format-decimals   = 6.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN4
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 4.
  ls_cell_format-rows       = 13.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = 2.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  ls_cell_format-number     = 1.
  ls_cell_format-decimals   = 5.
  APPEND ls_cell_format TO lt_cell_format.
*BODY LAST LINE
  CLEAR ls_cell_format.
  ls_cell_format-top        = 18.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 1.
  ls_cell_format-columns    = 4.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = 125.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
*HEADER
  CLEAR ls_cell_format.
  ls_cell_format-top        = 4.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 1.
  ls_cell_format-columns    = 4.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-bold       = 1.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = 191.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
  li_spreadsheet->cell_format(
    EXPORTING
      CELLS = lt_cell_format ).

  CLEAR: lt_title[], lt_cell_format.

********************************TITOLO*******************************
  li_spreadsheet->add_sheet(
    EXPORTING
      NAME     = 'medieanno'
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->select_sheet(
    EXPORTING
      NAME     = 'medieanno'
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  ls_title-ltext = 'CAMBI MEDI contro EUR'.
  APPEND ls_title TO lt_title.
  li_spreadsheet->set_selection(
    EXPORTING
      TOP      = 2
      LEFT     = 1
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->insert_range(
    EXPORTING
      NAME     = 'rTitolo'
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
    TABLES
      DATA   = lt_title[]
      FIELDS = lt_rfc_fields.

  li_spreadsheet->insert_one_table(
    EXPORTING
      DATA_TABLE   = lt_title[]
      FIELDS_TABLE = lt_rfc_fields
      RANGENAME    = 'rTitolo'
      WHOLETABLE   = 'X'
      NO_FLUSH     = ' '
    IMPORTING
      ERROR        = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

********************************HEADER*******************************
  ls_header2-ltext1  = 'DIVISA'.
  ls_header2-ltext2  = 'SIGLA'.
  ls_header2-ltext3  = 'GENNAIO'.
  ls_header2-ltext4  = 'FEBBRAIO'.
  ls_header2-ltext5  = 'MARZO'.
  ls_header2-ltext6  = 'APRILE'.
  ls_header2-ltext7  = 'MAGGIO'.
  ls_header2-ltext8  = 'GIUGNO'.
  ls_header2-ltext9  = 'LUGLIO'.
  ls_header2-ltext10 = 'AGOSTO'.
  ls_header2-ltext11 = 'SETTEMBRE'.
  ls_header2-ltext12 = 'OTTOBRE'.
  ls_header2-ltext13 = 'NOVEMBRE'.
  ls_header2-ltext14 = 'DICEMBRE'.
  ls_header2-ltext15 = 'MEDIE ANNUE'.
  APPEND ls_header2 TO lt_header2.
  li_spreadsheet->set_selection(
    EXPORTING
      TOP      = 4
      LEFT     = 1
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->insert_range(
    EXPORTING
      NAME     = 'rHeader2'
      ROWS     = 1
      COLUMNS  = 15
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
    TABLES
      DATA   = lt_header2[]
      FIELDS = lt_rfc_fields.

  li_spreadsheet->insert_one_table(
    EXPORTING
      DATA_TABLE   = lt_header2[]
      FIELDS_TABLE = lt_rfc_fields
      RANGENAME    = 'rHeader2'
      WHOLETABLE   = 'X'
      NO_FLUSH     = ' '
    IMPORTING
      ERROR        = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

*********************************BODY********************************
  li_spreadsheet->set_selection(
    EXPORTING
      TOP      = 6
      LEFT     = 1
      ROWS     = 1
      COLUMNS  = 1
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  PERFORM get_data2.
  DESCRIBE TABLE gt_out2 LINES lv_lines.
  li_spreadsheet->insert_range(
    EXPORTING
      NAME     = 'rBody2'
      ROWS     = lv_lines
      COLUMNS  = 15
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
    TABLES
      DATA   = gt_out2[]
      FIELDS = lt_rfc_fields.

  li_spreadsheet->insert_one_table(
    EXPORTING
      DATA_TABLE   = gt_out2[]
      FIELDS_TABLE = lt_rfc_fields
      RANGENAME    = 'rBody2'
      WHOLETABLE   = 'X'
      NO_FLUSH     = ' '
    IMPORTING
      ERROR        = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

  li_spreadsheet->fit_widest(
    EXPORTING
      NAME     = 'rBody2'
      NO_FLUSH = ' '
    IMPORTING
      ERROR    = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.

*TITOLO
  CLEAR ls_cell_format.
  ls_cell_format-top        = 2.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 1.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Arial'.
  ls_cell_format-size       = 12.
  ls_cell_format-bold       = 1.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = -1.
  ls_cell_format-framecolor = -1.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN1
  ADD 1 to lv_lines.
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = lv_lines.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN2
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 2.
  ls_cell_format-rows       = lv_lines.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = 1.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN3-14
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 3.
  ls_cell_format-rows       = lv_lines.
  ls_cell_format-columns    = 12.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = 2.
  ls_cell_format-frametyp   = 95.
  ls_cell_format-framecolor = 1.
  ls_cell_format-number     = 1.
  ls_cell_format-decimals   = 5.
  APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN15
  CLEAR ls_cell_format.
  ls_cell_format-top        = 5.
  ls_cell_format-left       = 15.
  ls_cell_format-rows       = lv_lines.
  ls_cell_format-columns    = 1.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-size       = 10.
  ls_cell_format-bold       = 0.
  ls_cell_format-italic     = 0.
  ls_cell_format-align      = 2.
  ls_cell_format-frametyp   = 73.
  ls_cell_format-framecolor = 1.
  ls_cell_format-number     = 1.
  ls_cell_format-decimals   = 5.
  APPEND ls_cell_format TO lt_cell_format.
*BODY LAST LINE
  ADD 5 to lv_lines.
  CLEAR ls_cell_format.
  ls_cell_format-top        = lv_lines.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 1.
  ls_cell_format-columns    = 15.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = 125.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
*HEADER
  CLEAR ls_cell_format.
  ls_cell_format-top        = 4.
  ls_cell_format-left       = 1.
  ls_cell_format-rows       = 1.
  ls_cell_format-columns    = 15.
  ls_cell_format-front      = -1.
  ls_cell_format-back       = -1.
  ls_cell_format-font       = 'Courier New'.
  ls_cell_format-bold       = 1.
  ls_cell_format-align      = -1.
  ls_cell_format-frametyp   = 191.
  ls_cell_format-framecolor = 1.
  APPEND ls_cell_format TO lt_cell_format.
  li_spreadsheet->cell_format(
    EXPORTING
      CELLS = lt_cell_format ).

  CLEAR: lt_title[], lt_cell_format.

********************************TITOLO*******************************
  lv_mesei = gv_mese.
  WHILE gv_mese > 0.

    CASE gv_mese.
      WHEN 1.
        lv_mesec = 'gennaio'.
      WHEN 2.
        lv_mesec = 'febbraio'.
      WHEN 3.
        lv_mesec = 'marzo'.
      WHEN 4.
        lv_mesec = 'aprile'.
      WHEN 5.
        lv_mesec = 'maggio'.
      WHEN 6.
        lv_mesec = 'giugno'.
      WHEN 7.
        lv_mesec = 'luglio'.
      WHEN 8.
        lv_mesec = 'agosto'.
      WHEN 9.
        lv_mesec = 'settembre'.
      WHEN 10.
        lv_mesec = 'ottobre'.
      WHEN 11.
        lv_mesec = 'novembre'.
      WHEN 12.
        lv_mesec = 'dicembre'.
    ENDCASE.

    li_spreadsheet->add_sheet(
      EXPORTING
        NAME     = lv_mesec
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    li_spreadsheet->select_sheet(
      EXPORTING
        NAME     = lv_mesec
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    CLEAR lt_title[].
    ls_title-ltext = 'CAMBI contro EUR'.
    APPEND ls_title TO lt_title.
    li_spreadsheet->set_selection(
      EXPORTING
        TOP      = 2
        LEFT     = 1
        ROWS     = 1
        COLUMNS  = 1
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    li_spreadsheet->insert_range(
      EXPORTING
        NAME     = 'rTitolo'
        ROWS     = 1
        COLUMNS  = 1
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
      TABLES
        DATA   = lt_title[]
        FIELDS = lt_rfc_fields.

    li_spreadsheet->insert_one_table(
      EXPORTING
        DATA_TABLE   = lt_title[]
        FIELDS_TABLE = lt_rfc_fields
        RANGENAME    = 'rTitolo'
        WHOLETABLE   = 'X'
        NO_FLUSH     = ' '
      IMPORTING
        ERROR        = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

********************************HEADER*******************************
    CLEAR: ls_header1, lt_header1[].
    ls_header1-ltext1  = 'DIVISA'.
    ls_header1-ltext2  = 'SIGLA'.
    PERFORM fieldcat1.
    LOOP AT gt_fieldcat1 INTO ls_fieldcat.
      CASE ls_fieldcat-col_pos.
        WHEN 4.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext3.
        WHEN 5.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext4.
        WHEN 6.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext5.
        WHEN 7.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext6.
        WHEN 8.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext7.
        WHEN 9.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext8.
        WHEN 10.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext9.
        WHEN 11.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext10.
        WHEN 12.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext11.
        WHEN 13.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext12.
        WHEN 14.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext13.
        WHEN 15.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext14.
        WHEN 16.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext15.
        WHEN 17.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext16.
        WHEN 18.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext17.
        WHEN 19.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext18.
        WHEN 20.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext19.
        WHEN 21.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext20.
        WHEN 22.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext21.
        WHEN 23.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext22.
        WHEN 24.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext23.
        WHEN 25.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext24.
        WHEN 26.
          CONCATENATE '''' ls_fieldcat-reptext
             INTO ls_header1-ltext25.
      ENDCASE.
    ENDLOOP.
    ls_header1-ltext26 = 'media mensile'.
    APPEND ls_header1 TO lt_header1.
    li_spreadsheet->set_selection(
      EXPORTING
        TOP      = 4
        LEFT     = 1
        ROWS     = 1
        COLUMNS  = 1
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    li_spreadsheet->insert_range(
      EXPORTING
        NAME     = 'rHeader1'
        ROWS     = 1
        COLUMNS  = 26
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
      TABLES
        DATA   = lt_header1[]
        FIELDS = lt_rfc_fields.

    li_spreadsheet->insert_one_table(
      EXPORTING
        DATA_TABLE   = lt_header1[]
        FIELDS_TABLE = lt_rfc_fields
        RANGENAME    = 'rHeader1'
        WHOLETABLE   = 'X'
        NO_FLUSH     = ' '
      IMPORTING
        ERROR        = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

*********************************BODY********************************
    li_spreadsheet->set_selection(
      EXPORTING
        TOP      = 6
        LEFT     = 1
        ROWS     = 1
        COLUMNS  = 1
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    PERFORM get_data1.
    DESCRIBE TABLE gt_out1 LINES lv_lines.
    li_spreadsheet->insert_range(
      EXPORTING
        NAME     = 'rBody1'
        ROWS     = lv_lines
        COLUMNS  = 26
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    CALL FUNCTION 'DP_GET_FIELDS_FROM_TABLE'
      TABLES
        DATA   = gt_out1[]
        FIELDS = lt_rfc_fields.

    li_spreadsheet->insert_one_table(
      EXPORTING
        DATA_TABLE   = gt_out1[]
        FIELDS_TABLE = lt_rfc_fields
        RANGENAME    = 'rBody1'
        WHOLETABLE   = 'X'
        NO_FLUSH     = ' '
      IMPORTING
        ERROR        = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    li_spreadsheet->fit_widest(
      EXPORTING
        NAME     = 'rBody1'
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

*TITOLO
    CLEAR ls_cell_format.
    ls_cell_format-top        = 2.
    ls_cell_format-left       = 1.
    ls_cell_format-rows       = 1.
    ls_cell_format-columns    = 1.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-font       = 'Arial'.
    ls_cell_format-size       = 12.
    ls_cell_format-bold       = 1.
    ls_cell_format-italic     = 0.
    ls_cell_format-align      = -1.
    ls_cell_format-frametyp   = -1.
    ls_cell_format-framecolor = -1.
    APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN1
    ADD 1 to lv_lines.
    CLEAR ls_cell_format.
    ls_cell_format-top        = 5.
    ls_cell_format-left       = 1.
    ls_cell_format-rows       = lv_lines.
    ls_cell_format-columns    = 1.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-font       = 'Courier New'.
    ls_cell_format-size       = 10.
    ls_cell_format-bold       = 0.
    ls_cell_format-italic     = 0.
    ls_cell_format-align      = -1.
    ls_cell_format-frametyp   = 73.
    ls_cell_format-framecolor = 1.
    APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN2
    CLEAR ls_cell_format.
    ls_cell_format-top        = 5.
    ls_cell_format-left       = 2.
    ls_cell_format-rows       = lv_lines.
    ls_cell_format-columns    = 1.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-font       = 'Courier New'.
    ls_cell_format-size       = 10.
    ls_cell_format-bold       = 0.
    ls_cell_format-italic     = 0.
    ls_cell_format-align      = 1.
    ls_cell_format-frametyp   = 73.
    ls_cell_format-framecolor = 1.
    APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN3-25
    CLEAR ls_cell_format.
    ls_cell_format-top        = 5.
    ls_cell_format-left       = 3.
    ls_cell_format-rows       = lv_lines.
    ls_cell_format-columns    = 23.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-font       = 'Courier New'.
    ls_cell_format-size       = 10.
    ls_cell_format-bold       = 0.
    ls_cell_format-italic     = 0.
    ls_cell_format-align      = 2.
    ls_cell_format-frametyp   = 95.
    ls_cell_format-framecolor = 1.
    ls_cell_format-number     = 1.
    ls_cell_format-decimals   = 5.
    APPEND ls_cell_format TO lt_cell_format.
*BODY COLUMN26
    CLEAR ls_cell_format.
    ls_cell_format-top        = 5.
    ls_cell_format-left       = 26.
    ls_cell_format-rows       = lv_lines.
    ls_cell_format-columns    = 1.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-font       = 'Courier New'.
    ls_cell_format-size       = 10.
    ls_cell_format-bold       = 0.
    ls_cell_format-italic     = 0.
    ls_cell_format-align      = 2.
    ls_cell_format-frametyp   = 73.
    ls_cell_format-framecolor = 1.
    ls_cell_format-number     = 1.
    ls_cell_format-decimals   = 5.
    APPEND ls_cell_format TO lt_cell_format.
*BODY LAST LINE
    ADD 5 to lv_lines.
    CLEAR ls_cell_format.
    ls_cell_format-top        = lv_lines.
    ls_cell_format-left       = 1.
    ls_cell_format-rows       = 1.
    ls_cell_format-columns    = 26.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-align      = -1.
    ls_cell_format-frametyp   = 125.
    ls_cell_format-framecolor = 1.
    APPEND ls_cell_format TO lt_cell_format.
*HEADER
    CLEAR ls_cell_format.
    ls_cell_format-top        = 4.
    ls_cell_format-left       = 1.
    ls_cell_format-rows       = 1.
    ls_cell_format-columns    = 26.
    ls_cell_format-front      = -1.
    ls_cell_format-back       = -1.
    ls_cell_format-font       = 'Courier New'.
    ls_cell_format-bold       = 1.
    ls_cell_format-align      = -1.
    ls_cell_format-frametyp   = 191.
    ls_cell_format-framecolor = 1.
    APPEND ls_cell_format TO lt_cell_format.
    li_spreadsheet->cell_format(
      EXPORTING
        CELLS = lt_cell_format ).
    CLEAR lt_cell_format[].

    li_spreadsheet->set_fixed_cols(
      EXPORTING
        COLUMNS  = 2
        NO_FLUSH = ' '
      IMPORTING
        ERROR    = li_error ).
    IF li_error->has_failed = 'X'.
      li_error->raise_message( 'E' ).
    ENDIF.

    lv_col = 25.
    WHILE lv_col > 3.
      ASSIGN COMPONENT lv_col OF STRUCTURE ls_header1 TO <lf_campo>.
      IF <lf_campo> IS INITIAL.
        lv_first = lv_col.
        IF lv_last IS INITIAL.
          lv_last = lv_col.
        ENDIF.
      ELSE.
        EXIT.
      ENDIF.
      SUBTRACT 1 FROM lv_col.
    ENDWHILE.
    IF lv_last IS NOT INITIAL.
      li_spreadsheet->hide_columns(
        EXPORTING
          NAME     = lv_mesec
          NO_FLUSH = ' '
          FIRST    = lv_first
          LAST     = lv_last
        IMPORTING
          ERROR    = li_error ).
      IF li_error->has_failed = 'X'.
        li_error->raise_message( 'E' ).
      ENDIF.
      CLEAR: lv_first, lv_last.
    ENDIF.

    SUBTRACT 1 FROM gv_mese.
  ENDWHILE.

  gv_mese = lv_mesei.
  PERFORM get_data1.

  CONCATENATE 'FILE://' gv_url
         INTO gv_url.
  li_document->save_document_to_url(
    EXPORTING
      URL   = gv_url
    IMPORTING
      ERROR = li_error ).
  IF li_error->has_failed = 'X'.
    li_error->raise_message( 'E' ).
  ENDIF.
  SHIFT gv_url BY 7 PLACES.

ENDFORM.                    "output_xls

Nessun commento: