ABAP顾问
分类:
2008-03-12 17:08:57
*&---------------------------------------------------------------------*
*& Report Z_BARRY_EXCEL_COL_NAME
*&
*&---------------------------------------------------------------------*
*& 根据数值获取Excel列字母的FORM
*& 如:1 -> A
*& 2 -> B
*& 27 -> AA
*&---------------------------------------------------------------------*
REPORT z_barry_excel_co_name NO STANDARD PAGE HEADING.
PARAMETERS: input TYPE i.
DATA: col_name(2) TYPE c .
START-OF-SELECTION.
DO 705 TIMES .
CLEAR col_name.
PERFORM get_col USING sy-index
col_name .
WRITE / col_name.
ENDDO.
*&---------------------------------------------------------------------*
*& Form get_col
*&---------------------------------------------------------------------*
FORM get_col USING col TYPE i
col_name TYPE c .
DATA: str TYPE string ,
xstr TYPE xstring,
l_con TYPE REF TO cl_rpe_convert .
DATA: mod TYPE i ,
div TYPE i .
IF col > 702 OR col < 1 .
MESSAGE '超过范围,FORM退出!' TYPE 'I'.
EXIT.
ENDIF.
CREATE OBJECT l_con.
mod = col MOD 26 .
div = col DIV 26 .
IF div > 0 AND mod <> 0.
xstr = div + 64 .
ELSEIF div > 1 AND mod = 0 .
xstr = div + 63 .
ENDIF.
CALL METHOD l_con->xstring_to_string
EXPORTING
input = xstr
IMPORTING
output = str.
col_name = str .
IF mod = 0 .
str = 'Z'.
ELSE.
xstr = mod + 64 .
CALL METHOD l_con->xstring_to_string
EXPORTING
input = xstr
IMPORTING
output = str.
ENDIF.
CONCATENATE col_name str INTO col_name.
ENDFORM. "get_col
START-OF-SELECTION.
DO 705 TIMES .
CLEAR col_name.
PERFORM get_col USING sy-index
col_name .
WRITE / col_name.
ENDDO.
*&---------------------------------------------------------------------*
*& Form get_col
*&---------------------------------------------------------------------*
FORM get_col USING col TYPE i
col_name TYPE c .
DATA: xc TYPE x .
DATA: mod TYPE i ,
div TYPE i .
ASSIGN xc TO
IF col > 702 OR col < 1 .
EXIT.
ENDIF.
mod = col mod 26 .
div = col DIV 26 .
IF div > 0 AND mod <> 0.
xc = div + 64 .
col_name =
ELSEIF div > 1 AND mod = 0 .
xc = div + 63 .
col_name =
ENDIF.
IF mod = 0 .
CONCATENATE col_name 'Z' INTO col_name.
ELSE.
xc = mod + 64 .
CONCATENATE col_name
ENDIF.
ENDFORM.