ABAP学习(26):ZIP打包Excel
ABAP ZIP打包
ABAP中使用cl_abap_zip类,创建zip压缩文件。
示例:
"excel文档类对象 DATA:lo_excel TYPE REF TO zcl_excel. TYPES:BEGIN OF s_spfli, carrid TYPE spfli-carrid, connid TYPE spfli-connid, countryfr TYPE spfli-countryfr, desc TYPE string, END OF s_spfli. DATA:lt_spfli TYPE TABLE OF s_spfli. DATA:ls_spfli LIKE LINE OF lt_spfli. DATA:lt_fcat TYPE lvc_t_fcat. DATA:ls_fcat TYPE lvc_s_fcat. START-OF-SELECTION. PERFORM f_get_data. PERFORM f_create_excel. PERFORM f_create_zip. "获取数据 FORM f_get_data. SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_spfli FROM spfli. LOOP AT lt_spfli INTO ls_spfli. ls_spfli-desc = '测试中文'. MODIFY lt_spfli FROM ls_spfli. ENDLOOP. "构建显示字段 CLEAR ls_fcat. ls_fcat-fieldname = 'CARRID'. ls_fcat-reptext = 'Carr.'. APPEND ls_fcat TO lt_fcat. ls_fcat-fieldname = 'CONNID'. ls_fcat-reptext = 'Conn.'. APPEND ls_fcat TO lt_fcat. ls_fcat-fieldname = 'COUNTRYFR'. ls_fcat-reptext = 'Country from'. APPEND ls_fcat TO lt_fcat. ls_fcat-fieldname = 'DESC'. ls_fcat-reptext = '描述'. APPEND ls_fcat TO lt_fcat. ENDFORM. "创建excel FORM f_create_excel. "excel worksheet类对象 DATA:lo_worksheet TYPE REF TO zcl_excel_worksheet. DATA:lv_col TYPE I. DATA:lv_row TYPE I. FIELD-SYMBOLS:<fs_val> TYPE ANY. TRY. "创建excel对象 CREATE OBJECT lo_excel. "获取当前获得worksheet lo_worksheet = lo_excel->get_active_worksheet( ). "设置worksheet lo_worksheet->set_title('sheet1'). "设置header LOOP AT lt_fcat INTO ls_fcat. lv_col = sy-tabix. lo_worksheet->set_cell( ip_column = lv_col ip_row = 1 ip_value = ls_fcat-reptext ). ENDLOOP. "设置值 LOOP AT lt_spfli INTO ls_spfli. lv_row = sy-tabix + 1. LOOP AT lt_fcat INTO ls_fcat. ASSIGN COMPONENT ls_fcat-fieldname OF STRUCTURE ls_spfli TO <fs_val>. lv_col = sy-tabix. lo_worksheet->set_cell( ip_column = lv_col ip_row = lv_row ip_value = <fs_val> ). ENDLOOP. ENDLOOP. CATCH ZCX_EXCEL. ENDTRY. ENDFORM. "excel转换为xstring,打包zip FORM f_create_zip. DATA:lo_excel_writer TYPE REF TO zif_excel_writer. DATA:lv_xstr TYPE xstring. DATA:lt_rawdata TYPE solix_tab. "字节数 DATA:lv_bytecount TYPE I. "文件名 DATA:lv_filename TYPE String VALUE 'test'. "文件路径 DATA:lv_path TYPE String. DATA:lv_fullpath TYPE String. "zip打包 DATA:lo_zip TYPE REF TO cl_abap_zip. DATA:lv_xstr_zip TYPE xstring. "创建lo_excel_writer对象 CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_2007. "获取xstring lv_xstr = lo_excel_writer->write_file( lo_excel ). "创建zip对象 CREATE OBJECT lo_zip. lo_zip->add( EXPORTING name = 'test1.xlsx' content = lv_xstr ). lo_zip->add( EXPORTING name = 'test2.xlsx' content = lv_xstr ). lo_zip->save( RECEIVING zip = lv_xstr_zip ). lt_rawdata = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xstr_zip ). lv_bytecount = xstrlen( lv_xstr_zip ). "路径选择框 cl_gui_frontend_services=>file_save_dialog( EXPORTING window_title = '选择路径:' default_extension = 'zip' CHANGING filename = lv_filename path = lv_path fullpath = lv_fullpath ). "下载excel cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount filename = lv_fullpath filetype = 'BIN' CHANGING data_tab = lt_rawdata ). ENDFORM.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/16025411.html