Monday 25 June 2012

To download multiple internal table data into multiple excel(OLE)

  type-pools : ole2 , slis.
* Type Declaration
typesbegin of typ_excel,
        line(5000type c,
       end of typ_excel.


************************************************************************
* OLE objects Declarations
************************************************************************
data: w_excel type ole2_object,
      w_workbook type ole2_object,
      w_worksheet type ole2_object,
      w_columns  type ole2_object,
      w_column_ent type ole2_object,
      w_cell type ole2_object,
      w_int type ole2_object,
      w_range type ole2_object.
************************************************************************
* Data declarations
************************************************************************
data: w_deli(1type c"Delimiter
      w_hex type x,
      w_rc type i,
      w_fval type string.
************************************************************************
* Field Symbols
************************************************************************
field-symbols: <fs> .
field-symbols: <lv_field> type any.

************************************************************************
* Constants
************************************************************************
constants wl_c09(2type n value 09.

****************************************************************************

  data: l_counter type i,
      l_int_tab type string,
      l_lcount type i.

data : p_file type rlgrap-filename  value 'C:\Documents and Settings\Test Data Generation Tool'.
*&---------------------------------------------------------------------*
*&      Form  SUBMIT_PROGRAMS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form submit_programs.


  loop at it_submit_programs into wa_submit_programs where flag = 'X'.
    l_lcount = l_lcount + 1.
    submit (wa_submit_programs-name)"ZTDGT_MM_12_PI_ACCOUNTING
           with s_bldat in  s_bldat
           and return
           exporting list to memory.
    l_counter = l_counter + 1 .
*l_int_tab = wa_submit_programs-int_tab.
    assign (wa_submit_programs-int_tab) to <ls_jest_buf>.
    import  <ls_jest_buf> from memory id 'memo1'.
    free memory id 'memo1'.

    perform download_excel.  "Ole logic for multi excel sheets

  endloop.

if sy-subrc = 0.
  get property of w_excel 'ActiveWorkbook' = w_workbook.   "Rot
  call method of w_workbook 'SAVEAS'
    exporting #1 = p_file.
  free object: w_worksheet, w_excel.

  endif.

endform.                    " SUBMIT_PROGRAMS

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form download_excel .

*  data : p_file type rlgrap-filename  value 'C:\Documents and Settings\295830\Desktop\ztest_g'.

  if l_lcount = 1 .                                                "Rot

    create object w_excel 'EXCEL.APPLICATION'"Create object for Excel
*ENDIF.
    set property of w_excel  'VISIBLE' = 1"In background Mode

    call method of w_excel 'WORKBOOKS' = w_workbook.
*ENDIF.
    call method of w_workbook 'ADD'"Create a new Workbook

    set property of w_excel 'SheetsInNewWorkbook' = 2"No of sheets


    data : lv_noofsheets type sy-tabix.
* Add two worksheets for Control & Status records
    lv_noofsheets = lv_noofsheets + 2.

* Add required number of worksheets
    do lv_noofsheets times.
      if sy-index > 1.
        call method of w_excel 'WORKSHEETS' = w_worksheet.
        call method of w_worksheet 'ADD'.
        free object w_worksheet.
      endif.
    enddo.
  endif.
                                                          "Rot
  perform concatenate_fileds.

*    LOOP at it_final_accounting INTO wa_final_accounting.
  loop at <ls_jest_buf> assigning <ls_jest>.

    move-corresponding <ls_jest> to wa_final_accounting.
*     Build excel table with Tab Delimited character
    clear: gs_excel.
    do.
      check sy-index <> 0.
      unassign <lv_field>.
      assign component sy-index of structure <ls_jest>   "to pass workarea fields
             to <lv_field>.
      if <lv_field> is assigned.
        clear: w_fval.
        w_fval = <lv_field>.

        if gs_excel-line is not initial.
          concatenate gs_excel-line w_fval
                      into gs_excel-line
                      separated by cl_abap_char_utilities=>horizontal_tab.
        else.
          gs_excel-line = w_fval.
        endif.
      else.
        exit.
      endif.
    enddo.
    append gs_excel to gt_excel.

  endloop.

  perform download_sheet tables gt_excel using l_counter wa_submit_programs-desc . "1 'Item Details'.

  get property of w_excel 'ActiveSheet' = w_worksheet.

  call method of w_worksheet 'PROTECT'                  "Rot
   exporting #1 = wa_submit_programs-name."'infy@123'.  "Rot
*if l_lcount > 1.                                        "Rot
** Save the Excel file
*  GET PROPERTY OF w_excel 'ActiveWorkbook' = w_workbook.
*  CALL METHOD OF w_workbook 'SAVEAS'
*    EXPORTING #1 = p_file.
*  FREE OBJECT: w_worksheet, w_excel.
*ENDIF.                                                  "Rot
endform.                    " DOWNLOAD_EXCEL
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_SHEET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_IT_TAB2  text
*      -->P_2      text
*      -->P_0137   text
*----------------------------------------------------------------------*
form download_sheet tables p_tab
                    using p_sheet type i
                          p_name type any.

  call method of w_excel 'WORKSHEETS' = w_worksheet
    exporting #1 = p_sheet.

  call method of w_worksheet 'ACTIVATE'.
  set property of w_worksheet 'NAME' = p_name.

  call method of w_excel 'Range' = w_range
    exporting #1 = 'A1' #2 = 'Z1'.

  call method of w_range 'INTERIOR' = w_int.
  set property of w_int 'ColorIndex' = 6.
  set property of w_int 'Pattern' = 1.

* Initially unlock all the columns( by default all the columns are locked )
  call method of w_excel 'Columns' = w_columns.
  set property of w_columns 'Locked' = 0.

* Export the contents in the internal table to the clipboard
  call method cl_gui_frontend_services=>clipboard_export
    importing
      data                 = p_tab[]
    changing
      rc                   = w_rc
    exceptions
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      others               = 4.

* Paste the contents in the clipboard to the worksheet
  call method of w_worksheet 'Paste'.

* Autofit the columns according to the contents
  call method of w_excel 'Columns' = w_columns.
  call method of w_columns 'AutoFit'.

  free object: w_columns, w_range, w_int, w_worksheet.
  refresh: p_tab[].

endform.                    " DOWNLOAD_SHEET



*&---------------------------------------------------------------------*
*&      Form  CONCATENATE_FILEDS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form concatenate_fileds .

*TO get the header details.

*  DATA: w_deli(1) TYPE c,
*         w_hex TYPE x.
*
* CONSTANTS wl_c09(2) TYPE n VALUE 09.
*  FIELD-SYMBOLS: <fs> .
*
* ASSIGN w_deli TO <fs> TYPE 'X'.
*  w_hex = wl_c09.
*  <fs> = w_hex.
*
* CONCATENATE 'Material_No' 'Physical Inventory Document'
*              'Fiscal Year' 'Plant'
*              INTO gs_excel SEPARATED BY w_deli.
*  MOVE wa_submit_programs-header to gs_excel .
  gs_excel = wa_submit_programs-header.
  append gs_excel to gt_excel.
endform.                    " CONCATENATE_FILEDS

No comments:

Post a Comment