Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

header to internal table fields

Former Member
0 Kudos

Hi All,

How can I add header description to the internal table fields and download this internal table to desktop as EXCEL form.

I want to show heading in the first line of the excel and then 2 - 3 blank lines and then next line should be populated with DATE and Exec. Time, then 2-3 blank lines , then the data with the header description to be displayed.

My sample internal table data is as below,

John M 12 2344

Thomas M 17 8960

Lin F 10 8467

Jan M 12 4323

I need to add, description as NAME above John, Thomas, Lin etc.

as GENDER above M,M,F etc.

as CODE above 12, 17,10 etc. and

as EXTENTION above 2344, 8960,8467 etc

final execl sheet should be as below,

COMPANY DATA

DATE 28 - 08 - 2009 TIME 12:32

NAME GENDER CODE EXTENTION

John M 12 2344

Thomas M 17 8960

Lin F 10 8467

Jan M 12 4323

Please let me know, how to do this.

Thanks in advance ,

Sabu

6 REPLIES 6

former_member218674
Contributor
0 Kudos

Hello Sabu,

You can use GUI_DOWNLOAD to download data to desktop with header fields:

In below code you need to define single column FIELDNAMES table. Check GUI_DOWNLOAD for the types of FIELDNAMES table.



CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
*   BIN_FILESIZE                    =
    filename                        =
*   FILETYPE                        = 'ASC'
*   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                       = ' '
*   TRUNC_TRAILING_BLANKS_EOL       = 'X'
* IMPORTING
*   FILELENGTH                      =
  tables
    data_tab                        =   ---> provide DATA table here
*   FIELDNAMES                      =   ---> provide FIELDNAMES table here
* 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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

Hope this helps!

Thanks,

Augustin.

Former Member
0 Kudos

HI,

The standard func to download to excel will not provide these features, for this you will have to write a code whjch uses OLE


REPORT  MULTIEXCEL                    .

INCLUDE ole2incl.
DATA: application TYPE ole2_object,
       workbook TYPE ole2_object,
       sheet TYPE ole2_object,
       cells TYPE ole2_object.
CONSTANTS: row_max TYPE i VALUE 256.
DATA index TYPE i.

DATA: BEGIN OF itab1 OCCURS 0, first_name(10), END OF itab1.
DATA: BEGIN OF itab2 OCCURS 0, last_name(10), END OF itab2.
DATA: BEGIN OF itab3 OCCURS 0, formula(50), END OF itab3.

************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.

  APPEND: 'Peter' TO itab1, 'Ivanov' TO itab2,
                              '=Sheet1!A1 & " " & Sheet2!A1' TO itab3,
            'John' TO itab1, 'Smith' TO itab2,
                              '=Sheet1!A2 & " " & Sheet2!A2' TO itab3.

  CREATE OBJECT application 'excel.application'.
  SET PROPERTY OF application 'visible' = 1.
  CALL METHOD OF application 'Workbooks' = workbook.
  CALL METHOD OF workbook 'Add'.

* Create first Excel Sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 1.
  CALL METHOD OF sheet 'Activate'.
  SET PROPERTY OF sheet 'Name' = 'Sheet1'.
  LOOP AT itab1.
    index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = index.
    SET PROPERTY OF cells 'Value' = itab1-first_name.
  ENDLOOP.

* Create second Excel sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 2.
  SET PROPERTY OF sheet 'Name' = 'Sheet2'.
  CALL METHOD OF sheet 'Activate'.
  LOOP AT itab2.
    index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = index.
    SET PROPERTY OF cells 'Value' = itab2-last_name.
  ENDLOOP.

* Create third Excel sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 3.
  SET PROPERTY OF sheet 'Name' = 'Sheet3'.
  CALL METHOD OF sheet 'Activate'.
  LOOP AT itab3.
    index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = index.
    SET PROPERTY OF cells 'Formula' = itab3-formula.
    SET PROPERTY OF cells 'Value' = itab3-formula.
  ENDLOOP.

* Save excel speadsheet to particular filename
  CALL METHOD OF sheet 'SaveAs'
                  EXPORTING #1 = 'c:\temp\exceldoc1.xls'     "filename
                            #2 = 1.                          "fileFormat

*  Closes excel window, data is lost if not saved
*  SET PROPERTY OF application 'visible' = 0.

Quick guide to some of the OLE statements for OLE processing in this program as well as a few other ones.

 Save Excel speadsheet to particular filename
CALL METHOD OF sheet 'SaveAs'
                EXPORTING #1 = 'c:\temp\exceldoc1.xls'     "filename
                          #2 = 1.                          "fileFormat

* Save Excel document
CALL METHOD OF sheet 'SAVE'.

* Quits out of Excel document
CALL METHOD OF sheet 'QUIT'.

*  Closes visible Excel window, data is lost if not saved
SET PROPERTY OF application 'visible' = 0.

venkat_o
Active Contributor
0 Kudos

Hi Sabu, Just try this way.


REPORT  ztest_notepad.
DATA: BEGIN OF it_t001 OCCURS 0,
        bukrs TYPE t001-bukrs,
        butxt TYPE t001-butxt,
        ort01 TYPE t001-ort01,
      END OF it_t001.
DATA: BEGIN OF it_head OCCURS 0,
        bukrs TYPE string,
        butxt TYPE string,
        ort01 TYPE string,
      END OF it_head.

START-OF-SELECTION.
  SELECT * FROM t001 INTO CORRESPONDING FIELDS OF TABLE it_t001 UP TO 10 ROWS.

  it_head-bukrs = 'Company Code'.
  it_head-butxt = 'Name of Company Code or Company'.
  it_head-ort01 = 'City'.
  APPEND it_head.
  CLEAR  it_head.
  "Download headers first
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename = 'C:/t001.xls'
      filetype = 'ASC'
      append   = space
    TABLES
      data_tab = it_head.
  "Download data to the same file with APPEND = 'X'
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename = 'C:/t001.xls'
      filetype = 'ASC'
      append   = 'X'
    TABLES
      data_tab = it_t001.
Thanks Venkat.O

Former Member
0 Kudos

Hi,

Kindly go through this sample code below:



"Create T_Head internal table with the required structure for the header
 
*func mod to download header and contents to the given path
call function 'GUI_DOWNLOAD'
exporting
write_field_separator = 'X'
filename              = v_pathname      "path entered by the user
tables
data_tab              = t_head.             "Header Internal table

refresh t_head.
clear t_head.

call function 'GUI_DOWNLOAD'
exporting
*   BIN_FILESIZE                    =
    filename                        = v_pathname
    filetype                        = 'ASC'
    append                          = 'X'                     "Here by giving 'X', header will be appended in the download int table 
    write_field_separator           = 'X'
*   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                       = ' '
*   TRUNC_TRAILING_BLANKS_EOL       = 'X'
*   WK1_N_FORMAT                    = ' '
*   WK1_N_SIZE                      = ' '
*   WK1_T_FORMAT                    = ' '
*   WK1_T_SIZE                      = ' '
* IMPORTING
*   FILELENGTH                      =
    tables
    data_tab                        = itab
*   FIELDNAMES                      =
  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 id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

endif.

Hope it helps

Regards

Mansi

Former Member
0 Kudos

Hi ,

Please Check this Program.

loop at i_tab2.
        i_outtab-fmatnr = i_tab2-fmatnr.
        i_outtab-fqty = i_tab2-fqty.
        i_outtab-bmatnr = i_tab2-bmatnr.

        i_outtab-bqty = i_tab2-bqty.
        i_outtab-maktx = i_tab2-maktx.

        append i_outtab.
      endloop.


      t_header-name = 'Finished Material'.
      append t_header.
      t_header-name = 'FQTY'.
      append t_header.
      t_header-name = 'Bare Block Material'.
      append t_header.
      t_header-name = 'BQTY'.
      append t_header.
      t_header-name = 'Description'.
      append t_header.

      call function 'GUI_FILE_SAVE_DIALOG'
        exporting
          default_extension = 'XLS'
        importing
          fullpath          = v_pass_path.

      call function 'GUI_DOWNLOAD'
        exporting
          filename   = v_pass_path
          filetype   = 'DBF'
        tables
          data_tab   = i_outtab
          fieldnames = t_header.

Hope its usefull for u .

Regards ,

Shankar GJ

Former Member
0 Kudos

Issue resolved , thanks for all replys.