1. 程式人生 > >輸入數字列號獲取EXCEL的列編號

輸入數字列號獲取EXCEL的列編號

根據輸入的數字列號,轉換為標準的excel列編號,比如:29對應AC
FUNCTION z_num_to_excel_column.
*"----------------------------------------------------------------------
*"*"本地介面:
*"  IMPORTING
*"     REFERENCE(IV_NUM) TYPE  I
*"  EXPORTING
*"     REFERENCE(IV_COLUMN) TYPE  STRING
*"----------------------------------------------------------------------

  IF iv_num LE 0.
    EXIT.
  ENDIF.

  DATA: lv_array  TYPE string,
        lv_num    TYPE i,
        lv_divide TYPE i,
        lv_mod    TYPE i,
        lv_float  TYPE p DECIMALS 2.

  lv_array = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

* 從右往左確定字母
  lv_num = iv_num.
  lv_mod = lv_num MOD 26.
  lv_mod = lv_mod - 1.
  IF lv_mod = -1.
    lv_mod = lv_mod + 26.
  ENDIF.
  iv_column = lv_array+lv_mod(1).

  lv_num = iv_num.
  WHILE lv_num > 26.
    lv_float = lv_num / 26.
    lv_num = lv_num / 26.  "Demo:27 AA
    IF lv_num > lv_float.
      lv_num = lv_num - 1.
    ENDIF.

    lv_mod = lv_num MOD 26.
    lv_mod = lv_mod - 1.
    IF lv_mod = -1.
      lv_mod = lv_mod + 26.
    ENDIF.

    CONCATENATE lv_array+lv_mod(1) iv_column INTO iv_column.
  ENDWHILE.

  CONDENSE iv_column.

ENDFUNCTION.

測試結果