abap文件上传与下载

一、文件上传
  本地的文本文件或者Excel文件上传到服务器内表中,数据自动转换,文件数据列之间默认通过table键分隔,也可以自己指定。
  核心函数:TEXT_CONVERT_XLS_TO_SAP
  上传示例代码:

TYPE-POOLS:truxs.
PARAMETERS:p_file LIKE rlgrap-filename OBLIGATORY MEMORY ID a1. "MEMORY ID 表示当用户再次进入该输入框时,上次输入的文件名还在
DATA: it_raw TYPE truxs_t_text_data.
DATA:l_obj TYPE REF TO cl_gui_frontend_services.
DATA: it_file TYPE filetable WITH HEADER LINE.
DATA: g_rc TYPE i.
DATA: BEGIN OF lt_data OCCURS 0,
        c(2) TYPE c,
        n(2) TYPE n,
        i TYPE i,
        d TYPE d,
      END OFlt_data.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. "弹出选择文件对话框
  CREATE OBJECT l_obj.
  CALL METHOD l_obj->file_open_dialog
    EXPORTING
      file_filter       = '*.xls;*.xlsx;*.txt'
      initial_directory = 'C:\data'
    CHANGING
      file_table        = it_file[]
      rc                = g_rc.
  READ TABLE it_file INDEX 1.
  p_file = it_file-filename.
START-OF-SELECTION.
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'    "可以是Excel文件,也可以是Txt文件
    EXPORTING
*     I_FIELD_SEPERATOR    = 分隔符,默认为Tab
      i_line_header        = 'X' 
      i_tab_raw_data       = it_raw "该参数实际上没有使用到,但为必输参数
      i_filename           = p_file
    TABLES
      i_tab_converted_data = lt_data. "会自动的将Excel、Txt文件中的数据一行行读取到数据内表中
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
      
 IF sy-subrc <> 0.
    MESSAGE '文件上传失败!' TYPE 'E'.
 ENDIF.

二、文件下载
  下面有两个函数可以把ABAP内表中的数据下载到本地Excel文件中,分别是
SAP_CONVERT_TO_XLS_FORMAT和GUI_DOWNLOAD。
  1、SAP_CONVERT_TO_XLS_FORMAT函数

	DATA: lt_lines TYPE TABLE OF YLY_TESTTABLE1.
	SELECT * FROM YLY_TESTTABLE1 INTO lt_lines.
	CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
		EXPORTING
    	i_filename     = 'd:\1.xlsx' "下载至除C盘以外的盘,以免发生意想不到的错误
	TABLES
    	i_tab_sap_data = lt_lines
	EXCEPTIONS
    	CONVERSION_FAILED          = 1
	OTHERS                     = 2.
	
	IF SY-SUBRC <> 0.
	write:'failed'.
	ELSE.
	write:'OK'.
	ENDIF.

注:如果下载的1.xlsx文件已经存在,那么数据将会覆盖原数据。但是如果新数据的行数比原数据的行数少,那么它在原数据中只覆盖掉 新数据行数 那么多的数据;
  2、GUI_DOWNLOAD函数

DATA: lt_lines TYPE TABLE OF YLY_TESTTABLE1.
SELECT * FROM YLY_TESTTABLE1 INTO TABLE lt_lines.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
       filename = 'D:\gui_download.XLS' "下载至除C盘以外的盘,以免发生意想不到的错误
       filetype = 'DAT'
       codepage = '4103'
       replacement = '#'
       write_field_separator = 'X'
       write_bom = 'X'
TABLES
       data_tab = lt_lines.

注:如果gui_download.XLS文件已经存在,它不像SAP_CONVERT_TO_XLS_FORMAT函数一样,它会完全覆盖原数据,而不管原数据行数是否与新数据行数相等。

相关的参数英文解释如下:
1. FILETYPE

‘ASC’ :

ASCII format. The table is transferred as text. Conversion exits are performed. The output format additionally depends on the parameters CODEPAGE, TRUNC_TRAILING_BLANKS, and TRUNC_TRAILING_BLANKS_EOL.

‘IBM’ :

ASCII format with IBM codepage conversion (DOS). This format corresponds to the ‘ASC’ format when using target codepage 1103. This codepage is frequently used for data exchange via floppy disk.

‘DAT’ :

Column-by-column transfer. With this format, the data is transferred as text as with ASC. However, no conversion exits are performed and the columns are separated by tab characters. This format generates files than can be uploaded again using gui_upload or ws_upload.

‘DBF’ :

Data is downloaded in dBase format. Since in this format the data types of the individual columns are stored as well, you can often avoid import problems, for example, into Microsoft Excel, especially when interpreting numeric values.

‘WK1’ :

Data is downloaded in Lotus 1-2-3 format.

‘BIN’ :

Binary format. Data is transferred binarily. There is no formatting and no codepage conversion. The data is interpreted row by row; it is not formatted by columns. Specify the data length in parameter BIN_FILESIZE. The table should consist of a column of type X, because especially in Unicode systems, the conversion of structured into binary data causes errors.

Default

‘ASC’

2. APPEND

GUI_DOWNLOAD APPEND

By default, existing local files are overwritten by new versions. By setting APPEND to ‘X’, the downloaded data is appended to an existing file. If the file does not yet exist, it is created.

Value range

‘X’ = Data is appended.

SPACE = Data is overwritten if the file already exists.

Default

SPACE

3. WRITE_FIELD_SEPARATOR

GUI_DOWNLOAD WRITE_FIELD_SEPARATOR

In the downloaded file, the columns are separated by tab characters (cl_abap_char_utilities=>horizontal_tab). You should use this setting if you want to upload the data from the file at a later time, because this is the only way of identifying individual columns.

The parameter makes sense only for the FILETYPE values ASC, DAT and IBM; for DAT it is set implicitly.

Value range

‘X’ : Write separator.

SPACE : Do not write separator.

Default

SPACE

4. WRITE_LF

GUI_DOWNLOAD WRITE_LF

If this parameter is set, at the end of each row a row separator is inserted by CL_ABAP_CHAR_UTILITIES=>CR_LF. This parameter makes sense only for FILETYPE ‘ASC’, ‘DAT’ and ‘IBM’. If this parameter is not set, blanks at the end of a row are not removed.

Value range

‘X’: Row separator is inserted.

SPACE: Row separator is not inserted.

Default

‘X’

5. DAT_MODE

GUI_DOWNLOAD DAT_MODE

If parameter DAT_MODE is set, the file is stored in ‘DAT’ format. No conversion exits are performed. Number and date fields are stored in a standard format which makes it possible, to later import the file using gui_upload.

Value range

‘X’: DAT_MODE is switched on.

SPACE: DAT_MODE is not switched on.

Default

SPACE

6. CODEPAGE

GUI_DOWNLOAD CODEPAGE

Use parameter CODEPAGE to specify the desired target codepage. If this parameter is not set, the codepage of the SAP GUI is used as the target codepage.

Value range

4-digit number of the SAP codepage. The function module SCP_CODEPAGE_BY_EXTERNAL_NAME returns the SAP codepage number for an external character set name, for example, “iso-8859-1”. The function module NLS_GET_FRONTEND_CP returns the appropriate non-Unicode frontend codepage for a language.

You can determine the desired codepage interactively, if the parameter with_encoding of method file_save_dialog is set by cl_gui_frontend_services.

SPACE: Codepage of the SAP GUI

Default

SPACE

7. REPLACEMENT

GUI_DOWNLOAD REPLACEMENT

Specifies the replacement character to be used when during a character set conversion a character cannot be converted.

Value range

An individual character.

Default

‘#’

8. WRITE_BOM

GUI_DOWNLOAD WRITE_BOM

If the data is written in a Unicode codepage, at the beginning of the file the respective byte order mark (BOM) is inserted.

Unicode - Little Endian Codepage 4103, binary values ‘FFFE’

Unicode - Big Endian Codepage 4102, binary values ‘FEFF’

UTF-8 Codepage 4110, binary values ‘EFBBBF’

Note: Microsoft Excel only supports Unicode data if they have been written in the format Unicode - Little Endian.

Value range

‘X’: Write BOM.

SPACE: Do not write BOM.

Default

SPACE

9. FIELDNAMES

GUI_DOWNLOAD FIELDNAMES

Optional table with column names for the individual columns.

‘DBF’: The column names are entered into the structure definition of the DBF file.
‘DAT’: An additional row with the column names is inserted at the beginning of the table.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值