专注专心
分类: IT职场
2011-12-22 07:17:50
tables KNA1. INCLUDE OLE2INCL. DATA: APPLICATION TYPE OLE2_OBJECT, WORKBOOK TYPE OLE2_OBJECT, SHEET TYPE OLE2_OBJECT, CELLS TYPE OLE2_OBJECT. data vfile(128) value 'c:\test123.xls'. data : begin of t_kna1 occurs 0 , kunnr like kna1-kunnr, name1 like kna1-name1, name2 like kna1-name2, end of t_kna1. CREATE OBJECT APPLICATION 'Excel.application'. SET PROPERTY OF APPLICATION 'Visible' = 0. * 1 display * 宣告一EXCEL工作底槁档(WORKBOOK) CALL METHOD OF APPLICATION 'Workbooks' = WORKBOOK. * 增加一新的工作表(SHEET),编号是 1 号 CALL METHOD OF WORKBOOK 'Add'. CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 1. * 设定此工作表开启使用 CALL METHOD OF SHEET 'Activate'. PERFORM FILL_SHEET. SET PROPERTY OF APPLICATION 'DisplayAlerts' = 'false'. CALL METHOD OF SHEET 'SAVEAS' EXPORTING #1 = vfile #2 = 1. SET PROPERTY OF APPLICATION 'Visible' = 1. *CALL METHOD OF APPLICATION 'QUIT'. *CALL METHOD OF APPLICATION 'EXIT'. ****关闭WORKSHEET**** CALL METHOD OF SHEET 'CLOSE'. ****关闭WORKBOOK CALL METHOD OF WORKBOOK 'CLOSE'. ****关闭EXCEL对象**** CALL METHOD OF APPLICATION 'QUIT'. FREE OBJECT CELLS. FREE OBJECT SHEET. FREE OBJECT WORKBOOK. FREE OBJECT APPLICATION. *SET PROPERTY OF APPLICATION 'Visible' = 1. *---------------------------------------------------------------------------------------------------------------- FORM FILL_SHEET. DATA: ROW_MAX TYPE I VALUE 256, ROWS TYPE I VALUE 1, INDEX TYPE I, pv_row TYPE I VALUE 1, pv_col TYPE I VALUE 1. FIELD-SYMBOLS: SELECT kunnr name1 name2 into t_kna1 FROM KNA1 UP TO 100 ROWS BYPASSING BUFFER. append t_kna1. endselect. * ROWS = ROWS + 1. "至第ROWS列 * INDEX = ROW_MAX * ( ROWS - 1 ) + 1. loop at t_kna1. CALL METHOD OF sheet 'Cells' = CELLS EXPORTING #1 = pv_row #2 = 1. SET PROPERTY OF CELLS 'Value' = t_KNA1-KUNNR. CALL METHOD OF sheet 'Cells' = CELLS EXPORTING #1 = pv_row #2 = 2. SET PROPERTY OF CELLS 'Value' = t_KNA1-NAME1. pv_row = pv_row + 1. endloop. * DO 10 TIMES. "如要每一列放入10个存格的数据 * ASSIGN COMPONENT SY-INDEX OF STRUCTURE KNA1 * TO * CALL METHOD OF SHEET 'Cells' = CELLS * EXPORTING #1 = INDEX. * SET PROPERTY OF CELLS 'Value' = * ADD 1 TO INDEX. * ENDDO. ENDFORM. |