函数:SOTR_SERV_TABLE_TO_STRING
SCMS_STRING_TO_XSTRING
类 IF_HTTP_RESPONSE
内表数据转换为16进制存储需要先把表体内容转换成一定的格式,再通过字符串去转换
拼接表头数据和标题数据 CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB单元格分割,CL_ABAP_CHAR_UTILITIES=>NEWLINE行转换
LOOP AT <DYN_TABLE> ASSIGNING FIELD-SYMBOL(<DYN_WA>).LR_DESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <DYN_WA> )."拼接表头数据,转文本描述IF LV_HEADER_FLAG IS INITIAL.LOOP AT LR_DESCR_REF->COMPONENTS INTO DATA(LS_COM).READ TABLE IT_SELECT INTO DATA(LV_SELECT) WITH KEY TABLE_LINE = LS_COM-NAME.IF SY-SUBRC EQ 0 .READ TABLE LT_HEADER INTO LS_HEADER WITH KEY FIELDNAME = LS_COM-NAME .IF SY-SUBRC EQ 0 .CONCATENATE LS_TEXT_TAB-LINE LS_HEADER-FIELDTEXT CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TABINTO LS_TEXT_TAB-LINE SEPARATED BY ''.ENDIF.ENDIF.ENDLOOP."换行处理CONCATENATE LS_TEXT_TAB-LINE CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO LS_TEXT_TAB-LINESEPARATED BY '' .APPEND LS_TEXT_TAB TO LT_TEXT_TAB .CLEAR LS_TEXT_TAB .LV_HEADER_FLAG = 'X'.ENDIF."拼接表体数据,把每一行数据转为string类型CLEAR LS_COM .LOOP AT LR_DESCR_REF->COMPONENTS INTO LS_COM.READ TABLE IT_SELECT INTO LV_SELECT WITH KEY TABLE_LINE = LS_COM-NAME.IF SY-SUBRC EQ 0 .ASSIGN COMPONENT LS_COM-NAME OF STRUCTURE <DYN_WA> TO FIELD-SYMBOL(<FS_FIELD>) .IF SY-SUBRC EQ 0.LV_FIELD = <FS_FIELD> ."类型转换CONCATENATE LS_TEXT_TAB-LINE LV_FIELD CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB INTO LS_TEXT_TAB-LINESEPARATED BY '' .ENDIF.ENDIF .ENDLOOP."换行处理CONCATENATE LS_TEXT_TAB-LINE CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO LS_TEXT_TAB-LINESEPARATED BY '' .APPEND LS_TEXT_TAB TO LT_TEXT_TAB .CLEAR LS_TEXT_TAB .ENDLOOP.
内表数据转string
"把内表数据转为string类型CALL FUNCTION 'SOTR_SERV_TABLE_TO_STRING'EXPORTING
* FLAG_NO_LINE_BREAKS = 'X'
* LINE_LENGTH =LANGU = SY-LANGUIMPORTINGTEXT = LV_TEXTTABLESTEXT_TAB = LT_TEXT_TAB.
STRING转XSTRING,8404防乱码
"string类型-> XSTRINGCALL FUNCTION 'SCMS_STRING_TO_XSTRING'EXPORTINGTEXT = LV_TEXTMIMETYPE = 'xls'ENCODING = '8404' "防止中文乱码IMPORTINGBUFFER = LV_BUFFEREXCEPTIONSFAILED = 1OTHERS = 2.
压缩文件转储虚拟服务器
CREATE OBJECT LO_CACHED_RESPONSETYPECL_HTTP_RESPONSEEXPORTINGADD_C_MSG = 1.* ***set the data and the headersLO_CACHED_RESPONSE->SET_DATA( LV_BUFFER ).LV_APP_TYPE = 'APPLICATION/MSEXCEL; charset=utf-8'.LO_CACHED_RESPONSE->SET_HEADER_FIELD( NAME = IF_HTTP_HEADER_FIELDS=>CONTENT_TYPEVALUE = LV_APP_TYPE ).* ***Set the Response StatusLO_CACHED_RESPONSE->SET_STATUS( CODE = 200 REASON = 'OK' ).* ***Set the Cache Timeout – 60 seconds – we only need this in the cache* ***long enough to build the pageLO_CACHED_RESPONSE->SERVER_CACHE_EXPIRE_REL( EXPIRES_REL = 60 ).* ***Create a unique URL for the object and export URLCONCATENATE IV_ENTITYNAME '_' SY-DATUM SY-UZEIT INTO LS_FILE_NAME.CONCATENATE '/sap/public/ping/' LS_FILE_NAME '.' 'xls' INTO EV_XTRING.* ***Cache the URLCL_HTTP_SERVER=>SERVER_CACHE_UPLOAD( URL = EV_XTRINGRESPONSE = LO_CACHED_RESPONSE ).
完整测试用例
METHOD ZCREATE_XSLX_XSTRING.FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE.DATA: LV_FILENAME TYPE RLGRAP-FILENAME,LT_CONVERTED_DATA TYPE TRUXS_T_TEXT_DATA,LV_BUFFER TYPE XSTRING,LV_TEXT TYPE STRING,LV_FIELD TYPE STRING.DATA LR_DESCR_REF TYPE REF TO CL_ABAP_STRUCTDESCR .DATA LS_ZSNRCONFIG01 TYPE ZSNRCONFIG01 .DATA LV_HEADER_FLAG TYPE C .DATA :LV_APP_TYPE TYPE STRING,LV_GUID TYPE GUID_32,LO_CACHED_RESPONSE TYPE REF TO IF_HTTP_RESPONSE,LS_AIRPORT_STRING TYPE STRING,LS_AIRPORT_XSTRING TYPE XSTRING,LT_AIRPORT TYPE STANDARD TABLE OF SAIRPORT,LS_AIRPORT LIKE LINE OF LT_AIRPORT,LS_FILE_NAME TYPE STRING.ASSIGN IV_DATA->* TO <DYN_TABLE>.TYPES: BEGIN OF TEXT_TABLE,LINE TYPE SOTR_TXT,END OF TEXT_TABLE.DATA LV_ENTITYNAME TYPE DDOBJNAME .DATA: LT_TEXT_TAB TYPE TABLE OF TEXT_TABLE .DATA: LS_TEXT_TAB TYPE TEXT_TABLE .DATA: LT_HEADER TYPE ZDTFIES,LS_HEADER LIKE LINE OF LT_HEADER."拼接表头LV_ENTITYNAME = IV_ENTITYNAME .CALL FUNCTION 'DDIF_FIELDINFO_GET'EXPORTINGTABNAME = LV_ENTITYNAME
* FIELDNAME = ' 'LANGU = SY-LANGU
* LFIELDNAME = ' '
* ALL_TYPES = ' '
* GROUP_NAMES = ' '
* UCLEN =
* DO_NOT_WRITE = ' '
* IMPORTING
* X030L_WA =
* DDOBJTYPE =
* DFIES_WA =
* LINES_DESCR =TABLESDFIES_TAB = LT_HEADER
* FIXED_VALUES =EXCEPTIONSNOT_FOUND = 1INTERNAL_ERROR = 2OTHERS = 3.LOOP AT <DYN_TABLE> ASSIGNING FIELD-SYMBOL(<DYN_WA>).LR_DESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <DYN_WA> )."拼接表头数据,转文本描述IF LV_HEADER_FLAG IS INITIAL.LOOP AT LR_DESCR_REF->COMPONENTS INTO DATA(LS_COM).READ TABLE IT_SELECT INTO DATA(LV_SELECT) WITH KEY TABLE_LINE = LS_COM-NAME.IF SY-SUBRC EQ 0 .READ TABLE LT_HEADER INTO LS_HEADER WITH KEY FIELDNAME = LS_COM-NAME .IF SY-SUBRC EQ 0 .CONCATENATE LS_TEXT_TAB-LINE LS_HEADER-FIELDTEXT CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TABINTO LS_TEXT_TAB-LINE SEPARATED BY ''.ENDIF.ENDIF.ENDLOOP."换行处理CONCATENATE LS_TEXT_TAB-LINE CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO LS_TEXT_TAB-LINESEPARATED BY '' .APPEND LS_TEXT_TAB TO LT_TEXT_TAB .CLEAR LS_TEXT_TAB .LV_HEADER_FLAG = 'X'.ENDIF."拼接表体数据,把每一行数据转为string类型CLEAR LS_COM .LOOP AT LR_DESCR_REF->COMPONENTS INTO LS_COM.READ TABLE IT_SELECT INTO LV_SELECT WITH KEY TABLE_LINE = LS_COM-NAME.IF SY-SUBRC EQ 0 .ASSIGN COMPONENT LS_COM-NAME OF STRUCTURE <DYN_WA> TO FIELD-SYMBOL(<FS_FIELD>) .IF SY-SUBRC EQ 0.LV_FIELD = <FS_FIELD> ."类型转换CONCATENATE LS_TEXT_TAB-LINE LV_FIELD CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB INTO LS_TEXT_TAB-LINESEPARATED BY '' .ENDIF.ENDIF .ENDLOOP."换行处理CONCATENATE LS_TEXT_TAB-LINE CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO LS_TEXT_TAB-LINESEPARATED BY '' .APPEND LS_TEXT_TAB TO LT_TEXT_TAB .CLEAR LS_TEXT_TAB .ENDLOOP."把内表数据转为string类型CALL FUNCTION 'SOTR_SERV_TABLE_TO_STRING'EXPORTING
* FLAG_NO_LINE_BREAKS = 'X'
* LINE_LENGTH =LANGU = SY-LANGUIMPORTINGTEXT = LV_TEXTTABLESTEXT_TAB = LT_TEXT_TAB."string类型-> XSTRINGCALL FUNCTION 'SCMS_STRING_TO_XSTRING'EXPORTINGTEXT = LV_TEXTMIMETYPE = 'xls'ENCODING = '8404' "防止中文乱码IMPORTINGBUFFER = LV_BUFFEREXCEPTIONSFAILED = 1OTHERS = 2.IF SY-SUBRC EQ 0.ENDIF.CREATE OBJECT LO_CACHED_RESPONSETYPECL_HTTP_RESPONSEEXPORTINGADD_C_MSG = 1.* ***set the data and the headersLO_CACHED_RESPONSE->SET_DATA( LV_BUFFER ).LV_APP_TYPE = 'APPLICATION/MSEXCEL; charset=utf-8'.LO_CACHED_RESPONSE->SET_HEADER_FIELD( NAME = IF_HTTP_HEADER_FIELDS=>CONTENT_TYPEVALUE = LV_APP_TYPE ).* ***Set the Response StatusLO_CACHED_RESPONSE->SET_STATUS( CODE = 200 REASON = 'OK' ).* ***Set the Cache Timeout – 60 seconds – we only need this in the cache* ***long enough to build the pageLO_CACHED_RESPONSE->SERVER_CACHE_EXPIRE_REL( EXPIRES_REL = 60 ).* ***Create a unique URL for the object and export URLCONCATENATE IV_ENTITYNAME '_' SY-DATUM SY-UZEIT INTO LS_FILE_NAME.CONCATENATE '/sap/public/ping/' LS_FILE_NAME '.' 'xls' INTO EV_XTRING.* ***Cache the URLCL_HTTP_SERVER=>SERVER_CACHE_UPLOAD( URL = EV_XTRINGRESPONSE = LO_CACHED_RESPONSE ).LS_ZSNRCONFIG01-UNAME = SY-UNAME .LS_ZSNRCONFIG01-ENTITY = IV_ENTITYNAME .LS_ZSNRCONFIG01-URI = EV_XTRING .MODIFY ZSNRCONFIG01 FROM LS_ZSNRCONFIG01 .ENDMETHOD.