Tip

Dynamically count fields in an internal table

The code below enables you to check how many fields there are in an itab in runtime. The code is one function module.
The code was written in R/3 4.6c.


FUNCTION ZMANDY_EXCEL_UPLOAD.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(FILE_NAME) TYPE  RLGRAP-FILENAME DEFAULT
*"       'C:WINDOWSDESKTOP'
*"  TABLES
*"      INTERNAL_TAB
*"----------------------------------------------------------------------

INCLUDE OLE2INCL.

DATA: STR(20),NDATE(10).
DATA: ROW TYPE I VALUE 1, COL TYPE I VALUE 1.
DATA OBJ TYPE OLE2_OBJECT.
DATA WORKBOOK TYPE OLE2_OBJECT.
DATA SH TYPE OLE2_OBJECT.
DATA CELL TYPE OLE2_OBJECT.
DATA RANGE TYPE OLE2_OBJECT.
DATA APP TYPE OLE2_OBJECT.
DATA SEL TYPE OLE2_OBJECT.
DATA COLUMNS TYPE OLE2_OBJECT.
DATA TYP.
FIELD-SYMBOLS: <F>.

CREATE OBJECT OBJ 'excel.application'.
SET PROPERTY OF OBJ 'visible' = 1.
CALL METHOD OF OBJ 'Workbooks' = WORKBOOK.
WRITE :/ 'workbook', SY-SUBRC.
CALL METHOD OF WORKBOOK 'add'.
CALL METHOD OF OBJ 'Worksheets' = SH EXPORTING #1 = 1.
CALL METHOD OF SH 'Activate'.



LOOP AT INTERNAL_TAB.
COL = 1.
do.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE INTERNAL_TAB TO <F>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
DESCRIBE FIELD <F>. "TYPE TYP COMPONENTS N.
CALL METHOD OF SH 'Cells' = CELL EXPORTING #1  = ROW #2 = COL.
SET PROPERTY OF CELL 'value' = <F>. "VALUE.
*PERFORM INSTERDATA USING ROW COL <F>.
COL = COL + 1.
ENDDO.
ROW

    Requires Free Membership to View

= ROW + 1. ENDLOOP. STR = ROW. ENDFUNCTION.

This was first published in November 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.