*&---------------------------------------------------------------------*
*& Report Y0825_TXT_EXCEL_YBP
*&
*&---------------------------------------------------------------------*
*& 数据库内容导出到excel,
*&---------------------------------------------------------------------*
REPORT Y0825_TXT_EXCEL_YBP MESSAGE-ID YQYZ_TXT_SQL_EXCEL.
************************** 定义数据***********************************************
TABLES SSCRFIELDS.
************************对应的数据库表结构************************
DATA: BEGIN OF ITAB,
MANDT TYPE C LENGTH 3,
MATNR TYPE C LENGTH 18,
ERSDA TYPE D,
ERNAM TYPE C LENGTH 12,
LAEDA TYPE D,
AENAM TYPE C LENGTH 12,
VPSTA TYPE C LENGTH 15,
PSTAT TYPE C LENGTH 15,
LVORM TYPE C ,
MTART TYPE C LENGTH 4,
MBRSH TYPE C,
END OF ITAB.
DATA: WA LIKE ITAB.
DATA: ITAB_DATA LIKE TABLE OF ITAB.
************************导出的结构************************
DATA: APPLICATION TYPE OLE2_OBJECT, " excel object
WORKBOOK TYPE OLE2_OBJECT, " 工作簿
SHEET TYPE OLE2_OBJECT, " 工作表
COLUMNS TYPE OLE2_OBJECT, " 表格的行
ROWS TYPE OLE2_OBJECT, " 表格的列
RANGE TYPE OLE2_OBJECT,
RANGE1 TYPE OLE2_OBJECT,
FONT TYPE OLE2_OBJECT,
CELL TYPE OLE2_OBJECT,
CELL1 TYPE OLE2_OBJECT,
SHEET1 TYPE OLE2_OBJECT,
SHEET2 TYPE OLE2_OBJECT,
BORDERS TYPE OLE2_OBJECT.
DATA: INDEX TYPE I VALUE 0.
DATA TAB_DATA LIKE TABLE OF YTABLE_TXT_EXCEL. "数据库数据的内表
DATA REC_DATA LIKE YTABLE_TXT_EXCEL.
*导出数据块
SELECTION-SCREEN BEGIN OF BLOCK STATUS2 WITH FRAME TITLE TEXT-F02.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(20) TEXT-002.
SELECTION-SCREEN: PUSHBUTTON 75(15) BUT2 USER-COMMAND CLI2. " 从系统中导出excel模板 到本地
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK STATUS2.
****初始化屏幕****
INITIALIZATION.
BUT2 = ‘开始导出‘. " 数据库数据导出到本地的excel
AT SELECTION-SCREEN.
IF SSCRFIELDS-UCOMM EQ ‘CLI2‘. "导出数据库内容到excel
PERFORM DEMO_FORMS.
ENDIF.
START-OF-SELECTION.
PERFORM DEMO_FORMS. "数据库内容导出到excel
*************************************子程序**************************************
*----------------------------------------------------------------------*
* 数据库内容导出到excel
*----------------------------------------------------------------------*
FORM DEMO_FORMS.
SELECT * FROM YTABLE_TXT_EXCEL INTO TABLE ITAB_DATA. "取得数据库表内容
PERFORM CREATE_EXCEL. "创建excel
LOOP AT ITAB_DATA INTO WA.
PERFORM INSERT_ROW USING 1. " 插入一行
PERFORM FILL_CELL USING 1 1 1 WA-MANDT. "填充单元格
PERFORM FILL_CELL USING 1 2 1 WA-MATNR.
PERFORM FILL_CELL USING 1 3 0 WA-ERSDA.
PERFORM FILL_CELL USING 1 4 0 WA-ERNAM.
PERFORM FILL_CELL USING 1 5 0 WA-LAEDA.
PERFORM FILL_CELL USING 1 6 0 WA-AENAM.
PERFORM FILL_CELL USING 1 7 0 WA-VPSTA.
PERFORM FILL_CELL USING 1 8 0 WA-PSTAT.
PERFORM FILL_CELL USING 1 9 0 WA-LVORM.
PERFORM FILL_CELL USING 1 10 0 WA-MTART.
PERFORM FILL_CELL USING 1 11 0 WA-MBRSH.
INDEX = INDEX + 1.
ENDLOOP.
PERFORM FREE_OBJECT.
ENDFORM. "DEMO_FORMS
*****************************创建excel *******************************************
FORM CREATE_EXCEL.
CREATE OBJECT APPLICATION ‘excel.APPLICATION‘.
IF SY-SUBRC <> 0.
MESSAGE ‘EXCEL ERROR‘ TYPE ‘S‘ DISPLAY LIKE ‘E‘.
STOP.
ENDIF.
CALL METHOD OF
APPLICATION
‘WORKBOOKS‘ = WORKBOOK.
SET PROPERTY OF APPLICATION ‘VISIBLE‘ = 1.
SET PROPERTY OF APPLICATION ‘SHEETSINNEWWORKBOOK‘ = 1.
CALL METHOD OF
WORKBOOK
‘ADD‘ = SHEET.
CALL METHOD OF
SHEET
‘ACTIVE‘.
ENDFORM. "CREATE_EXCEL
************************************************************************
* 根据行号插入一行 *
* -->I_ROW 行号 *
************************************************************************
FORM INSERT_ROW USING I_ROW.
CALL METHOD OF
APPLICATION
‘ROWS‘ = ROWS
EXPORTING
#1 = I_ROW.
CALL METHOD OF
ROWS
‘INSERT‘.
ENDFORM. "INSERT_ROW
************************************************************************
* 填充单元格 *
* --> I_ROW 行号 *
* --> I_COL 列号 *
* --> BOLD 是否加粗 -->P_VALUE 赋值 *
************************************************************************
FORM FILL_CELL USING I_ROW I_COL BOLD P_VALUE.
CALL METHOD OF
APPLICATION
‘CELLS‘ = CELL
EXPORTING
#1 = I_ROW
#2 = I_COL.
SET PROPERTY OF CELL ‘VALUE‘ = P_VALUE.
SET PROPERTY OF CELL ‘HORIZONTALALIGNMENT‘ = 2.
GET PROPERTY OF CELL ‘FONT‘ = FONT.
SET PROPERTY OF FONT ‘BOLD‘ = BOLD.
SET PROPERTY OF FONT ‘COLORINDEX‘ = 41.
CALL METHOD OF
CELL
‘BORDERS‘ = BORDERS
EXPORTING
#1 = ‘2‘.
SET PROPERTY OF BORDERS ‘LINESTYLE‘ = ‘0‘.
SET PROPERTY OF BORDERS ‘WEIGHT‘ = 0.
ENDFORM.
************************************************************************
* 释放object *
************************************************************************
FORM FREE_OBJECT.
FREE OBJECT FONT.
FREE OBJECT RANGE.
FREE OBJECT RANGE1.
FREE OBJECT COLUMNS.
FREE OBJECT ROWS.
FREE OBJECT CELL.
FREE OBJECT CELL1.
FREE OBJECT SHEET.
FREE OBJECT SHEET1.
FREE OBJECT WORKBOOK.
FREE OBJECT APPLICATION.
ENDFORM. "FREE_OBJECT