ABAP中讀取EXCEL中不同的SHEET數據
作者:侯志宇
function ZALSM_EXCEL_TO_INTERNAL_TABLE .
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(FILENAME) LIKE RLGRAP-FILENAME
*" VALUE(I_BEGIN_COL) TYPE I
*" VALUE(I_BEGIN_ROW) TYPE I
*" VALUE(I_END_COL) TYPE I
*" VALUE(I_END_ROW) TYPE I
*" VALUE(SHEET_NAME) LIKE ALSMEX_TABLINE-VALUE OPTIONAL
*" TABLES
*" INTERN STRUCTURE ALSMEX_TABLINE
*" EXCEPTIONS
*" INCONSISTENT_PARAMETERS
*" UPLOAD_OLE
*"----------------------------------------------------------------------
data: EXCEL_TAB type TY_T_SENDER.
data: LD_SEPARATOR type C.
data: APPLICATION type OLE2_OBJECT,
WORKBOOK type OLE2_OBJECT,
RANGE type OLE2_OBJECT,
WORKSHEET type OLE2_OBJECT.
data: H_CELL type OLE2_OBJECT,
H_CELL1 type OLE2_OBJECT.
data:
LD_RC type I.
* Rückgabewert der Methode "clipboard_export "
* Makro für Fehlerbehandlung der Methods
define M_MESSAGE.
CASE SY-SUBRC.
WHEN 0.
WHEN 1.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
WHEN OTHERS. RAISE UPLOAD_OLE.
ENDCASE.
end-of-definition.
* check parameters
if I_BEGIN_ROW > I_END_ROW. raise INCONSISTENT_PARAMETERS. endif.
if I_BEGIN_COL > I_END_COL. raise INCONSISTENT_PARAMETERS. endif.
* Get TAB-sign for separation of fields
class CL_ABAP_CHAR_UTILITIES definition load.
LD_SEPARATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
* open file in Excel
if APPLICATION-HEADER = SPACE or APPLICATION-HANDLE = -1.
create object APPLICATION 'Excel.Application'.
M_MESSAGE.
endif.
call method of APPLICATION 'Workbooks' = WORKBOOK.
M_MESSAGE.
call method of WORKBOOK 'Open'
exporting
#1 = FILENAME.
M_MESSAGE.
*--ADD HOUZHIYU
** set property of application 'Visible' = 1.
** m_message.
if SHEET_NAME = SPACE."用默認模式
get property of APPLICATION 'ACTIVESHEET' = WORKSHEET.
M_MESSAGE.
else.
*-->可以實現讀取多個sheet
call method of APPLICATION 'WORKSHEETS' = WORKSHEET
exporting
#1 = SHEET_NAME.
call method of WORKSHEET 'Activate'.
M_MESSAGE.
endif.
*---END OF------------------------------------------------
* mark whole spread sheet
call method of WORKSHEET 'Cells' = H_CELL
exporting
#1 = I_BEGIN_ROW
#2 = I_BEGIN_COL.
M_MESSAGE.
call method of WORKSHEET 'Cells' = H_CELL1
exporting
#1 = I_END_ROW
#2 = I_END_COL.
M_MESSAGE.
call method of WORKSHEET 'RANGE' = RANGE
exporting
#1 = H_CELL
#2 = H_CELL1.
M_MESSAGE.
call method of RANGE 'SELECT'.
M_MESSAGE.
* copy marked area (whole spread sheet) into Clippboard
call method of RANGE 'COPY'.
M_MESSAGE.
* read clipboard into ABAP
call method CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_IMPORT
importing
DATA = EXCEL_TAB
exceptions
CNTL_ERROR = 1
* ERROR_NO_GUI = 2
* NOT_SUPPORTED_BY_GUI = 3
others = 4
.
if SY-SUBRC <> 0.
message A037(ALSMEX).
endif.
perform SEPARATED_TO_INTERN_CONVERT tables EXCEL_TAB INTERN
using LD_SEPARATOR.
* clear clipboard
refresh EXCEL_TAB.
call method CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_EXPORT
importing
DATA = EXCEL_TAB
changing
RC = LD_RC
exceptions
CNTL_ERROR = 1
* ERROR_NO_GUI = 2
* NOT_SUPPORTED_BY_GUI = 3
others = 4
.
* quit Excel and free ABAP Object - unfortunately, this does not kill
* the Excel process
call method of APPLICATION 'QUIT'.
M_MESSAGE.
* >>>>> Begin of change note 575877
* to kill the Excel process it's necessary to free all used objects
free object H_CELL. M_MESSAGE.
free object H_CELL1. M_MESSAGE.
free object RANGE. M_MESSAGE.
free object WORKSHEET. M_MESSAGE.
free object WORKBOOK. M_MESSAGE.
free object APPLICATION. M_MESSAGE.
* <<<<< End of change note 575877
endfunction.
2013年3月23日 星期六
金額依幣別顯示轉換
CALL FUNCTION 'CURRENCY_AMOUNT_SAP_TO_DISPLAY'
EXPORTING
currency = 幣別
amount_internal = SAP DB 儲存金額
IMPORTING
AMOUNT_DISPLAY = 顯示金額
* EXCEPTIONS
* INTERNAL_ERROR = 1
* OTHERS = 2
.
CALL FUNCTION 'CURRENCY_CONVERTING_FACTOR'
EXPORTING
currency = lt_current-waers
IMPORTING
factor = lv_factor
EXCEPTIONS
too_many_decimals = 1
OTHERS = 2.
lt_current-dmbtr = lt_current-dmbtr * lv_factor . "依幣別轉換顯示金額
CALL FUNCTION 'CURRENCY_AMOUNT_DISPLAY_TO_SAP'
EXPORTING
currency = 幣別
amount_display = 顯示金額
IMPORTING
AMOUNT_INTERNAL = SAP DB 儲存金額
EXCEPTIONS
INTERNAL_ERROR = 1
OTHERS = 2
.
EXPORTING
currency = 幣別
amount_internal = SAP DB 儲存金額
IMPORTING
AMOUNT_DISPLAY = 顯示金額
* EXCEPTIONS
* INTERNAL_ERROR = 1
* OTHERS = 2
.
CALL FUNCTION 'CURRENCY_CONVERTING_FACTOR'
EXPORTING
currency = lt_current-waers
IMPORTING
factor = lv_factor
EXCEPTIONS
too_many_decimals = 1
OTHERS = 2.
lt_current-dmbtr = lt_current-dmbtr * lv_factor . "依幣別轉換顯示金額
CALL FUNCTION 'CURRENCY_AMOUNT_DISPLAY_TO_SAP'
EXPORTING
currency = 幣別
amount_display = 顯示金額
IMPORTING
AMOUNT_INTERNAL = SAP DB 儲存金額
EXCEPTIONS
INTERNAL_ERROR = 1
OTHERS = 2
.
ALV use top of page
ALV grid:
直接將form name 指派給 i_callback_top_of_page
ALV list:
it_events 的name要有TOP_OF_PAGE,並且指派自建的form name到form欄位中
Example:
gt_events TYPE slis_t_event WITH HEADER LINE.
gt_events-form = 'TOP_OF_PAGE'.
gt_events-name = 'TOP_OF_PAGE'.
APPEND gt_events.
也可以把SAP內建的EVENT全部叫出來
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = GT_EVENTS.
直接將form name 指派給 i_callback_top_of_page
ALV list:
it_events 的name要有TOP_OF_PAGE,並且指派自建的form name到form欄位中
Example:
gt_events TYPE slis_t_event WITH HEADER LINE.
gt_events-form = 'TOP_OF_PAGE'.
gt_events-name = 'TOP_OF_PAGE'.
APPEND gt_events.
也可以把SAP內建的EVENT全部叫出來
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = GT_EVENTS.
2013年3月20日 星期三
2013年3月14日 星期四
Excel自本機上傳SAP server
TYPE-POOLS : truxs.
DATA: file_str TYPE string.
DATA: lv_raw_data TYPE truxs_t_text_data.
DATA: BEGIN OF lt_tab1 OCCURS 0, "上傳暫存
lifnr TYPE string,
descr TYPE string,
matnr TYPE string,
ekorg TYPE string,
esokz TYPE string,
ekgrp TYPE string,
netpr TYPE string,
peinh TYPE string,
meins TYPE string,
waers TYPE string,
minbm TYPE string,
aplfz TYPE string,
datab TYPE string,
datbi TYPE string,
END OF lt_tab1.
file_str = p_file.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_tab_raw_data = lv_raw_data
i_filename = p_file
TABLES
i_tab_converted_data = lt_tab1
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
DATA: file_str TYPE string.
DATA: lv_raw_data TYPE truxs_t_text_data.
DATA: BEGIN OF lt_tab1 OCCURS 0, "上傳暫存
lifnr TYPE string,
descr TYPE string,
matnr TYPE string,
ekorg TYPE string,
esokz TYPE string,
ekgrp TYPE string,
netpr TYPE string,
peinh TYPE string,
meins TYPE string,
waers TYPE string,
minbm TYPE string,
aplfz TYPE string,
datab TYPE string,
datbi TYPE string,
END OF lt_tab1.
file_str = p_file.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_tab_raw_data = lv_raw_data
i_filename = p_file
TABLES
i_tab_converted_data = lt_tab1
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
將錯誤訊息寫入變數
Function的exceptions部份,要設定
error_message = n
才能阻擋function發出錯誤訊息
然而此法只對message type = E, A有效
如為X則仍然會強制跳出,成為short dump
DATA lv_text LIKE t100-text.
DATA: lv_msgv1 TYPE symsgv,
lv_msgv2 TYPE symsgv,
lv_msgv3 TYPE symsgv,
lv_msgv4 TYPE symsgv.
SELECT SINGLE text FROM t100 INTO lv_text
WHERE sprsl = 'M'
AND arbgb = gt_msgtab-msgid
AND msgnr = gt_msgtab-msgnr.
lv_msgv1 = gt_msgtab-msgv1. "Function或BDC傳出的錯誤訊息
lv_msgv2 = gt_msgtab-msgv2.
lv_msgv3 = gt_msgtab-msgv3.
lv_msgv4 = gt_msgtab-msgv4.
CALL FUNCTION 'FIEB_REPLACE_AMPERSAND_MESSAGE'
EXPORTING
im_text = lv_text
im_msgv1 = lv_msgv1
im_msgv2 = lv_msgv2
im_msgv3 = lv_msgv3
im_msgv4 = lv_msgv4
IMPORTING
ex_text = lv_text.
error_message = n
才能阻擋function發出錯誤訊息
然而此法只對message type = E, A有效
如為X則仍然會強制跳出,成為short dump
DATA lv_text LIKE t100-text.
DATA: lv_msgv1 TYPE symsgv,
lv_msgv2 TYPE symsgv,
lv_msgv3 TYPE symsgv,
lv_msgv4 TYPE symsgv.
SELECT SINGLE text FROM t100 INTO lv_text
WHERE sprsl = 'M'
AND arbgb = gt_msgtab-msgid
AND msgnr = gt_msgtab-msgnr.
lv_msgv1 = gt_msgtab-msgv1. "Function或BDC傳出的錯誤訊息
lv_msgv2 = gt_msgtab-msgv2.
lv_msgv3 = gt_msgtab-msgv3.
lv_msgv4 = gt_msgtab-msgv4.
CALL FUNCTION 'FIEB_REPLACE_AMPERSAND_MESSAGE'
EXPORTING
im_text = lv_text
im_msgv1 = lv_msgv1
im_msgv2 = lv_msgv2
im_msgv3 = lv_msgv3
im_msgv4 = lv_msgv4
IMPORTING
ex_text = lv_text.
ABAP BDC 模版
*BDC data declaration
DATA gt_bdctab LIKE bdcdata OCCURS 0 WITH HEADER LINE.
DATA gt_msgtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
PERFORM bdc_dynpro USING 'SAPLMEOR' '0200'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM call_transaction USING 'ME01'.
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0829 text
* -->P_0830 text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING p_program p_dynpro.
CLEAR gt_bdctab.
gt_bdctab-program = p_program.
gt_bdctab-dynpro = p_dynpro.
gt_bdctab-dynbegin = 'X'.
APPEND gt_bdctab.
ENDFORM. " BDC_DYNPRO
*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0834 text
* -->P_0835 text
*----------------------------------------------------------------------*
FORM bdc_field USING p_fnam
p_fval.
CLEAR gt_bdctab.
gt_bdctab-fnam = p_fnam.
gt_bdctab-fval = p_fval.
APPEND gt_bdctab.
ENDFORM. " BDC_FIELD
*&---------------------------------------------------------------------*
*& Form CALL_TRANSACTION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0412 text
*----------------------------------------------------------------------*
FORM call_transaction USING p_tcode.
CALL TRANSACTION p_tcode USING gt_bdctab
MODE 'N'
UPDATE 'S'
MESSAGES INTO gt_msgtab.
ENDFORM. " CALL_TRANSACTION
DATA gt_bdctab LIKE bdcdata OCCURS 0 WITH HEADER LINE.
DATA gt_msgtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
PERFORM bdc_dynpro USING 'SAPLMEOR' '0200'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM call_transaction USING 'ME01'.
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0829 text
* -->P_0830 text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING p_program p_dynpro.
CLEAR gt_bdctab.
gt_bdctab-program = p_program.
gt_bdctab-dynpro = p_dynpro.
gt_bdctab-dynbegin = 'X'.
APPEND gt_bdctab.
ENDFORM. " BDC_DYNPRO
*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0834 text
* -->P_0835 text
*----------------------------------------------------------------------*
FORM bdc_field USING p_fnam
p_fval.
CLEAR gt_bdctab.
gt_bdctab-fnam = p_fnam.
gt_bdctab-fval = p_fval.
APPEND gt_bdctab.
ENDFORM. " BDC_FIELD
*&---------------------------------------------------------------------*
*& Form CALL_TRANSACTION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0412 text
*----------------------------------------------------------------------*
FORM call_transaction USING p_tcode.
CALL TRANSACTION p_tcode USING gt_bdctab
MODE 'N'
UPDATE 'S'
MESSAGES INTO gt_msgtab.
ENDFORM. " CALL_TRANSACTION
訂閱:
文章 (Atom)