*&---------------------------------------------------------------------*
*& 包括 ZHENGZJ_TEST001_FORM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form SELECT_FILE
*&---------------------------------------------------------------------*
* 说明 选择本地的ASCII数据文件
*----------------------------------------------------------------------*
* <--T_SELFILE text
* <--P_FLAG text
*----------------------------------------------------------------------*
FORM SELECT_FILE USING P_FLAG TYPE I
CHANGING T_SELFILE.
DATA: l_rc TYPE I,
l_FileTable TYPE FILETABLE.
DATA: L_NAME TYPE STRING.
IF P_FLAG = 1.
L_NAME = '请选择文件保存路径'.
ELSE.
L_NAME = '请选择导入文件'.
ENDIF.
* Open The txt File
CALL METHOD CL_GUI_FrontEnd_Services=>File_Open_Dialog
EXPORTING
WINDOW_TITLE = L_NAME
FILE_FILTER = '文本文件 (*.XLS)|*.XLS|全部文件 (*.*)|*.*|'
MULTISELECTION = space
CHANGING
FILE_TABLE = l_FileTable
RC = l_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
others = 5.
IF SY-SUBRC = 0 AND l_rc = 1.
READ TABLE l_FileTable INTO T_SELFILE INDEX 1.
ENDIF.
ENDFORM. " SELECT_FILE
*&---------------------------------------------------------------------*
*& Form UPLOAD
*&---------------------------------------------------------------------*
* 说明:获得并处理上载数据
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM UPLOAD .
DATA: l_FileName TYPE RLGRAP-FILENAME,
l_rc TYPE I,
l_FileTable TYPE FILETABLE,
l_rlt TYPE C.
data:l_line type CHAR01.
DATA:L_LEN TYPE I,
STR TYPE STRING,
G_FILE TYPE RLGRAP-FILENAME,
ITAB TYPE HRPFDNL_T_EXCEL.
DATA WA_ITAB LIKE LINE OF ITAB.
* P_RtnCode = -1.
G_DataFile = F_INFILE.
l_rlt = CL_GUI_FRONTEND_SERVICES=>FILE_EXIST( G_DataFile ).
IF l_rlt IS INITIAL.
MESSAGE E000(ZHRIS_PA015).
EXIT.
ENDIF.
* P_RtnCode = 0.
DATA:FROM TYPE I,
TO TYPE I.
L_LEN = STRLEN( F_INFILE ).
FROM = 0 - ( 4 - L_LEN ).
TO = L_LEN - FROM.
STR = F_INFILE+FROM(TO) .
CASE STR.
WHEN '.TXT' OR '.txt'.
* Upload the file txt
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = G_DataFile
FILETYPE = 'ASC'
* HAS_FIELD_SEPARATOR = 'O'
READ_BY_LINE = 'X'
TABLES
DATA_TAB = TAB_DATA
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
DP_OUT_OF_MEMORY = 10
ACCESS_DENIED = 11.
IF SY-SUBRC <> 0.
MESSAGE E001(ZHRIS_PA015) WITH SY-TABIX.
* P_RtnCode = -1.
ENDIF.
WHEN '.xls' OR '.XLS'.
****发现此处是一个盲点,整理出来以备后用 ZHENGZJ 2011.11.18 不知道是否有更好的方法可以使用,空了再研究一下
**欢迎学习 版权所有,违者必究 O(∩_∩)O哈哈~
G_FILE = F_INFILE.
CALL FUNCTION 'HR_PK_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = G_FILE
I_BEGIN_COL = 1
I_BEGIN_ROW = 1
I_END_COL = 10
I_END_ROW = 10
CHANGING
INTERN = ITAB
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
SORT itab BY row.
DATA: descr_ref TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS: <field_name> TYPE abap_compdescr.
DATA:xfc TYPE lvc_s_fcat, "列结构
ifc TYPE lvc_t_fcat. "列内表
DATA INDEX TYPE I VALUE 1.
descr_ref ?= cl_abap_typedescr=>describe_by_data( TAB_DATA ).
LOOP AT descr_ref->components ASSIGNING <field_name>.
xfc-fieldname = <field_name>-name.
xfc-decimals = <field_name>-decimals.
xfc-inttype = <field_name>-type_kind.
xfc-COL_POS = INDEX.
APPEND xfc TO ifc.
INDEX = INDEX + 1.
ENDLOOP.
DATA: L_NAME TYPE STRING.
FIELD-SYMBOLS: <FS_FIELD> TYPE ANY. "字段(值)
LOOP AT ITAB INTO WA_ITAB.
READ TABLE IFC INTO xfc WITH KEY COL_POS = WA_ITAB-COL.
l_name = xfc-fieldname.
ASSIGN COMPONENT L_NAME OF STRUCTURE TAB_DATA TO <FS_FIELD>.
<FS_FIELD> = WA_ITAB-value.
AT END OF row.
APPEND TAB_DATA.
CLEAR TAB_DATA.
ENDAT.
ENDLOOP.
WHEN OTHERS.
ENDCASE.
DATA:L_ID(19) TYPE C,
L_STEXT(50) TYPE C.
SORT TAB_DATA BY ID DESCENDING.
LOOP AT TAB_DATA FROM 2.
IF TAB_DATA-ID = L_ID AND TAB_DATA-STEXT = L_STEXT.
MOVE-CORRESPONDING TAB_DATA to out_data.
append out_data.
clear out_data.
PERNR_LIST-pernr = TAB_DATA-pernr.
append PERNR_LIST.
clear PERNR_LIST.
else.
CLEAR: l_id, l_stext.
L_ID = TAB_DATA-ID.
L_STEXT = TAB_DATA-STEXT.
endif.
ENDLOOP.
SORT out_data by pernr.
SORT PERNR_LIST by pernr.
delete ADJACENT DUPLICATES FROM PERNR_LIST.
data:lin type i.
DESCRIBE TABLE PERNR_LIST lines lin.
write lin.
ENDFORM. " UPLOAD