ABAP學習(16):ABAP2XLSX,操做excel

ABAP2XLSX

    ABAP2XLS是一個操做EXCEL的開源項目,相比較OLE2和DOI方式操做excel更加方便。ios

    ABAP中搜索ZDEMO_EXCEL*搜索Program能夠查看SAP提供的示例程序,實現多種Excel。windows

1.建立excel

    Excel類:zcl_excel類,表明一個excel文檔;網絡

    Worksheet類:zcl_excel_worksheet類,表明一個worksheet;app

    異常類:zcx_excel類,異常類;less

    超連接類:zcl_excel_hyperlink類,經過設置超連接可連接到指定sheet欄位或者打開網絡連接;frontend

    建立excel過程:ide

    一、定義zcl_excel類對象對應變量lo_excel;字體

    二、CREATE OBJECT lo_excel,建立zcl_excel類對象;ui

    三、經過調用zcl_excel的get_active_sheet方法,獲取當前活動sheet,當建立zcl_excel對象後,默認有一個worksheet處於活動;this

    四、調用add_new_worksheet方法,能夠建立新的worksheet;

示例:

"excel文檔類對象
DATA:lo_excel TYPE REF TO zcl_excel. "excel worksheet類對象
DATA:lo_worksheet TYPE REF TO zcl_excel_worksheet. "異常類
DATA:lf_cxexcel TYPE REF TO ZCX_EXCEL. "建立excel
FORM creat_excel. "excel超連接
  DATA:lo_hyperlink TYPE REF TO zcl_excel_hyperlink. TRY. "建立excel對象
      CREATE OBJECT lo_excel. "得到當前worksheet lo_worksheet = lo_excel->get_active_worksheet( ). "設置單元格 lo_worksheet->set_cell( ip_column = 'A' ip_row = 1 ip_value = 'hello world!' ). lo_worksheet->set_cell( ip_column = 'A' ip_row = 2 ip_value = sy-datum ). lo_worksheet->set_cell( ip_column = 'A' ip_row = 3 ip_value = sy-uzeit ). "建立超連接
      "外部連接 lo_hyperlink = zcl_excel_hyperlink=>create_external_link( iv_url = 'https://www.baidu.com' ). lo_worksheet->set_cell( ip_column = 'A' ip_row = 4 ip_value = '百度一下' ip_hyperlink = lo_hyperlink ). "內部連接到其餘sheet lo_hyperlink = zcl_excel_hyperlink=>create_internal_link( iv_location = 'itab!A1' ). lo_worksheet->set_cell( ip_column = 'A' ip_row = 5 ip_value = 'itab:A1' ip_hyperlink = lo_hyperlink ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

2.excel下載與顯示

    Excel下載和顯示須要將excel轉換成xstring操做。

    經過類型zif_excel_writer的cl_writer對象,調用write_file方法,將zcl_excel對象轉換成xstring。將xstring轉換爲solix_tab類型變量,方便顯示和下載操做。

2.1將excel轉換xstring

    使用zif_excel_writer類,若是須要下載成xlsx類型文檔,建立實例化對象時類型爲zcl_excel_writer_2007;經過write_file方法獲取xstring類型數據;再將xstring類型data轉換爲solix_tab類型數據。

示例:

"下載顯示excel轉換
DATA:cl_writer TYPE REF TO zif_excel_writer. DATA:xdata TYPE xstring. DATA:t_rawdata TYPE solix_tab. DATA:bytecount TYPE i. "excel轉換成xstring
FORM translate_excel. "實例化cl_writer對象
  CREATE OBJECT cl_writer TYPE zcl_excel_writer_2007. xdata = cl_writer->write_file( lo_excel ). * "方式1:convert to binary
* CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
* EXPORTING
* buffer = xdata
* IMPORTING
* output_length = bytecount
* TABLES
* binary_tab = t_rawdata.

  "方式2:This method is only available on AS ABAP > 6.40
  "excel轉換 t_rawdata = cl_bcs_convert=>xstring_to_solix( iv_xstring  = xdata ). "統計字節數 bytecount = xstrlen( xdata ). ENDFORM.

2.2GUI直接顯示excel

示例:

"在ABAP顯示
FORM display_excel. DATA:error TYPE REF TO i_oi_error. DATA:t_errors TYPE STANDARD TABLE OF REF TO i_oi_error WITH NON-UNIQUE DEFAULT KEY. DATA:cl_control  TYPE REF TO i_oi_container_control. "OIContainerCtrl
  DATA:cl_document TYPE REF TO i_oi_document_proxy.   "Office Dokument
 c_oi_container_control_creator=>get_container_control( IMPORTING control = cl_control error = error ). APPEND error TO t_errors. cl_control->init_control( EXPORTING inplace_enabled = 'X' no_flush = 'X' r3_application_name = 'Demo Document Container' parent = cl_gui_container=>screen0 IMPORTING error = error EXCEPTIONS OTHERS = 2 ). APPEND error TO t_errors. cl_control->get_document_proxy( EXPORTING document_type = 'Excel.Sheet'                " EXCEL no_flush = ' '
    IMPORTING document_proxy = cl_document error = error ). APPEND error TO t_errors. cl_document->open_document_from_table( EXPORTING document_size = bytecount document_table = t_rawdata open_inplace = 'X' ). WRITE: '.'.  " To create an output. That way screen0 will exist
ENDFORM.

2.3導出excel到本地

    主要使用cl_gui_frontend_services=>gui_download方法下載。

示例:

"下載到本地
FORM download_excel. DATA:filepath TYPE String VALUE 'C:'. DATA:filename TYPE String VALUE 'abap2xlsx1'. "選擇文件保存路徑 cl_gui_frontend_services=>directory_browse( EXPORTING window_title = 'Select path to download EXCEL-file' initial_folder = filepath CHANGING selected_folder = filepath EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4 ). filename = filepath && '\' && filename && '.XLSX'.
"下載excel cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = bytecount filename = filename filetype =
'BIN' CHANGING data_tab = t_rawdata ). ENDFORM.

2.4將excel保存爲csv文件

    使用zif_excel_writer類,若是須要下載成csv類型文檔,建立實例化對象時類型爲zcl_excel_writer_csv;調用write_file方法將zcl_excel轉換爲xstring;再將xstring類型數據轉換爲solix_tab類型數據。這個是單個sheet下載,使用zcl_excel_writer_csv類對象的set_active_sheet_index()設置要下載的worksheet。

示例:

"excel轉換下載成csv
FORM translate_csv. "cl_writer對象
  CREATE OBJECT cl_writer TYPE zcl_excel_writer_csv. zcl_excel_writer_csv=>set_delimiter( ip_value = cl_abap_char_utilities=>horizontal_tab ). zcl_excel_writer_csv=>set_enclosure( ip_value = '''' ). zcl_excel_writer_csv=>set_endofline( ip_value = cl_abap_char_utilities=>cr_lf ). "單個sheet寫入,set_active_sheet_index_by_name( ) zcl_excel_writer_csv=>set_active_sheet_index( i_active_worksheet = 2 ). xdata = cl_writer->write_file( lo_excel ). "後續操做和excel一致,文件後綴.csv
ENDFORM.

2.5文件類型xlsm讀寫

    使用zif_excel_writer類,若是須要下載成xlsm類型文檔,建立實例化對象時類型爲zcl_excel_writer_xlsm.經過write_file方法獲取xstring類型數據;再將xstring類型data轉換爲solix_tab類型數據。

    使用zif_excel_reader類,建立對象時類型爲zcl_excel_reader_xlsm,經過類對象的load_file()方法加載xlsm類型文檔。

示例:

"讀寫xlsm類型文檔
FORM translate_xlsm. DATA:lo_excel_writer TYPE REF TO zif_excel_writer. DATA:lo_excel_reader TYPE REF TO zif_excel_reader. TRY . CREATE OBJECT lo_excel_reader TYPE zcl_excel_reader_xlsm. CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_xlsm. lo_excel = lo_excel_reader->load_file( '文件路徑' ). xdata = lo_excel_writer->write_file( lo_excel ). "後續操做和excel一致,文件後綴.xlsm
    CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

2.6alv導出到excel

    使用zcl_excel_converter類導出alv到excel。

    一、建立zcl_excel_converter對象;

    二、調用convert方法,傳入cl_salv_table和內表等參數;

    三、調用writer_file方法導出到excel;

示例1:

"使用zcl_excel_converter導出excel
FORM export_excel_conv. DATA:t_sflight TYPE TABLE OF sflight. DATA:lo_salv TYPE REF TO cl_salv_table. SELECT * INTO TABLE t_sflight FROM sflight UP TO 10 ROWS. TRY. cl_salv_table=>factory( EXPORTING list_display = abap_false IMPORTING r_salv_table = lo_salv CHANGING t_table = t_sflight[] ). CATCH cx_salv_msg . ENDTRY. "調用顯示alv
* lo_salv->display( ).
  DATA: lo_converter TYPE REF TO zcl_excel_converter. "建立zcl_excel_converter類
  CREATE OBJECT lo_converter. TRY . lo_converter->convert( EXPORTING io_alv = lo_salv it_table = t_sflight i_row_int = 2 i_column_int = 2 ). CATCH zcx_excel. ENDTRY. "調用write_file方法導出excel lo_converter->write_file( i_path = 'D:/test.xlsx' ). ENDFORM.                    "EXPORT_TO_EXCEL_CONV

 

    使用zcl_excel_worksheet導出alv到excel。

    一、調用zcl_excel_worksheet對象的方法bind_alv(),將alv和worksheet綁定;

    二、而後zif_excel_writer接口實現對象,將lo_excel轉換爲xstring,而後再轉換爲solix_tab類型,最後調用cl_gui_frontend_services的gui_download方法導出excel。

示例2:

"使用worksheet的bind_alv方法
FORM export_excel_bind. DATA:t_sflight TYPE TABLE OF sflight. DATA:lo_salv TYPE REF TO cl_salv_table. SELECT * INTO TABLE t_sflight FROM sflight UP TO 10 ROWS. TRY. cl_salv_table=>factory( EXPORTING list_display = abap_false IMPORTING r_salv_table = lo_salv CHANGING t_table = t_sflight[] ). CATCH cx_salv_msg . ENDTRY. "調用顯示alv
* lo_salv->display( ).

  TRY . "將alv綁定到worksheet,
      "只是綁定,導出excel須要調用gui_download方法 lo_worksheet->bind_alv( io_alv = lo_salv it_table = t_sflight i_top = 2 i_left = 1 ). CATCH zcx_excel. ENDTRY. "下載excel操做
  "zcl_excel_writer對象,實例化
  CREATE OBJECT cl_writer TYPE zcl_excel_writer_2007. xdata = cl_writer->write_file( lo_excel ). t_rawdata = cl_bcs_convert=>xstring_to_solix( iv_xstring  = xdata ). "統計字節數 bytecount = xstrlen( xdata ). "下載excel cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = bytecount filename = 'D:/test.xlsx' filetype = 'BIN'
    CHANGING data_tab = t_rawdata ). ENDFORM.

   

  使用zcl_excel_worksheet的bind_alv_ole2方法直接導出。

    一、經過get_globals_from_slvc_fullscr方法獲取屏幕的alv對象;

    二、直接調用bind_alv_ole2方法,就能夠將alv導出到excel。

示例3:

"經過worksheet的bind_alv_ole2方法,將alv導出
FORM export_excel_bind1. "屏幕alv對象
  DATA:lo_alv TYPE REF TO cl_gui_alv_grid. "excel header
  DATA:gt_listheader TYPE slis_t_listheader. DATA:wa_listheader LIKE LINE OF gt_listheader. "excel保存路徑
  DATA:l_path TYPE string VALUE 'C:temp/test.xlsx'. "獲取屏幕alv對象
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING e_grid = lo_alv. wa_listheader-typ = 'H'. wa_listheader-info = 'header line'. APPEND wa_listheader TO gt_listheader. wa_listheader-typ = 'S'. wa_listheader-info = 'second line'. APPEND wa_listheader TO gt_listheader. wa_listheader-typ = 'A'. wa_listheader-info = '地址信息'. APPEND wa_listheader TO gt_listheader. "調用lo_worksheet的bind_alv_ole2方法,直接調用下載 lo_worksheet->bind_alv_ole2( EXPORTING
* I_DOCUMENT_URL = SPACE " excel template
* I_XLS = 'X' " create in xls format? i_save_path = l_path io_alv = lo_alv it_listheader = gt_listheader i_top = 2 i_left = 1
* I_COLUMNS_HEADER = 'X'
* I_COLUMNS_AUTOFIT = 'X'
* I_FORMAT_COL_HEADER =
* I_FORMAT_SUBTOTAL =
* I_FORMAT_TOTAL =
    EXCEPTIONS miss_guide = 1 ex_transfer_kkblo_error = 2 fatal_error = 3 inv_data_range = 4 dim_mismatch_vkey = 5 dim_mismatch_sema = 6 error_in_sema = 7 OTHERS = 8 ). IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDFORM.

2.7excel上傳

    經過zcl_excel_reader_2007類實例化對象cl_reader,調用load_file方法,獲取zcl_excel類對象lo_excel。經過lo_excel獲取worksheet,而後經過worksheet獲取上傳excel單元格內容,將單元格內容保存到內表。

示例:

"讀取excel

FORM read_excel. "excel文檔類對象

  DATA:lo_excel TYPE REF TO zcl_excel. "excel worksheet類對象

  DATA:lo_worksheet TYPE REF TO zcl_excel_worksheet. "異常類

  DATA:lf_cxexcel TYPE REF TO ZCX_EXCEL. "上傳excel

  DATA:cl_reader TYPE REF TO zif_excel_reader. "excel導入內表

  DATA:BEGIN OF t_excel OCCURS 0, str1 TYPE string, str2 TYPE string, str3 TYPE string, str4 TYPE string, END OF t_excel. "讀取行列數

  DATA:col_count TYPE I. DATA:row_count TYPE I. DATA:col TYPE I. DATA:row TYPE I. TRY. CREATE OBJECT lo_excel. "cl_reader對象

      CREATE OBJECT cl_reader TYPE zcl_excel_reader_2007. lo_excel = cl_reader->load_file( i_filename = 'D:\test.xlsx' ). lo_worksheet = lo_excel->get_active_worksheet( ). "獲取行數,列數
 row_count = lo_worksheet->get_highest_row( ). col_count = lo_worksheet->get_highest_column( ). DO row_count TIMES. row = sy-index. CLEAR t_excel. DO col_count TIMES. col = sy-index. CASE col. WHEN 1. lo_worksheet->get_cell( EXPORTING ip_column = col ip_row = row IMPORTING ep_value = t_excel-str1 ). WHEN 2. lo_worksheet->get_cell( EXPORTING ip_column = col ip_row = row IMPORTING ep_value = t_excel-str2 ). WHEN 3. lo_worksheet->get_cell( EXPORTING ip_column = col ip_row = row IMPORTING ep_value = t_excel-str3 ). WHEN 4. lo_worksheet->get_cell( EXPORTING ip_column = col ip_row = row IMPORTING ep_value = t_excel-str4 ). ENDCASE. ENDDO. APPEND t_excel. ENDDO. LOOP AT t_excel. WRITE:/ t_excel-str1,t_excel-str2,t_excel-str3,t_excel-str4. ENDLOOP. CATCH ZCX_EXCEL INTO lf_cxexcel. "獲取錯誤信息

      DATA:result TYPE string. CALL METHOD lf_cxexcel->IF_MESSAGE~GET_TEXT RECEIVING result = result. MESSAGE result TYPE 'E'. ENDTRY. ENDFORM.

3.設置workbook屬性

    設置workbook保護,禁止編輯、新增worksheet。

示例:

"設置workbook級別protection,禁止編輯新建worksheet
FORM set_workbook. lo_excel->zif_excel_book_protection~protected = zif_excel_book_protection=>c_protected. lo_excel->zif_excel_book_protection~lockrevision = zif_excel_book_protection=>c_locked. lo_excel->zif_excel_book_protection~lockstructure = zif_excel_book_protection=>c_locked. lo_excel->zif_excel_book_protection~lockwindows = zif_excel_book_protection=>c_locked. lo_excel->zif_excel_book_protection~workbookpassword = zcl_excel_common=>encrypt_password( 'secret' ). lo_excel->zif_excel_book_protection~revisionspassword = zcl_excel_common=>encrypt_password( 'secret' ). ENDFORM.

4.設置worksheet屬性

    一、獲取zcl_excel_worksheet類對象;

    經過zcl_excel對象調用add_new_worksheet方法建立類zcl_excel_worksheet實例對象lo_worksheet;

    經過lo_worksheet調用對應方法設置worksheet屬性;

示例:

"設置worksheet
FORM set_worksheet. "sheet顏色
  DATA:ls_tabcolor TYPE ZEXCEL_S_TABCOLOR. "打印頭和腳
  DATA:ls_header  TYPE zexcel_s_worksheet_head_foot. DATA:ls_footer  TYPE zexcel_s_worksheet_head_foot. TRY . "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'sheet2' ). lo_worksheet->zif_excel_sheet_properties~selected = zif_excel_sheet_properties=>c_selected.
"設置sheet標籤顏色 ls_tabcolor-rgb = zcl_excel_style_color=>c_blue. lo_worksheet->set_tabcolor( iv_tabcolor = ls_tabcolor ).
"設置sheet隱藏 * lo_worksheet->zif_excel_sheet_properties~hidden = zif_excel_sheet_properties=>c_hidden.
"設置隱藏0 lo_worksheet->set_cell( ip_column = 'C' ip_row = 1 ip_value = 'C2隱藏0:' ). lo_worksheet->set_cell( ip_column = 'C' ip_row = 2 ip_value = 0 ). lo_worksheet->zif_excel_sheet_properties~show_zeros = zif_excel_sheet_properties=>c_hidezero. "sheet打印設置,printing settings lo_worksheet->sheet_setup->set_page_margins( ip_header = '1' ip_footer = '1' ip_unit = 'cm' ). lo_worksheet->sheet_setup->black_and_white = 'X'. lo_worksheet->sheet_setup->fit_to_page = 'X'. " you should turn this on to activate fit_to_height and fit_to_width lo_worksheet->sheet_setup->fit_to_height = 0. " used only if ip_fit_to_page = 'X' lo_worksheet->sheet_setup->fit_to_width = 2. " used only if ip_fit_to_page = 'X' lo_worksheet->sheet_setup->orientation = zcl_excel_sheet_setup=>c_orientation_landscape. lo_worksheet->sheet_setup->page_order = zcl_excel_sheet_setup=>c_ord_downthenover. lo_worksheet->sheet_setup->paper_size = zcl_excel_sheet_setup=>c_papersize_a4. lo_worksheet->sheet_setup->scale = 80. " used only if ip_fit_to_page = SPACE "設置打印頭和腳 " Header and Footer ls_header-right_value = 'print date &D'. ls_header-right_font-size = 8. ls_header-right_font-name = zcl_excel_style_font=>c_name_arial. ls_footer-left_value = '&Z&F'. "Path / Filename ls_footer-left_font = ls_header-right_font. ls_footer-right_value = 'page &P of &N'. "page x of y ls_footer-right_font = ls_header-right_font. lo_worksheet->sheet_setup->set_header_footer( ip_odd_header = ls_header ip_odd_footer = ls_footer ). "設置隱藏列 lo_worksheet->zif_excel_sheet_properties~hide_columns_from = 'M'. "設置行收縮展開,lv_collapsed:true收縮,false展開 lo_worksheet->set_row_outline( iv_row_from = 10 iv_row_to = 16 iv_collapsed = abap_true ). " collapsed "設置sheet保護 DATA:lo_style TYPE REF TO zcl_excel_style. DATA:lv_style_guid TYPE zexcel_cell_style. lo_worksheet->zif_excel_sheet_protection~protected = zif_excel_sheet_protection=>c_protected. "設置密碼加密 lo_worksheet->zif_excel_sheet_protection~password = zcl_excel_common=>encrypt_password( 'secret' ). lo_worksheet->zif_excel_sheet_protection~sheet = zif_excel_sheet_protection=>c_active. lo_worksheet->zif_excel_sheet_protection~objects = zif_excel_sheet_protection=>c_active. lo_worksheet->zif_excel_sheet_protection~scenarios = zif_excel_sheet_protection=>c_active. lo_style = lo_excel->add_new_style( ). "設置unlocked鎖住樣式 lo_style->protection->locked = zcl_excel_style_protection=>c_protection_unlocked. lv_style_guid = lo_style->get_guid( ). "設置可編輯 lo_worksheet->set_cell( ip_column = 'E' ip_row = 2 ip_value = 'unlocked' ip_style = lv_style_guid ). "設置單元格尺寸 lo_worksheet->zif_excel_sheet_properties~zoomscale = 150. * lo_worksheet->zif_excel_sheet_properties~zoomscale_normal = 150. * lo_worksheet->zif_excel_sheet_properties~zoomscale_pagelayoutview = 150. * lo_worksheet->zif_excel_sheet_properties~zoomscale_sheetlayoutview = 150. "設置是否顯示打印sheet網格線 lo_worksheet->set_show_gridlines( i_show_gridlines = abap_false ). lo_worksheet->set_print_gridlines( i_print_gridlines = abap_false ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

   2. 獲取設置當前活動worksheet,特別注意操做多個worksheet時,當前哪一個worksheet處於活動狀態。

示例:

"設置獲取active worksheet
FORM set_active_sheet. "當建立lo_excle->add_new_worksheet(),當前建立worksheet處於active狀態
  "get_active_sheet_index()方法,獲取活動worksheet的index
  "get_active_worksheet()方法,獲取活動worksheet
  "set_active_sheet_index()方法,經過index設置活動worksheet
  "set_active_sheet_index_by_name()方法,
ENDFORM.

5.設置單元格樣式

    設置單元格樣式:主要是zcl_excel_style類,使用zexcel_cell_style數據類型保存guid。

    一、經過excel對象lo_excel調用add_new_style,建立zcl_excel_style類對象;

    二、經過zcl_excel_style對象屬性font,fill,borders,alignment,number_format等,設置顯示style;

    三、經過zcl_excel_style對象的get_guid方法獲取style對應的guid;

    四、zcl_excel_worksheet類lo_worksheet的set_cell方法輸入參數ip_style等於對應獲取的guid;

示例:

"設置單元格樣式
FORM set_style. "style類
  DATA:lo_style TYPE REF TO zcl_excel_style. "style的guid
  DATA:lv_style_guid TYPE zexcel_cell_style.TRY. "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'sheet3' ). CATCH ZCX_EXCEL. ENDTRY. 
  "字體設置,類:zcl_excel_style_font
  "建立一個新style lo_style = lo_excel->add_new_style( ). "加粗 lo_style->font->bold   = abap_true. "斜體 lo_style->font->italic = abap_true. "下劃線 lo_style->font->underline = abap_true. "下劃線mode,單下劃線雙下劃線 lo_style->font->underline_mode = zcl_excel_style_font=>c_underline_double. "字體family,C_FAMILY_XXX lo_style->font->family = zcl_excel_style_font=>c_family_roman. "字體名:c_name_XXX,直接設置字體名:'YouYuan'
* lo_style->font->name = zcl_excel_style_font=>c_name_arial. lo_style->font->name   = 'YouYuan'. "字體主題:C_SCHEME_XXX lo_style->font->scheme = zcl_excel_style_font=>c_scheme_none. "字體顏色,使用CSS顏色值:後面六位,前兩位默認FF
* lo_style->font->color-rgb = zcl_excel_style_color=>c_red. lo_style->font->color-rgb = 'FF66FFCC'. "自定義color,設置紅綠藍;create_new_argb方法和create_new_argb_int方法,返回zecel_style_color_argb類型
* zcl_excel_style_color=>create_new_argb( ip_red = 'FF' ip_green = '00' ip_blu = '00' ).
  "顏色對應結構體ZEXCEL_S_STYLE_COLOR
* zexcel_s_style_color-rgb對應:ZEXCEL_STYLE_COLOR_ARGB類型
* zexcel_s_style_color-indexed 對應:ZEXCEL_STYLE_COLOR_INDEXED類型
* zexcel_s_style_color-theme 對應:ZEXCEL_STYLE_COLOR_THEME類型
* zexcel_s_style_color-tint 對應:ZEXCEL_STYLE_COLOR_TINT類型
  "字體大小 lo_style->font->size = 11. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 1 ip_style = lv_style_guid ip_value = 'style樣式字體' ). CATCH ZCX_EXCEL. ENDTRY. 
"fill設置,類:zcl_excel_style_fill "建立一個新style lo_style = lo_excel->add_new_style( ). "填充類型 lo_style->fill->filltype = zcl_excel_style_fill=>c_fill_solid. "前景色 lo_style->fill->fgcolor-rgb = 'FF66FFCC'. "背景色 lo_style->fill->bgcolor-rgb = 'FF000022'. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 2 ip_style = lv_style_guid ip_value = 'style樣式fill' ). CATCH ZCX_EXCEL. ENDTRY.
"設置邊框,類:zcl_excel_style_borders
"border類 DATA:lo_border TYPE REF TO zcl_excel_style_border.
"建立一個新style lo_style = lo_excel->add_new_style( ). "建立border對象 CREATE OBJECT lo_border. "設置border樣式 lo_border->border_style = zcl_excel_style_border=>c_border_dashdot. lo_border->border_color-rgb = 'FFCC0066'. "設置全部border lo_style->borders->allborders = lo_border. "經過down,left,right,top設置單獨邊框 * lo_style->borders->down = lo_border. "設置單元格中交叉線 * lo_style->borders->diagonal = lo_border. * lo_style->borders->diagonal_mode = zcl_excel_style_borders=>c_diagonal_both. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 3 ip_style = lv_style_guid ip_value = 'style樣式border' ). CATCH ZCX_EXCEL. ENDTRY. "設置alignment,類:zcl_excel_style_alignment "建立一個新style lo_style = lo_excel->add_new_style( ). "設置水平方向,c_horizontal_xxxx:居中center,靠左left,靠右right,默認general lo_style->alignment->horizontal = zcl_excel_style_alignment=>c_horizontal_left. "設置豎直方向,c_vertical_xxxx:頂部top,底部bottom,中間center,排齊justify lo_style->alignment->vertical = zcl_excel_style_alignment=>c_vertical_top. "設置文本旋轉 lo_style->alignment->textrotation = 0. "設置縮小適應 lo_style->alignment->shrinktofit = abap_true. "設置縮進字符 lo_style->alignment->indent = 2. "換行 lo_style->alignment->wraptext = abap_true. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 4 ip_style = lv_style_guid ip_value = 'style樣式alignment' ) CATCH ZCX_EXCEL. ENDTRY.
"設置number_format格式,類:zcl_excel_style_number_format "建立一個新style lo_style = lo_excel->add_new_style( ). "日期格式 lo_style->number_format->format_code = zcl_excel_style_number_format=>c_format_date_ddmmyyyy. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 5 ip_style = lv_style_guid ip_value = sy-datum ). CATCH ZCX_EXCEL. ENDTRY. "建立一個新style lo_style = lo_excel->add_new_style( ). "文本格式 lo_style->number_format->format_code = zcl_excel_style_number_format=>c_format_text. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 6 ip_style = lv_style_guid ip_value = '01234' ). CATCH ZCX_EXCEL. ENDTRY. "設置保護protection,類:zcl_excel_style_protection "建立一個新style lo_style = lo_excel->add_new_style( ). "是否隱藏?? * lo_style->protection->hidden = zcl_excel_style_protection=>c_protection_hidden. "是否鎖住?? lo_style->protection->locked = '1'. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). TRY . "設置style lo_worksheet->set_cell( ip_column = 'C' ip_row = 7 ip_style = lv_style_guid ip_value = 'style樣式protection' ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

    設置單元格對應數據ABAP類型。

示例:

"設置單元格對應ABAP數據類型
FORM set_typedesc. TRY. "建立一個新的worksheet
      lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'sheet6' ). "設置type desc
      "cl_abap_typedescr=>typekind_string,
      "cl_abap_typedescr=>typekind_packed,
      "cl_abap_typedescr=>typekind_num,
      "cl_abap_typedescr=>typekind_date,
      lo_worksheet->set_cell( ip_column = 'C' ip_row = 1 ip_abap_type = cl_abap_typedescr=>typekind_string ip_value = 'string' ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

6.顯示內表到excel

    經過worksheet的bind_table方法將一個內表顯示到sheet中

示例:

"展現一個內表到excel
FORM show_itab. "內表定義
  DATA:t_sflight LIKE TABLE OF sflight. SELECT * INTO CORRESPONDING FIELDS OF TABLE t_sflight FROM sflight WHERE CARRID = 'AA'. "excel內表設置
  DATA: ls_table_settings TYPE zexcel_s_table_settings. "excel內表顯示欄位,樣式
  DATA: lt_field_catalog TYPE zexcel_t_fieldcatalog. "指針
  FIELD-SYMBOLS:<fs_catalog> TYPE zexcel_s_fieldcatalog. TRY . "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). "設置sheet標題 lo_worksheet->set_title( ip_title = 'itab' ). "設置內表樣式 ls_table_settings-table_name = 'itab'. ls_table_settings-table_style = zcl_excel_table=>builtinstyle_medium2. "設置內表起始列和行 ls_table_settings-top_left_column = 'B'. ls_table_settings-top_left_row = 2. * ls_table_settings-bottom_right_column = 'F'.
* ls_table_settings-bottom_right_row = 8.
      "行條紋 ls_table_settings-show_row_stripes  = abap_false. "列條紋 ls_table_settings-show_column_stripes  = abap_true. "無過濾器 ls_table_settings-nofilters = abap_true. "iconset
      DATA: ls_iconset TYPE zexcel_conditional_iconset. "條件格式
      DATA: lo_style_conditional   TYPE REF TO zcl_excel_style_conditional. "設置顯示圖標類型c_iconset_XXXX,不一樣圖標 ls_iconset-iconset = zcl_excel_style_conditional=>c_iconset_5rating. "iconset_數字,數字是幾設置幾個cfvo,顯示不一樣圖標區間 ls_iconset-cfvo1_type  = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo1_value = '0'. ls_iconset-cfvo2_type  = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo2_value = '20'. ls_iconset-cfvo3_type  = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo3_value = '30'. ls_iconset-cfvo4_type  = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo4_value = '40'. ls_iconset-cfvo5_type  = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo5_value = '50'. "條件設置 lo_style_conditional = lo_worksheet->add_new_conditional_style( ). "規則 lo_style_conditional->rule  = zcl_excel_style_conditional=>c_rule_iconset. "優先級 lo_style_conditional->priority = 1. lo_style_conditional->mode_iconset  = ls_iconset. "是否同時顯示單元格值 ls_iconset-showvalue = zcl_excel_style_conditional=>c_showvalue_true. "獲取內表字段 lt_field_catalog = zcl_excel_common=>get_fieldcatalog( ip_table = t_sflight ). "設置field catalog
      "zexcel_s_fieldcatalog字段解析
      "position:字段顯示位置
      "dynpfld:字段是否顯示,true顯示,false隱藏
      "abap_type:字段對應ABAP類型
      "cond_style:能夠添加zcl_excel_style_conditional,圖標顯示
      "totals_function:該列添加統計行,統計類型,
      "TOTALS_FUNCTION_AVERAGE:平均值
      "TOTALS_FUNCTION_COUNT:統計記錄數
      "TOTALS_FUNCTION_MAX:最大值
      "TOTALS_FUNCTION_MIN:最小值
      "TOTALS_FUNCTION_SUM:合計
      LOOP AT lt_field_catalog ASSIGNING <fs_catalog>. CASE <fs_catalog>-fieldname. WHEN 'CARRID'. <fs_catalog>-position = 1. <fs_catalog>-dynpfld = abap_true. WHEN 'CONNID'. <fs_catalog>-position = 2. <fs_catalog>-dynpfld = abap_true. <fs_catalog>-abap_type = cl_abap_typedescr=>typekind_int. WHEN 'FLDATE'. <fs_catalog>-position = 3. <fs_catalog>-dynpfld = abap_true. <fs_catalog>-totals_function = zcl_excel_table=>totals_function_count. WHEN 'PRICE'. <fs_catalog>-position = 4. <fs_catalog>-dynpfld = abap_true. <fs_catalog>-cond_style = lo_style_conditional. <fs_catalog>-totals_function = zcl_excel_table=>totals_function_sum. WHEN OTHERS. <fs_catalog>-dynpfld = abap_false. ENDCASE. ENDLOOP. "設置內表和worksheet綁定 lo_worksheet->bind_table( ip_table = t_sflight is_table_settings = ls_table_settings it_field_catalog = lt_field_catalog ). "設置固定行 lo_worksheet->freeze_panes( ip_num_rows = 3 ). "設置活動的lo_worksheet,從1開始,
      "使用set_active_sheet_index_by_name方法,經過sheet名設置active sheet lo_excel->set_active_sheet_index( 1 ). CATCH ZCX_EXCEL INTO lf_cxexcel. "獲取錯誤信息
      DATA:result TYPE String. CALL METHOD lf_cxexcel->IF_MESSAGE~GET_TEXT RECEIVING result = result. MESSAGE result TYPE 'E'. ENDTRY. ENDFORM.

7.condition圖標顯示

  經過zcl_excel_style_conditional類,顯示不一樣圖標。

示例:

"圖標顯示
FORM conditional_format. "條件格式
  DATA: lo_style_conditional   TYPE REF TO zcl_excel_style_conditional. TRY. "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'sheet4' ). 
"iconset DATA: ls_iconset TYPE zexcel_conditional_iconset. "設置顯示圖標類型c_iconset_XXXX,不一樣圖標 ls_iconset-iconset = zcl_excel_style_conditional=>c_iconset_3trafficlights2. "iconset_數字,數字是幾設置幾個cfvo,顯示不一樣圖標區間 ls_iconset-cfvo1_type = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo1_value = '0'. ls_iconset-cfvo2_type = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo2_value = '33'. ls_iconset-cfvo3_type = zcl_excel_style_conditional=>c_cfvo_type_percent. ls_iconset-cfvo3_value = '66'. "是否同時顯示單元格值 ls_iconset-showvalue = zcl_excel_style_conditional=>c_showvalue_true. "條件設置 lo_style_conditional = lo_worksheet->add_new_conditional_style( ). "規則 lo_style_conditional->rule = zcl_excel_style_conditional=>c_rule_iconset. "優先級 lo_style_conditional->priority = 1. lo_style_conditional->mode_iconset = ls_iconset. "設置範圍range lo_style_conditional->set_range( ip_start_column = 'C' ip_start_row = 4 ip_stop_column = 'C' ip_stop_row = 8 ). lo_worksheet->set_cell( ip_row = 4 ip_column = 'C' ip_value = 100 ). lo_worksheet->set_cell( ip_row = 5 ip_column = 'C' ip_value = 1000 ). lo_worksheet->set_cell( ip_row = 6 ip_column = 'C' ip_value = 150 ). lo_worksheet->set_cell( ip_row = 7 ip_column = 'C' ip_value = 10 ). lo_worksheet->set_cell( ip_row = 8 ip_column = 'C' ip_value = 500 ).
"databar DATA:ls_databar TYPE zexcel_conditional_databar. ls_databar-cfvo1_type = zcl_excel_style_conditional=>c_cfvo_type_min. ls_databar-cfvo1_value = '0'. ls_databar-cfvo2_type = zcl_excel_style_conditional=>c_cfvo_type_max. ls_databar-cfvo2_value = '0'. ls_databar-colorrgb = 'FF638EC6'. "條件設置 lo_style_conditional = lo_worksheet->add_new_conditional_style( ). "規則 lo_style_conditional->rule = zcl_excel_style_conditional=>c_rule_databar. "優先級 lo_style_conditional->priority = 1. lo_style_conditional->mode_databar = ls_databar. "設置範圍range lo_style_conditional->set_range( ip_start_column = 'D' ip_start_row = 4 ip_stop_column = 'D' ip_stop_row = 8 ). lo_worksheet->set_cell( ip_row = 4 ip_column = 'D' ip_value = 100 ). lo_worksheet->set_cell( ip_row = 5 ip_column = 'D' ip_value = 200 ). lo_worksheet->set_cell( ip_row = 6 ip_column = 'D' ip_value = 300 ). lo_worksheet->set_cell( ip_row = 7 ip_column = 'D' ip_value = 400 ). lo_worksheet->set_cell( ip_row = 8 ip_column = 'D' ip_value = 500 ). "colorscale DATA:ls_colorscale TYPE zexcel_conditional_colorscale. ls_colorscale-cfvo1_type = zcl_excel_style_conditional=>c_cfvo_type_min. ls_colorscale-cfvo1_value = 0. ls_colorscale-cfvo2_type = zcl_excel_style_conditional=>c_cfvo_type_percentile. ls_colorscale-cfvo2_value = '50'. ls_colorscale-colorrgb1 = 'FFF8696B'. ls_colorscale-colorrgb2 = 'FF63BE7B'. "條件設置 lo_style_conditional = lo_worksheet->add_new_conditional_style( ). "規則 lo_style_conditional->rule = zcl_excel_style_conditional=>c_rule_colorscale. "優先級 lo_style_conditional->priority = 1. lo_style_conditional->mode_colorscale = ls_colorscale. "設置範圍range lo_style_conditional->set_range( ip_start_column = 'E' ip_start_row = 4 ip_stop_column = 'E' ip_stop_row = 8 ). lo_worksheet->set_cell( ip_row = 4 ip_column = 'E' ip_value = 10 ). lo_worksheet->set_cell( ip_row = 5 ip_column = 'E' ip_value = 20 ). lo_worksheet->set_cell( ip_row = 6 ip_column = 'E' ip_value = 30 ). lo_worksheet->set_cell( ip_row = 7 ip_column = 'E' ip_value = 40 ). lo_worksheet->set_cell( ip_row = 8 ip_column = 'E' ip_value = 50 ). "cellis "根據條件,設置單元格樣式 DATA:lo_style TYPE REF TO zcl_excel_style. DATA:lv_style_guid TYPE zexcel_cell_style. DATA:ls_cellis TYPE zexcel_conditional_cellis. lo_style = lo_excel->add_new_style( ). lo_style->fill->filltype = zcl_excel_style_fill=>c_fill_solid. lo_style->fill->bgcolor-rgb = 'FF00CCFF'. lv_style_guid = lo_style->get_guid( ).
    "條件設置 lo_style_conditional
= lo_worksheet->add_new_conditional_style( ). lo_style_conditional->rule = zcl_excel_style_conditional=>c_rule_cellis. ls_cellis-formula = '"hello"'. ls_cellis-operator = zcl_excel_style_conditional=>c_operator_equal. ls_cellis-cell_style = lv_style_guid. lo_style_conditional->mode_cellis = ls_cellis. lo_style_conditional->priority = 1. lo_style_conditional->set_range( ip_start_column = 'G' ip_start_row = 2 ip_stop_column = 'G' ip_stop_row = 2 ). lo_worksheet->set_cell( ip_row = 1 ip_column = 'G' ip_value = 'G2輸入hello顯示樣式:' ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

8.設置單元格公式

    設置單元格公式:經過set_cell方法的ip_formula參數設置;

示例:

"formula,公式
FORM set_formula. TRY . lo_excel->set_active_sheet_index_by_name( 'sheet4' ). "獲取worksheet,經過sheet名 lo_worksheet = lo_excel->get_worksheet_by_name( 'sheet4' ). lo_worksheet->set_cell( ip_row = 9 ip_column = 'C' ip_value = '求和:' ). lo_worksheet->set_cell( ip_row = 10 ip_column = 'C' ip_formula = 'SUM(C4:C5)' ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

批量設置多個單元格公式:

    使用zcl_excel_commom的shift_formula方法批量獲取動態公式。

示例:

"經過循環獲取動態公式
      DO 2 TIMES. "獲取轉換的公式 lv_formula = zcl_excel_common=>shift_formula( iv_reference_formula = 'SUM(C4:C5)' iv_shift_cols = 0 iv_shift_rows = sy-index ). lv_row = 10 + sy-index.  " Absolute row = sy-index rows below reference cell lo_worksheet->set_cell( ip_row = lv_row ip_column = 'C' ip_formula = lv_formula ). ENDDO.

9.定義range區域

    定義一個range區域,至關於Excel選擇區域,進行批量單元格設置。

示例:

"range獲取
FORM set_range. "range
  DATA:lo_range TYPE REF TO zcl_excel_range. "sheet名
  DATA: lv_title TYPE zexcel_sheet_title VALUE 'sheet4'. lo_range = lo_excel->add_new_range( ). lo_range->name = 'range'. "A4A5區域 lo_range->set_value( ip_sheet_name    = lv_title ip_start_column = 'A' ip_start_row = 4 ip_stop_column = 'A' ip_stop_row = 5 ). ENDFORM.

10.通用zcl_excel_common類

    通用方法類zcl_excel_common,提供一些通用方法使用。

示例:

"ZCL_EXCEL_COMMON類
FORM common_excel. "ZCL_EXCEL_COMMON類提供一些通用方法
  "CONVERT_COLUMN2ALPHA:將列號轉換對應字母
  "DATE_TO_EXCEL_STRING:將日期轉換爲string類型對應數字字符串
  "ENCRYPT_PASSWORD:加密密碼字符串
  "EXCEL_STRING_TO_DATE:將日期數字字符串轉換爲日期
  "SHIFT_FORMULA:移動應用單元格公式
ENDFORM.

11.設置單元格驗證

  1.經過zcl_excel_worksheet類對象lo_worksheet的add_new_worksheet方法獲取zcl_excel_data_validation類;  

  2.經過zcl_excel_data_validation類設置驗證規則;

示例:

"單元格驗證
FORM set_validation. "range
  DATA:lo_range TYPE REF TO zcl_excel_range. "data validation驗證規則
  DATA:lo_data_validation TYPE REF TO zcl_excel_data_validation. TRY. "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'data validation' ). "設置下拉選擇值 lo_worksheet->set_cell( ip_row = 1 ip_column = 'A' ip_value = '蘋果' ). lo_worksheet->set_cell( ip_row = 2 ip_column = 'A' ip_value = '梨子' ). lo_worksheet->set_cell( ip_row = 3 ip_column = 'A' ip_value = '香蕉' ). "建立新range lo_range = lo_worksheet->add_new_range( ). lo_range->name = 'fruit'. "ip_sheet_name設置range數據所在sheet,設置range範圍 lo_range->set_value( ip_sheet_name = 'data validation' ip_start_column = 'A' ip_start_row = '1' ip_stop_column = 'A' ip_stop_row = '3' ). "建立驗證規則 lo_data_validation = lo_worksheet->add_new_data_validation( ). "驗證類型:下拉選擇框 lo_data_validation->type = zcl_excel_data_validation=>c_type_list. lo_data_validation->formula1 = 'fruit'. "規則所在單元格 lo_data_validation->cell_row = 4. lo_data_validation->cell_column = 'A'. lo_worksheet->set_cell( ip_row = 4 ip_column = 'A' ip_value = 'Select a value' ). "建立驗證規則 lo_data_validation = lo_worksheet->add_new_data_validation( ). "驗證類型:文本長度 lo_data_validation->type = zcl_excel_data_validation=>c_type_textlength. "操做符:小於等於 lo_data_validation->operator = zcl_excel_data_validation=>c_operator_lessthanorequal. "公式 lo_data_validation->formula1 = 10. "規則所在單元格 lo_data_validation->cell_row = 2. lo_data_validation->cell_column = 'B'. "規則區域
* lo_data_validation->cell_row_to = 3.
* lo_data_validation->cell_column = 'D'. lo_worksheet->set_cell( ip_row = 1 ip_column = 'B' ip_value = 'text長度小於10:' ). "建立驗證規則 lo_data_validation = lo_worksheet->add_new_data_validation( ). "驗證類型: lo_data_validation->type = zcl_excel_data_validation=>c_type_whole. "操做符:between lo_data_validation->operator    = zcl_excel_data_validation=>c_operator_between. "範圍 lo_data_validation->formula1    = 1. lo_data_validation->formula2    = 10. "選擇單元格,彈出提示信息 lo_data_validation->prompttitle = 'Range'. lo_data_validation->prompt      = 'Enter a value between 1 and 10'. "錯誤彈窗,顯示錯誤信息 lo_data_validation->errortitle  = 'Error'. lo_data_validation->error       = 'You have entered a wrong value. Please use only numbers between 1 and 10.'. "規則生效單元格 lo_data_validation->cell_row    = 2. lo_data_validation->cell_column = 'C'. lo_worksheet->set_cell( ip_row = 1 ip_column = 'C' ip_value = '數字在1-10:' ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

12.設置單元格合併

    使用zcl_excel_worksheet類對象set_merge方法,合併單元格操做。

示例:

"單元格合併
FORM set_merge. TRY . lo_worksheet->set_cell( ip_row = 1 ip_column = 'F' ip_value = '單元格合併' ). "設置單元格合併 lo_worksheet->set_merge( ip_column_start = 'F' ip_column_end = 'H' ip_row = 1 ip_row_to = 2 ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

13.行列選擇

    使用zcl_excel_worksheet_columndime類對象保存列信息,zcl_excel_worksheet_rowdimensi 類對象保存行信息。

    經過zcl_excel_worksheet類對象的get_column_dimension方法獲取列對象;

    經過zcl_excel_worksheet類對象的get_row_dimension方法行對象;

    調用行列對象方法設置行列參數。

示例:

"行列選擇
FORM select_area. "列選擇對象
  DATA:column_dimension TYPE REF TO zcl_excel_worksheet_columndime. "行選擇對象
  DATA:row_dimension TYPE REF TO zcl_excel_worksheet_rowdimensi. TRY . "列選擇 column_dimension = lo_worksheet->get_column_dimension( ip_column = 'A' ). "設置列屬性
      "設置列寬 column_dimension->set_width( ip_width = 6 ). "設置自動列寬 column_dimension->set_auto_size( ip_auto_size = abap_true ). "設置是否可見 column_dimension->set_visible( ip_visible = abap_true ). "設置列樣式
      "style類
      DATA:lo_style TYPE REF TO zcl_excel_style. "style的guid
      DATA:lv_style_guid TYPE zexcel_cell_style. "建立一個新style lo_style = lo_excel->add_new_style( ). "文本格式 lo_style->number_format->format_code =  zcl_excel_style_number_format=>c_format_text. "獲取style的編碼uuid lv_style_guid = lo_style->get_guid( ). column_dimension->set_column_style_by_guid( ip_style_guid = lv_style_guid ). "設置outline level,出現列收縮展開欄 column_dimension->set_outline_level( ip_outline_level = 0 ). "行選擇 row_dimension = lo_worksheet->get_row_dimension( ip_row = 1 ). "設置行高 row_dimension->set_row_height( ip_row_height = 32 ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

14.獲取worksheet的行列數

    經過zcl_excel_worksheet類對象的ge_hightest_row方法獲取當前worksheet中行數;

    經過zcl_excel_worksheet類對象的ge_hightest_column方法獲取當前worksheet中列數;

示例:

"獲取excel的列數和行數
FORM get_rc. "行數
  DATA:lv_rows TYPE I. "列數
  DATA:lv_columns TYPE I. "列數對應字母
  DATA:col_alpha TYPE zexcel_cell_column_alpha. TRY . "獲取行列數 lv_rows = lo_worksheet->get_highest_row( ). lv_columns = lo_worksheet->get_highest_column( ). "將列數轉換成對應列字母 col_alpha = zcl_excel_common=>convert_column2alpha( ip_column = 2 ). CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

15.excel顯示圖片

    經過zcl_excel_drawing類對象實現excel中繪製圖片。

示例:

"顯示圖片
FORM set_drawing. "繪製圖片對象
  DATA:lo_drawing TYPE REF TO zcl_excel_drawing. TRY . "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'drawing' ). "顯示本地路徑圖片
      DATA:t_solix TYPE solix_tab. DATA:lv_len TYPE I. DATA:lv_xstring TYPE xstring. CALL METHOD cl_gui_frontend_services=>gui_upload EXPORTING filename = 'D:\圖片\back2.jpg' filetype = 'BIN'
        IMPORTING filelength = lv_len CHANGING data_tab = t_solix. IF sy-subrc <> 0 . MESSAGE '讀取失敗!' TYPE 'E'. ENDIF. CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
        EXPORTING input_length = lv_len IMPORTING buffer = lv_xstring TABLES binary_tab = t_solix EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. "建立zcl_excel_drawing對象 lo_drawing = lo_excel->add_new_drawing( ). lo_drawing->set_position( ip_from_row = 3 ip_from_col = 'B' ). lo_drawing->set_media( ip_media = lv_xstring ip_media_type = zcl_excel_drawing=>c_media_type_bmp ip_width = 83 ip_height = 160 ). "worksheet添加圖片 lo_worksheet->add_drawing( lo_drawing ). "加載tcode:SMW0圖片
      DATA:ls_key TYPE wwwdatatab. ls_key-relid = 'MI'. "SMW0,經過object id能夠查找到圖片 ls_key-objid = 'SAPLOGO.GIF'. lo_drawing = lo_excel->add_new_drawing( ). lo_drawing->set_position( ip_from_row = 16 ip_from_col = 'B' ). lo_drawing->set_media_www( ip_key = ls_key ip_width = 166 ip_height = 75 ). "worksheet添加圖片 lo_worksheet->add_drawing( lo_drawing ). "Mime repository (by default Question mark in standard Web Dynpro WDT_QUIZ
      "使用lo_worksheet->set_media_mime方法
    CATCH ZCX_EXCEL. ENDTRY. ENDFORM.

16.設置過濾器filter

    經過zcl_excel_autofilter類對象實現過濾器filter。

示例:

"設置filter
FORM set_filter. DATA:t_sflight TYPE TABLE OF sflight. SELECT * FROM sflight INTO TABLE t_sflight UP TO 10 ROWS. "將alv或者內表綁定到excel,converter類
  DATA:lo_converter TYPE REF TO zcl_excel_converter. "自動過濾類
  DATA:lo_autofilter TYPE REF TO zcl_excel_autofilter. "過濾器範圍
  DATA:ls_area TYPE zexcel_s_autofilter_area. "列單元格值
  DATA:lv_cell_value TYPE zexcel_cell_value. TRY . "建立一個新的worksheet lo_worksheet = lo_excel->add_new_worksheet( ). lo_worksheet->set_title( ip_title = 'filter' ). CREATE OBJECT lo_converter. lo_converter->convert( EXPORTING it_table = t_sflight i_row_int = 1 i_column_int = 1 io_worksheet = lo_worksheet CHANGING co_excel = lo_excel ). "當前worksheet,建立新的filter對象 lo_autofilter = lo_excel->add_new_autofilter( io_sheet = lo_worksheet ) . ls_area-row_start = 1. ls_area-col_start = 1. ls_area-row_end = lo_worksheet->get_highest_row( ). ls_area-col_end = lo_worksheet->get_highest_column( ). lo_autofilter->set_filter_area( ls_area ). "設置第三列filter value
* lo_worksheet->get_cell( EXPORTING
* ip_column = 'C'
* ip_row = 2
* IMPORTING
* ep_value = lv_cell_value ).
    "設置列篩選值
* lo_autofilter->set_value( i_column = 3 i_value = lv_cell_value ).
    CATCH ZCX_EXCEL. ENDTRY. ENDFORM.
相關文章
相關標籤/搜索