FORM down_load_file_excel USING i_dataout2.
* data gt_file_vat_excel type STANDARD TABLE OF .
* Starting download
DATA: l_lcobj_excel TYPE ole2_object,
l_lcobj_workbook TYPE ole2_object,
l_lcobj_sheet TYPE ole2_object,
l_cell TYPE ole2_object,
l_rc TYPE i,
l_sheetname(20) TYPE c,
l_range TYPE ole2_object.
* ls_file_excel LIKE LINE OF gt_file_vat_excel.
DATA: ls_separator TYPE c.
CLASS cl_abap_char_utilities DEFINITION LOAD.
ls_separator = cl_abap_char_utilities=>horizontal_tab.
* CLEAR ls_file_excel.
* INSERT ls_file_excel INTO p_file_excel INDEX 1.
l_sheetname = text-c43.
* Create an Excel object and start Excel.
CREATE OBJECT l_lcobj_excel ‘Excel.Application‘.
SET PROPERTY OF l_lcobj_excel ‘Visible‘ = 0.
* Create an Excel workbook Object.
CALL METHOD OF
l_lcobj_excel
‘Workbooks‘ = l_lcobj_workbook.
SET PROPERTY OF l_lcobj_excel ‘SheetsInNewWorkbook‘ = 1.
CALL METHOD OF
l_lcobj_workbook
‘ADD‘.
CALL METHOD OF
l_lcobj_excel
‘Sheets‘ = l_lcobj_sheet
EXPORTING
#1 = 1.
* change sheet name
IF l_sheetname IS NOT INITIAL.
SET PROPERTY OF l_lcobj_sheet ‘NAME‘ = l_sheetname.
ENDIF.
CALL METHOD OF
l_lcobj_excel
‘Worksheets‘ = l_lcobj_sheet
EXPORTING
#1 = l_sheetname.
CALL METHOD OF
l_lcobj_sheet
‘Activate ‘.
CALL METHOD OF
l_lcobj_excel
‘COLUMNS‘ = l_range
EXPORTING
#1 = 2. "the column number
SET PROPERTY OF l_range ‘ColumnWidth‘ = 10.
SET PROPERTY OF l_range ‘NumberFormatLocal‘ = ‘@‘ .
CALL METHOD OF
l_lcobj_excel
‘COLUMNS‘ = l_range
EXPORTING
#1 = 3. "the column number
SET PROPERTY OF l_range ‘ColumnWidth‘ = 10.
SET PROPERTY OF l_range ‘NumberFormatLocal‘ = ‘@‘ .
CALL METHOD OF
l_lcobj_sheet
‘COLUMNS‘ = l_range
EXPORTING
#1 = 6. "the column number
SET PROPERTY OF l_range ‘ColumnWidth‘ = 20.
SET PROPERTY OF l_range ‘NumberFormat‘ = ‘@‘.
CALL METHOD OF
l_lcobj_sheet
‘COLUMNS‘ = l_range
EXPORTING
#1 = 10. "the column number
SET PROPERTY OF l_range ‘ColumnWidth‘ = 20.
SET PROPERTY OF l_range ‘NumberFormat‘ = ‘@‘.
CALL METHOD OF
l_lcobj_sheet
‘COLUMNS‘ = l_range
EXPORTING
#1 = 11. "the column number
SET PROPERTY OF l_range ‘ColumnWidth‘ = 12.
SET PROPERTY OF l_range ‘NumberFormat‘ = ‘@‘.
CALL METHOD OF
l_lcobj_sheet
‘COLUMNS‘ = l_range
EXPORTING
#1 = 12. "the column number
SET PROPERTY OF l_range ‘ColumnWidth‘ = 10.
SET PROPERTY OF l_range ‘NumberFormat‘ = ‘@‘.
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = i_dataout2
CHANGING
rc = l_rc
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
CALL METHOD OF
l_lcobj_sheet
‘Cells‘ = l_cell
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF
l_cell
‘SELECT‘.
CALL METHOD OF
l_lcobj_sheet
‘PASTE‘.
GET PROPERTY OF l_lcobj_excel ‘ActiveWorkbook‘ = l_lcobj_workbook.
CALL METHOD OF
l_lcobj_workbook
‘SAVEAS‘
EXPORTING
#1 = ‘C:\Users\i060729\Desktop\aaaa‘
#2 = 1.
CALL METHOD OF
l_lcobj_workbook
‘CLOSE‘.
CALL METHOD OF
l_lcobj_excel
‘QUIT‘.
ENDFORM.
原文:http://www.cnblogs.com/xjm123/p/3746732.html