> Excel OLE 예시 <
DATA: LV_RETCODE TYPE SOI_RET_STRING,
LT_DOC_TABLE LIKE W3MIME OCCURS 0,
LV_DOC_SIZE TYPE I,
LV_DOC_TYPE(80) VALUE SOI_DOCTYPE_EXCEL_SHEET,
LV_DOC_FORMAT(80) TYPE C.
*-- SMW0 양식을 Load.
CALL FUNCTION 'SAP_OI_LOAD_MIME_DATA'
EXPORTING
OBJECT_ID = PV_OBJECT_ID
IMPORTING
DATA_SIZE = LV_DOC_SIZE
DOCUMENT_FORMAT = LV_DOC_FORMAT
DOCUMENT_TYPE = LV_DOC_TYPE
TABLES
DATA_TABLE = LT_DOC_TABLE
EXCEPTIONS
OBJECT_NOT_FOUND = 1
INTERNAL_ERROR = 2
OTHERS = 3.
*-- PC에 EXCEL을 DOWNLOAD.
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
FILENAME = PV_FILENAME
FILETYPE = 'BIN'
BIN_FILESIZE = LV_DOC_SIZE
TABLES
DATA_TAB = LT_DOC_TABLE
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TYPE = 5
NO_BATCH = 6
UNKNOWN_ERROR = 7.
*-- EXCEL OBJECT생성.
IF GS_EXCEL-HEADER = SPACE OR GS_EXCEL-HANDLE = -1.
CREATE OBJECT GS_EXCEL 'Excel.Application'.
ENDIF.
CALL METHOD OF GS_EXCEL 'Workbooks' = GS_WBOOK.
CALL METHOD OF GS_WBOOK 'OPEN' = GS_BOOK
EXPORTING
#1 = PV_FILENAME.
SET PROPERTY OF GS_EXCEL 'Visible' = 0.
*-- 활성 Sheet 변경.
CALL METHOD OF GS_EXCEL 'WORKSHEETS' = GS_SHEET
EXPORTING
#1 = PV_INDEX. "- 활성화할 Sheet.
CALL METHOD OF GS_SHEET 'ACTIVATE'.
*-- Sheet의 명칭변경.
SET PROPERTY OF GS_SHEET 'Name' = LV_NAME.
*-- SHEET 복사 (2번째 Sheet를 복사하여 2번째에 붙여넣기)
CALL METHOD OF GS_EXCEL 'WORKSHEETS' = GS_SHEET
EXPORTING
#1 = 2.
CALL METHOD OF GS_EXCEL 'WORKSHEETS' = GS_TMP_SHEET
EXPORTING
#1 = 2.
CALL METHOD OF GS_SHEET 'COPY'
EXPORTING
#1 = GS_TMP_SHEET.
*-- Cell에 값넣기.
CALL METHOD OF GS_EXCEL 'CELLS' = GS_CELL
EXPORTING
#1 = I "- 행(Row)
#2 = J. "- 열(Column)
SET PROPERTY OF GS_CELL 'VALUE' = VAL. "- 값.
*-- 엑셀저장.
CALL METHOD OF GS_EXCEL 'ActiveWorkbook' = GS_AWORK.
CALL METHOD OF GS_AWORK 'Save'.
*-- 엑셀표시 (PC에서 Excel 파일이 보이도록).
SET PROPERTY OF GS_EXCEL 'Visible' = 1.
*-- 셀의 커서 이동.
CALL METHOD OF GS_EXCEL 'CELLS' = GS_CELL
EXPORTING
#1 = I "- 행(Row)
#2 = J. "- 열(Column)
CALL METHOD OF GS_CELL 'Select'.
*-- 특정 Row를 복사하여 복사된 정보를 추가하기.
"- 양식복사.
CALL METHOD OF GS_EXCEL 'Rows' = GS_ROWS
EXPORTING
#1 = PV_ROW. "- 특정 Row(1개)
CALL METHOD OF GS_ROWS 'Select'. "- Row선택.
CALL METHOD OF GS_ROWS 'Copy'. "- 선택된 Row 복사.
"- Row 복사추가.
CALL METHOD OF GS_EXCEL 'Rows' = GS_ROWS
EXPORTING
#1 = PV_ROWS. "- 복사될 위치 (복수가능-->3:4)
CALL METHOD OF GS_ROWS 'Select'. "- Row선택.
CALL METHOD OF GS_ROWS 'Insert'. "- Row추가.
CALL METHOD OF GS_ROWS 'Paste'. "- 붙여넣기.
*-- 범위선택 및 범위복사 (예--> A1:B3를 A4:B6에 복사)
CALL METHOD OF EXCEL 'Cells' = LS_CELL1 "- 시작셀.
EXPORTING
#1 = lv_from_row1
#2 = LV_from_COL1.
CALL METHOD OF EXCEL 'Cells' = LS_CELL2 "- 종료셀.
EXPORTING
#1 = lv_from_row2
#2 = LV_from_COL2.
CALL METHOD OF EXCEL 'Range' = CELLS "- 범위지정.
EXPORTING
#1 = LS_CELL1
#2 = LS_CELL2.
CALL METHOD OF CELLS 'Select'. "- 범위선택.
CALL METHOD OF CELLS 'Copy'. "- 복사.
CALL METHOD OF EXCEL 'Cells' = LS_CELL1 "- 시작셀.
EXPORTING
#1 = lv_to_row1
#2 = LV_to_COL1.
CALL METHOD OF EXCEL 'Cells' = LS_CELL2 "- 종료셀.
EXPORTING
#1 = lv_to_row2
#2 = LV_to_COL2.
CALL METHOD OF EXCEL 'Range' = CELLS "- 범위지정.
EXPORTING
#1 = LS_CELL1
#2 = LS_CELL2.
CALL METHOD OF CELLS 'Select'. "- 범위선택.
CALL METHOD OF SHEETS 'Paste'. "- 붙여넣기.
*-- Data를 클립보드에 담아 한번에 붙여넣기.
TYPES: RECORD(20000) TYPE C,
T_DATA TYPE TABLE OF RECORD.
DATA : GT_CLIPBOARD TYPE T_DATA WITH HEADER LINE,
DELI(1) TYPE C.
DELI = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB. "- 탭문자.
LOOP AT GT_DATA.
"- 값을 탭으로 구분된 문자열로 변형하여 테이블에 APPEND.
CONCATENATE GT_DATA-COL1 GT_DATA-COL2 GT_DATA-COL3
INTO LT_CLIPBOARD
SEPARATED BY DELI.
APPEND LT_CLIPBOARD.
CLEAR LT_CLIPBOARD.
ENDLOOP.
"- 탭으로 구분된 문자열 테이블을 클립보드에 복사.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_EXPORT
IMPORTING
DATA = LT_CLIPBOARD[]
CHANGING
RC = LV_RC
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
"- 클립보드 Data 붙여넣기
CALL METHOD OF EXCEL 'Cells' = CELL "- 복사시작셀.
EXPORTING
#1 = LV_ROW
#2 = LV_COL.
CALL METHOD OF CELL 'Select'.
CALL METHOD OF SHEETS 'Paste'.
- 끝 -