zoukankan      html  css  js  c++  java
  • 2020.03.04 【ABAP随笔】- Excel批量导出7-CL_XLSX_DOCUMENT 导出

    今天写一下CL_XLSX_DOCUMENT个人觉得这个比较好用。

    导出代码如下:

    SELECT * INTO CORRESPONDING FIELDS OF TABLE @lt_zmmt001 FROM zmmt001.
          IF sy-subrc EQ 0 AND p_file IS NOT INITIAL .
            lv_filename = p_file.
            CALL METHOD zcl_document_jxzhu=>export_document_to_frontend
              EXPORTING
                pi_filename       = lv_filename
    *           iv_sheetname      = 'Sheet1'
    *          IMPORTING
    *           error_text        =
              CHANGING
                pt_tab            = lt_zmmt001
              EXCEPTIONS
                file_export_error = 1
                OTHERS            = 2.
            IF sy-subrc <> 0.
    *         Implement suitable error handling here
            ENDIF.
    METHOD export_document_to_frontend.
        IF pt_tab IS INITIAL.
          MESSAGE e001(00) RAISING file_export_error WITH |Table has no entry.|.
        ENDIF.
    
        DATA  filecontent TYPE xstring."内容 16进制
        TRY.
            DATA(xlsx_handling) = cl_ehfnd_xlsx=>get_instance( )."xlsx 句柄
            DATA(xlsx_document) = xlsx_handling->create_doc( )."xlsx 文件
            DATA(xlsx_sheets) = xlsx_document->get_sheets( )."得到sheets
            DATA(first_xlsx_sheet) = xlsx_document->get_sheet_by_id( xlsx_sheets[ 1 ]-sheet_id )."得到sheet
            first_xlsx_sheet->change_sheet_name( iv_sheetname )."设置sheet的名称
    
            DATA(lv_column) = 1."设置列 = 1
    
            DATA lw_tab_ref    TYPE REF TO data.
            CREATE DATA lw_tab_ref LIKE LINE OF pt_tab.
    
            DATA tablestructure TYPE REF TO cl_abap_structdescr.
            tablestructure ?= cl_abap_typedescr=>describe_by_data_ref( lw_tab_ref ).
    *set column name 得到列名
            LOOP AT tablestructure->components REFERENCE INTO DATA(component).
              first_xlsx_sheet->set_cell_content( iv_row = 1
                                               iv_column = lv_column
                                               iv_value = component->name ).
              lv_column = lv_column + 1.
            ENDLOOP.
    
            DATA(lv_row) = 2.
            FIELD-SYMBOLS <tabledata> TYPE STANDARD TABLE.
            ASSIGN pt_tab TO <tabledata>.
    *set data of excel 写入内表的数据
            LOOP AT <tabledata> ASSIGNING FIELD-SYMBOL(<currenttabledata>).
              lv_column = 1.
              LOOP AT tablestructure->components REFERENCE INTO component.
                ASSIGN COMPONENT component->name OF STRUCTURE <currenttabledata> TO FIELD-SYMBOL(<columnvalue>).
                first_xlsx_sheet->set_cell_content( iv_row = lv_row
                                                 iv_column = lv_column
                                                  iv_value = <columnvalue> ).
                lv_column = lv_column + 1.
              ENDLOOP.
              lv_row = lv_row + 1.
            ENDLOOP.
            filecontent = xlsx_document->save( ).
          CATCH cx_openxml_format  INTO DATA(openxml_format_exception).
            MESSAGE e001(00) RAISING file_export_error
              WITH 'Error occurs when constructing excel file instance.'." cx_openxml_format.
          CATCH cx_openxml_not_found INTO DATA(openxml_not_found_exception).
            MESSAGE e001(00) RAISING file_export_error
           WITH ' Error occurs when constructing excel file instance.' "CX_OPENXML_NOT_FOUND |.
    .
          CATCH cx_openxml_not_allowed INTO DATA(openxml_not_allowed_exception).
            MESSAGE e001(00) RAISING file_export_error
             WITH 'Error occurs when constructing excel file instance.'" CX_OPENXML_NOT_ALLOWED |.
    .
        ENDTRY.
    
    *将16进制转化为内表
        cl_scp_change_db=>xstr_to_xtab( EXPORTING im_xstring = filecontent
                                        IMPORTING ex_xtab    = DATA(filecontenttab) ).
    *输出
        cl_gui_frontend_services=>gui_download(
          EXPORTING
            bin_filesize              = xstrlen( filecontent )
            filename                  = pi_filename
            filetype                  = 'BIN'
            confirm_overwrite         = abap_true
          IMPORTING
            filelength                = DATA(bytestransferred)
          CHANGING
            data_tab                  = filecontenttab
          EXCEPTIONS
            file_write_error          = 1
            no_batch                  = 2
            gui_refuse_filetransfer   = 3
            invalid_type              = 4
            no_authority              = 5
            unknown_error             = 6
            header_not_allowed        = 7
            separator_not_allowed     = 8
            filesize_not_allowed      = 9
            header_too_long           = 10
            dp_error_create           = 11
            dp_error_send             = 12
            dp_error_write            = 13
            unknown_dp_error          = 14
            access_denied             = 15
            dp_out_of_memory          = 16
            disk_full                 = 17
            dp_timeout                = 18
            file_not_found            = 19
            dataprovider_exception    = 20
            control_flush_error       = 21
            not_supported_by_gui      = 22
            error_no_gui              = 23
            OTHERS                    = 24
        ).
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ELSE.
          MESSAGE s001(00) WITH bytestransferred ' bytes transferred'.
        ENDIF.
      ENDMETHOD.

    这个方法对于excel的导出比之前的方法速度都要快,

    而且时可以客制输出的样式的。

    METHOD export_document_to_frontend.
        
    IF pt_tab IS INITIAL.
          
    MESSAGE e001(00RAISING file_export_error WITH |Table has no entry.|.
        
    ENDIF.

        
    DATA  filecontent TYPE xstring."内容 16进制
        
    TRY.
            
    DATA(xlsx_handling) = cl_ehfnd_xlsx=>get_instance( )."xlsx 句柄
            
    DATA(xlsx_document) = xlsx_handling->create_doc( )."xlsx 文件
            
    DATA(xlsx_sheets) = xlsx_document->get_sheets( )."得到sheets
            
    DATA(first_xlsx_sheet) = xlsx_document->get_sheet_by_idxlsx_sheets[ ]-sheet_id )."得到sheet
            first_xlsx_sheet
    ->change_sheet_nameiv_sheetname )."设置sheet的名称

            
    DATA(lv_column) = 1."设置列 = 1

            
    DATA lw_tab_ref    TYPE REF TO data.
            
    CREATE DATA lw_tab_ref LIKE LINE OF pt_tab.

            
    DATA tablestructure TYPE REF TO cl_abap_structdescr.
            tablestructure ?= cl_abap_typedescr
    =>describe_by_data_reflw_tab_ref ).
    *set column name 得到列名
            
    LOOP AT tablestructure->components REFERENCE INTO DATA(component).
              first_xlsx_sheet
    ->set_cell_contentiv_row 1
                                               iv_column 
    lv_column
                                               iv_value 
    component->name ).
              lv_column 
    lv_column + 1.
            
    ENDLOOP.

            
    DATA(lv_row) = 2.
            
    FIELD-SYMBOLS <tabledata> TYPE STANDARD TABLE.
            
    ASSIGN pt_tab TO <tabledata>.
    *set data of excel 写入内表的数据
            
    LOOP AT <tabledata> ASSIGNING FIELD-SYMBOL(<currenttabledata>).
              lv_column 
    1.
              
    LOOP AT tablestructure->components REFERENCE INTO component.
                
    ASSIGN COMPONENT component->name OF STRUCTURE <currenttabledata> TO FIELD-SYMBOL(<columnvalue>).
                first_xlsx_sheet
    ->set_cell_contentiv_row lv_row
                                                 iv_column 
    lv_column
                                                  iv_value 
    <columnvalue> ).
                lv_column 
    lv_column + 1.
              
    ENDLOOP.
              lv_row 
    lv_row + 1.
            
    ENDLOOP.
            filecontent 
    xlsx_document->save( ).
          
    CATCH cx_openxml_format  INTO DATA(openxml_format_exception).
            
    MESSAGE e001(00RAISING file_export_error
              
    WITH 'Error occurs when constructing excel file instance.'." cx_openxml_format.
          
    CATCH cx_openxml_not_found INTO DATA(openxml_not_found_exception).
            
    MESSAGE e001(00RAISING file_export_error
           
    WITH ' Error occurs when constructing excel file instance.' "CX_OPENXML_NOT_FOUND |.
    .
          
    CATCH cx_openxml_not_allowed INTO DATA(openxml_not_allowed_exception).
            
    MESSAGE e001(00RAISING file_export_error
             
    WITH 'Error occurs when constructing excel file instance.'" CX_OPENXML_NOT_ALLOWED |.
    .
        
    ENDTRY.

    *16进制转化为内表
        cl_scp_change_db
    =>xstr_to_xtabEXPORTING im_xstring filecontent
                                        
    IMPORTING ex_xtab    DATA(filecontenttab) ).
    *输出
        cl_gui_frontend_services
    =>gui_download(
          
    EXPORTING
            bin_filesize              
    xstrlenfilecontent )
            filename                  
    pi_filename
            filetype                  
    'BIN'
            confirm_overwrite         
    abap_true
          
    IMPORTING
            filelength                
    DATA(bytestransferred)
          
    CHANGING
            data_tab                  
    filecontenttab
          
    EXCEPTIONS
            file_write_error          
    1
            no_batch                  
    2
            gui_refuse_filetransfer   
    3
            invalid_type              
    4
            no_authority              
    5
            unknown_error             
    6
            header_not_allowed        
    7
            separator_not_allowed     
    8
            filesize_not_allowed      
    9
            header_too_long           
    10
            dp_error_create           
    11
            dp_error_send             
    12
            dp_error_write            
    13
            unknown_dp_error          
    14
            access_denied             
    15
            dp_out_of_memory          
    16
            disk_full                 
    17
            dp_timeout                
    18
            file_not_found            
    19
            dataprovider_exception    
    20
            control_flush_error       
    21
            not_supported_by_gui      
    22
            error_no_gui              
    23
            
    OTHERS                    24
        
    ).
        
    IF sy-subrc <> 0.
          
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                     
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        
    ELSE.
          
    MESSAGE s001(00WITH bytestransferred ' bytes transferred'.
        
    ENDIF.
      
    ENDMETHOD.

  • 相关阅读:
    Python自动化交易学习笔记(九)——将添加佣金数据,来使回测更逼近与真实交易。
    python自动化交易学习笔记五-Demo8-策略已经设置了买入条件-设置卖出条件
    python自动化交易学习笔记(四)第一个策略回测程序Demo7(添加买入条件-当股价连续下跌2天后买入)
    python量化交易学习笔记(三)——第一个策略回测程序Demo6
    取sql server 图片二进制流
    JDBC连接sql取数据
    量化交易学习笔记(二)-实例化Cerebro引擎-代理已经手握一笔资金准备进行交易
    maven本地化jbarcode-0.2.8.jar
    量化自动化交易python学习笔记之(一)BaoStock使用A股K线数据股票代码sh.60000,四年历史数据,用于后期追溯测试和策略可行性
    Angularjs插件ui-select的使用方法
  • 原文地址:https://www.cnblogs.com/jxzhu/p/12407823.html
Copyright © 2011-2022 走看看