엑셀 파일을 다운로드 하는 방법 크게 3가지가 있다.
1. METHOD를 이용하여 다운로드 하는 방법
2. OLE2를 이용하요 다운로드 하는 방법
3. SMW0을 이용하여 다운로드 하는 방법
EXCEL DOWNLOAD
👩⚕️3가지 방법을 통해 엑셀 파일 다운로드를 하기 위한 Function key 생성 먼저.
*-------------------------------------------------------
" EXCEL DOWNLOAD & UPLOAD
*-------------------------------------------------------
DATA: gv_answer. " 팝업창 yes no 변수
DATA: gs_functext1 TYPE smp_dyntxt, " 엑셀 폼 다운로드 버튼
gs_functext2 TYPE smp_dyntxt,
gs_functext3 TYPE smp_dyntxt.
INITIALIZATION.
PERFORM init.
SELECTION-SCREEN FUNCTION KEY 1.
gs_functext1-icon_id = icon_export.
gs_functext1-icon_text = 'EXCEL DOWN METHOD'.
gs_functext1-quickinfo = 'EXCEL DOWN METHOD'.
sscrfields-functxt_01 = gs_functext1.
SELECTION-SCREEN FUNCTION KEY 2.
gs_functext2-icon_id = icon_export.
gs_functext2-icon_text = 'EXCEL DOWN OLE2'.
gs_functext2-quickinfo = 'EXCEL DOWN OLE2'.
sscrfields-functxt_02 = gs_functext2.
SELECTION-SCREEN FUNCTION KEY 3.
gs_functext3-icon_id = icon_export.
gs_functext3-icon_text = 'EXCEL DOWN SMW0'.
gs_functext3-quickinfo = 'EXCEL DOWN SMW0'.
sscrfields-functxt_03 = gs_functext3.
AT SELECTION-SCREEN.
CASE sy-ucomm.
WHEN 'FC01'.
PERFORM excel_down_method.
WHEN 'FC02'.
PERFORM excel_down_ole2.
WHEN 'FC03'.
PERFORM excel_down_smw0 USING gv_key 'sample'.
ENDCASE.
1. MEHTOD를 이용하여 양식을 FILE로 저장하는 방법. ( PERFORM excel_down_method.)
1-1. CALL METHOD cl_gui_fronted_services=>file_save_dialog와
CALL METHOD cl_gui_fronted_services=>execute를 이용
해당 매소드로 .XLS로만 다운받을 수 있다.
*&---------------------------------------------------------------------*
*& Form excel_down_method
*&---------------------------------------------------------------------*
FORM excel_down_method .
DATA: l_filename TYPE string,
l_path TYPE string,
l_fullpath TYPE string,
lv_name TYPE rlgrap-filename,
l_filelength TYPE i.
DATA: BEGIN OF ls_format OCCURS 0,
a1(20) VALUE '자재번호',
a2(20) VALUE '산업부문',
a3(30) VALUE '자재유형',
a4(30) VALUE '자재내역',
a5(30) VALUE '기본단위',
a6(30) VALUE '자재그룹',
a7(30) VALUE '제품군',
a8(30) VALUE '일반제품구조',
a9(30) VALUE '일반품목범주 GR',
a10(30) VALUE 'DG지시자프로파일',
END OF ls_format,
lt_format LIKE TABLE OF ls_format. "lt_format에 최종적으로 엑셀 데이터가 딤김.
" 다운 받을 창을 어디에 띄울지....
" gui_download 라는 메소드를 통해 다운을 받음.
PERFORM file_save_dialog USING 'EXCEL_UPLOAD_sample.xls'
CHANGING l_filename
l_path
l_fullpath.
CHECK sy-subrc EQ 0.
APPEND ls_format TO lt_format.
* 1. gui_download 이용 : .xls 다운
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
* bin_filesize =
filename = l_fullpath
* filetype = 'ASC'
* append = SPACE
write_field_separator = 'X'
IMPORTING
filelength = l_filelength
CHANGING
data_tab = lt_format.
CHECK sy-subrc EQ 0.
CHECK NOT l_fullpath IS INITIAL.
p_file = l_fullpath.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_save_dialog
*&---------------------------------------------------------------------*
FORM file_save_dialog USING p_value
CHANGING p_filename
p_path
p_fullpath.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_extension = 'XLS'
default_file_name = p_value
CHANGING
filename = p_filename
path = p_path
fullpath = p_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
invalid_default_file_name = 4
OTHERS = 5.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_execute
*&---------------------------------------------------------------------*
FORM file_execute USING p_filename
p_path.
" ABAP에서 웹브라우저 열기.
CALL METHOD cl_gui_frontend_services=>execute
EXPORTING
document = p_filename
* application =
* parameter =
default_directory = p_path
* maximized =
* minimized =
* synchronous =
* operation = 'OPEN'
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
bad_parameter = 3
file_not_found = 4
path_not_found = 5
file_extension_unknown = 6
error_execute_failed = 7
synchronous_failed = 8
not_supported_by_gui = 9
OTHERS = 10.
" exceptions 처리를 통해서 excel 이 저장되지 않고 취소될 경우에 덤프가 발생하지 않고 온전히 처리된다
ENDFORM.
1-2. CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT' 을 이용
.XLSX 로 다운받고자 할 때 해당 펑션을 쓴다. .XLS와 .XLSX 둘 다 다운이 가능하다.
실습할 때 두 가지 방법을 쓰고자 MOVE l_fullpath TO lv_name. 을 해주었다.
그런데 이 펑션에는 문제가 있다.
문제1) 파일 덮어쓰기가 안되는 문제
문제2) 거부(x)를 눌러서 창을 끄고자 할 때 두번째 창에서 x를 누르면 short dump : MESSAGE TYPE X 가 뜨는 문제..
MOVE l_fullpath TO lv_name.
* 2. SAP_CONVERT_TO_XLS_FORMAT 이용. : .xlsx 다운
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
i_filename = lv_name " 펑션 import에 i_filename type RLGRAP-FILENAME. 맞춰주지 않으면 덤프남.
TABLES
i_tab_sap_data = lt_format
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
CHECK sy-subrc EQ 0.
PERFORM file_execute USING l_filename l_path.
" execute 대신에 그냥 저장되었다는 메세지를 써줄 수도 있다.
CHECK sy-subrc EQ 0.
CHECK NOT l_fullpath IS INITIAL.
p_file = l_fullpath.
ENDFORM.
2. OLE2_OBJECT를 이용하여 EXCEL을 실행하고 양식을 입력. ( PERFORM excel_down_ole2 .)
2-1. _TOP에 선언
" EXCEL DOWNLOAD OLE2 방식 변수들
" 데이터가 많아지면 OLE2 방식은 속도가 느려서 간단한 템플릿에 사용.
DATA: gs_application TYPE ole2_object,
gs_workbook TYPE ole2_object,
gs_sheet TYPE ole2_object,
gs_cells TYPE ole2_object,
gs_color TYPE ole2_object,
gs_font TYPE ole2_object.
2-2. 폼문
*&---------------------------------------------------------------------*
*& Form excel_down_ole2
*&---------------------------------------------------------------------*
FORM excel_down_ole2 .
DATA: l_index TYPE i.
* CHECK sscrfields-ucomm = 'FC02'.
CREATE OBJECT gs_application 'EXCEL.APPLICATION'.
SET PROPERTY OF gs_application 'visible' = 1.
CALL METHOD OF gs_application 'Workbooks' = gs_workbook.
CALL METHOD OF gs_workbook 'Add'.
" Create first Excel sheet
CALL METHOD OF gs_application 'Worksheets' = gs_sheet
EXPORTING
#1 = 1.
CALL METHOD OF gs_sheet 'Activate'.
SET PROPERTY OF gs_sheet 'NAME' = '자재등록'.
"download 데이터 포맷
PERFORM set_download_format USING '자재번호' l_index.
PERFORM set_download_format USING '산업부문' l_index.
PERFORM set_download_format USING '자재유형' l_index.
PERFORM set_download_format USING '자재내역' l_index.
PERFORM set_download_format USING '기본단위' l_index.
PERFORM set_download_format USING '자재그룹' l_index.
PERFORM set_download_format USING '제품군' l_index.
PERFORM set_download_format USING '일반제품구조' l_index.
PERFORM set_download_format USING '일반품목범주 GR' l_index.
PERFORM set_download_format USING 'DG지시자프로파일' l_index.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form set_download_format
*&---------------------------------------------------------------------*
FORM set_download_format USING p_fieldname
p_index.
p_index = p_index + 1.
CALL METHOD OF gs_application 'Cells' = gs_cells
EXPORTING
#1 = 1 " row
#2 = p_index. "colomn
SET PROPERTY OF gs_cells 'Value' = p_fieldname.
GET PROPERTY OF gs_cells 'Font' = gs_font.
SET PROPERTY OF gs_font 'Bold' = 1.
SET PROPERTY OF gs_font 'Color' = 3.
CALL METHOD OF gs_cells 'INTERIOR' = gs_color.
SET PROPERTY OF gs_color 'ColorIndex' = 7. "채우기색
SET PROPERTY OF gs_color 'Pattern' = 1. "채우기지정
ENDFORM.
▼ 결과물
3. T-Code: SMW0을 미리 저장한 양식파일을 다운하여 사용하는 방법. ( PERFORM excel_down_smw0 .)
3-0. _TOP에 선언
" SMW0 방식 변수들
DATA: gv_key TYPE w3objid VALUE 'ZEXCEL_DOWNLOAD'.
SMW0을 이용하여 다운 받는 방법에는 2가지 방법이 있다.
3-1. 첫번째 방법
*&---------------------------------------------------------------------*
*& Form excel_down_smwo
*&---------------------------------------------------------------------*
FORM excel_down_smw0 USING VALUE(pv_docname)
VALUE(pv_filename).
DATA: lt_doc_table LIKE w3mime OCCURS 0.
DATA: excel TYPE ole2_object,
books TYPE ole2_object,
cell TYPE ole2_object.
DATA: lv_doc_size TYPE i,
lv_doc_type(80) VALUE 'Excel.Sheet',
lv_doc_format(80).
DATA: lv_filename_1 TYPE string,
lv_filename_2(100).
"2진 파일로 다운로드
CALL FUNCTION 'SAP_OI_LOAD_MIME_DATA'
EXPORTING
object_id = pv_docname
IMPORTING
data_size = lv_doc_size
document_type = lv_doc_format
document_format = lv_doc_type
TABLES
data_table = lt_doc_table
EXCEPTIONS
object_not_found = 1
internal_error = 2
OTHERS = 3.
" 파일 경로 지정
CONCATENATE pv_filename '.xls' INTO lv_filename_1.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = lv_filename_1
def_path = 'C:₩'
mask = ',*.xls,*.XLS.'
mode = 'S'
title = ' '
IMPORTING
filename = lv_filename_2
EXCEPTIONS
selection_cancel = 3
OTHERS = 5.
CHECK sy-subrc EQ 0.
lv_filename_1 = lv_filename_2.
" EXCEL 파일 다운로드
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = lv_doc_size
filename = lv_filename_1
filetype = 'BIN'
* IMPORTING
* FILELENGTH =
TABLES
data_tab = lt_doc_table
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
no_authority = 5
unknown_error = 6
header_not_allowed = 7.
3-2. 두번째 방법. 위 방법보다 간단한 방법이다.
*&---------------------------------------------------------------------*
*& SMW0 간단한 방법
*&---------------------------------------------------------------------*
"1. SMW0에 저장되어 있는 오브젝트를 검색
DATA: ls_key TYPE wwwdatatab,
lv_destination TYPE rlgrap-filename,
lv_rc TYPE sy-subrc.
DATA: l_filename TYPE string,
l_path TYPE string,
l_fullpath TYPE string,
l_filelength TYPE i.
SELECT SINGLE *
FROM wwwdata
WHERE objid = 'ZDINTERN13_TEST'
INTO CORRESPONDING FIELDS OF @ls_key.
IF ls_key IS INITIAL.
MESSAGE '데이터가 없습니다.' TYPE 'S' DISPLAY LIKE 'E'.
ELSE.
"2. 해당 파일을 다운로드
"다운 받을 경로
PERFORM file_save_dialog USING '저장할파일name.XLSX'
CHANGING l_filename l_path l_fullpath.
"다운로드
IF l_fullpath IS NOT INITIAL.
lv_destination = l_fullpath.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_key
destination = lv_destination
IMPORTING
rc = lv_rc.
ENDIF.
ENDIF.
ENDFORM.
EXCEL UPLOAD
1. 엑셀 업로드를 위해 필요한 변수 선언
*-------------------------------------------------------
" EXCEL UPLOAD
" gs_excel : 업로드 할 엑셀 파일과 컬럼 구조(순서)가 같아야 한다.
DATA: BEGIN OF gs_excel.
INCLUDE TYPE ts_alv.
DATA: END OF gs_excel.
DATA: it_xls_data TYPE STANDARD TABLE OF alsmex_tabline,
is_xls_data LIKE LINE OF it_xls_data,
i_s_col TYPE i, " Excel Sheet에서 실제 데이터가 위치한 col
i_s_row TYPE i, " Excel Sheet에서 실제 데이터가 위치한 row
i_e_col TYPE i, " Excel Sheet의 총 col
i_e_row TYPE i. " Excel Sheet의 총 row
FIELD-SYMBOLS: <i_fs0>,
<i_fs1>.
2. 셀렉션 스크린
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: p_rad1 RADIOBUTTON GROUP rg1 DEFAULT 'X' USER-COMMAND rad1.
SELECTION-SCREEN COMMENT 5(8) TEXT-m01 FOR FIELD p_rad1.
PARAMETERS: p_rad2 RADIOBUTTON GROUP rg1.
SELECTION-SCREEN COMMENT 30(20) TEXT-m02 FOR FIELD p_rad2.
SELECTION-SCREEN END OF LINE.
PARAMETERS: p_file LIKE rlgrap-filename DEFAULT 'C:₩' MODIF ID gr2.
PARAMETERS: p_mode LIKE ctu_params-dismode DEFAULT 'N' MODIF ID gr2.
SELECTION-SCREEN END OF BLOCK b2.
AT SELECTION-SCREEN OUTPUT.
PERFORM modify_screen.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM iform_search_filepath USING p_file.
START-OF-SELECTION.
IF p_rad1 = 'X'.
PERFORM get_data.
ELSE.
PERFORM excel_upload.
ENDIF.
CALL SCREEN '0100'.
3. 폼문
*&---------------------------------------------------------------------*
*& Form iform_search_filepath
*&---------------------------------------------------------------------*
FORM iform_search_filepath USING p_file.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_path = 'C:₩'
mask = '*.XLS.'
mode = '0'
title = '파일선택'
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form excel_upload
*&---------------------------------------------------------------------*
FORM excel_upload .
i_s_col = 1.
" i_s_row가 2인 것은 맨 위에 한글로 입력한 헤더 데이터를 제외해야 하기 때문이다.
" (데이터가 시작되는 행을 의미함)
i_s_row = 2.
i_e_col = 220.
i_e_row = 65000.
" 콜펑션으로 엑셀에서 인터널테이블로 가져오기.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = i_s_col
i_begin_row = i_s_row
i_end_col = i_e_col
i_end_row = i_e_row
TABLES
intern = it_xls_data
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
SORT it_xls_data BY row col.
LOOP AT it_xls_data INTO is_xls_data.
TRANSLATE is_xls_data-value TO UPPER CASE.
"ASSING COMPONENT idx OF STRUCTURE strc TO <VAL>.
"structure의 구성요소 순서를 idx에서 읽어 그 순서에 위치한 구성요소의 값을 <fs>에 할당한다.
ASSIGN COMPONENT is_xls_data-col OF STRUCTURE gs_excel TO <i_fs0>.
<i_fs0> = is_xls_data-value.
"AT END OF row : INTERNAL TABLE을 읽을 때 ROW의 마지막 FIELD에서
"AT END OF 구문을 실행하여 gt_data에 excel 내용을 APPEND 하게 된다.
"TABLE이 SORT 되어있어야 하며, 명시된 필드 외의 필드는 ‘***’으로 채워진다.
AT END OF row.
MOVE-CORRESPONDING gs_excel TO gs_data.
gs_data-icon = icon_led_yellow.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_data-matnr
IMPORTING
output = gs_data-matnr.
APPEND gs_data TO gt_data.
CLEAR: gs_excel, gs_data.
ENDAT.
CLEAR: is_xls_data.
ENDLOOP.
ENDFORM.
▼ 업로드 후 결과물
프로그램
*&---------------------------------------------------------------------*
INCLUDE zed13_r8_excel_upload_top.
INCLUDE zed13_r8_excel_upload_s01.
INCLUDE zed13_r8_excel_upload_c01.
INCLUDE zed13_r8_excel_upload_o01.
INCLUDE zed13_r8_excel_upload_i01.
INCLUDE zed13_r8_excel_upload_f01.
INITIALIZATION.
PERFORM init.
SELECTION-SCREEN FUNCTION KEY 1.
gs_functext1-icon_id = icon_export.
gs_functext1-icon_text = 'EXCEL DOWN METHOD'.
gs_functext1-quickinfo = 'EXCEL DOWN METHOD'.
sscrfields-functxt_01 = gs_functext1.
SELECTION-SCREEN FUNCTION KEY 2.
gs_functext2-icon_id = icon_export.
gs_functext2-icon_text = 'EXCEL DOWN OLE2'.
gs_functext2-quickinfo = 'EXCEL DOWN OLE2'.
sscrfields-functxt_02 = gs_functext2.
SELECTION-SCREEN FUNCTION KEY 3.
gs_functext3-icon_id = icon_export.
gs_functext3-icon_text = 'EXCEL DOWN SMW0'.
gs_functext3-quickinfo = 'EXCEL DOWN SMW0'.
sscrfields-functxt_03 = gs_functext3.
AT SELECTION-SCREEN.
CASE sy-ucomm.
WHEN 'FC01'.
PERFORM excel_down_method.
WHEN 'FC02'.
PERFORM excel_down_ole2.
WHEN 'FC03'.
PERFORM excel_down_smw0 USING gv_key 'sample'.
ENDCASE.
AT SELECTION-SCREEN OUTPUT.
PERFORM modify_screen.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM iform_search_filepath USING p_file.
START-OF-SELECTION.
IF p_rad1 = 'X'.
PERFORM get_data.
ELSE.
PERFORM excel_upload.
ENDIF.
CALL SCREEN '0100'.
*-------------------------------------------------------------------------------------------------------
TOP
*-------------------------------------------------------------------------------------------------------
REPORT zed13_exam_r7_01 MESSAGE-ID oo.
TABLES: mara, makt, sscrfields, icon.
" 이벤트
CLASS lcl_event_handler DEFINITION DEFERRED.
DATA: gr_event_handler TYPE REF TO lcl_event_handler.
TYPES: BEGIN OF ts_alv,
matnr TYPE mara-matnr,
mbrsh TYPE mara-mbrsh,
mtart TYPE mara-mtart,
maktx TYPE makt-maktx,
meins TYPE mara-meins,
matkl TYPE mara-matkl,
spart TYPE mara-spart,
prdha TYPE mara-prdha,
mtpos_mara TYPE mara-mtpos_mara,
profl TYPE mara-profl,
END OF ts_alv.
DATA: BEGIN OF gs_data.
INCLUDE TYPE ts_alv.
DATA: werks TYPE t001w-werks,
icon(30),
message(50).
DATA: END OF gs_data.
DATA: gt_data LIKE TABLE OF gs_data.
DATA: BEGIN OF gs_upload.
INCLUDE STRUCTURE zed13_talv.
DATA: END OF gs_upload.
DATA: gt_upload LIKE TABLE OF gs_upload.
DATA: gv_okcode TYPE sy-ucomm.
*-------------------------------------------------------
" EXCEL DOWNLOAD & UPLOAD
*-------------------------------------------------------
DATA: gv_answer. " 팝업창 yes no 변수
DATA: gs_functext1 TYPE smp_dyntxt, " 엑셀 폼 다운로드 버튼
gs_functext2 TYPE smp_dyntxt,
gs_functext3 TYPE smp_dyntxt.
" EXCEL DOWNLOAD OLE2 방식 변수들
" 데이터가 많아지면 OLE2 방식은 속도가 느려서 간단한 템플릿에 사용.
DATA: gs_application TYPE ole2_object,
gs_workbook TYPE ole2_object,
gs_sheet TYPE ole2_object,
gs_cells TYPE ole2_object,
gs_color TYPE ole2_object,
gs_font TYPE ole2_object.
" SMW0 방식 변수들
DATA: gv_key TYPE w3objid VALUE 'ZEXCEL_DOWNLOAD'.
*-------------------------------------------------------
" EXCEL UPLOAD
" gs_excel : 업로드 할 엑셀 파일과 컬럼 구조(순서)가 같아야 한다.
DATA: BEGIN OF gs_excel.
INCLUDE TYPE ts_alv.
DATA: END OF gs_excel.
DATA: it_xls_data TYPE STANDARD TABLE OF alsmex_tabline,
is_xls_data LIKE LINE OF it_xls_data,
i_s_col TYPE i, " Excel Sheet에서 실제 데이터가 위치한 col
i_s_row TYPE i, " Excel Sheet에서 실제 데이터가 위치한 row
i_e_col TYPE i, " Excel Sheet의 총 co
i_e_row TYPE i. " Excel Sheet의 총 row
FIELD-SYMBOLS: <i_fs0>,
<i_fs1>.
" ALV
DATA: gr_docking_container TYPE REF TO cl_gui_docking_container,
gr_splitter TYPE REF TO cl_gui_splitter_container,
gr_alv_grid TYPE REF TO cl_gui_alv_grid,
gt_fcat TYPE lvc_t_fcat,
gs_fcat TYPE lvc_s_fcat,
gs_layout TYPE lvc_s_layo,
gs_variant TYPE disvariant,
gt_sort TYPE lvc_t_sort,
gs_stable TYPE lvc_s_stbl.
" Toolbar
DATA: gt_exclude TYPE ui_functions.
'ABAP' 카테고리의 다른 글
ABAP_ AT 구문 (0) | 2023.01.26 |
---|---|
Selection-screen 달의 마지막날 가져오는 function (0) | 2023.01.26 |
동적 FROM 사용하기 (0) | 2023.01.10 |
SELECT 문에서 CASE...WHEN 사용하기 (0) | 2023.01.10 |
MACRO 이용하여 SPLIT 하기 (0) | 2023.01.10 |