REPORT OO_ALV.
CLASS OO_ALV DEFINITION.
PUBLIC SECTION.
METHODS:GET_DATA IMPORTING AMOUNT TYPE I,"取数
DISPLAY_ALV,"显示ALV
DOWNLOAD_ALV."下载ALV
PRIVATE SECTION.
DATA:IT_SPFLI TYPE STANDARD TABLE OF SPFLI.
DATA:LO_SALV TYPE REF TO CL_SALV_TABLE.
DATA:O_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE.
DATA: LO_CONVERTER TYPE REF TO ZCL_EXCEL_CONVERTER.
ENDCLASS. CLASS OO_ALV IMPLEMENTATION.
METHOD GET_DATA.
SELECT * FROM SPFLI INTO CORRESPONDING FIELDS OF TABLE IT_SPFLI UP TO AMOUNT ROWS .
ENDMETHOD.
METHOD DISPLAY_ALV.
TRY.
CL_SALV_TABLE=>FACTORY(
EXPORTING
LIST_DISPLAY = ABAP_FALSE
IMPORTING
R_SALV_TABLE = LO_SALV
CHANGING
T_TABLE = IT_SPFLI ).
CATCH CX_SALV_MSG .
ENDTRY.
O_COLUMNS = LO_SALV->GET_COLUMNS( ).
O_COLUMNS->SET_OPTIMIZE( ABAP_TRUE ). "自动优化列宽
"调用显示alv
LO_SALV->DISPLAY( ).
ENDMETHOD.
METHOD DOWNLOAD_ALV.
"创建zcl_excel_converter类
CREATE OBJECT LO_CONVERTER.
TRY .
LO_CONVERTER->CONVERT(
EXPORTING
IO_ALV = LO_SALV
IT_TABLE = IT_SPFLI
I_ROW_INT =
I_COLUMN_INT =
).
CATCH ZCX_EXCEL.
ENDTRY.
"调用write_file方法导出excel
LO_CONVERTER->WRITE_FILE( I_PATH = 'D:/test1.xlsx' ).
ENDMETHOD.
ENDCLASS. START-OF-SELECTION.
DATA(OUTPUT) = NEW OO_ALV( ).
OUTPUT->GET_DATA( ).
OUTPUT->DISPLAY_ALV( ).
OUTPUT->DOWNLOAD_ALV( ).