DATA: EXCEL TYPE OLE2_OBJECT,
SHEET TYPE OLE2_OBJECT,
CELL TYPE OLE2_OBJECT,
WORKBOOK TYPE OLE2_OBJECT,
XLSNAME TYPE STRING VALUE 'C:\sap_usr02.xls',
LINE TYPE I VALUE 0. "行号
CREATE OBJECT EXCEL 'EXCEL.APPLICATION'."启动Excel
IF SY-SUBRC NE 0.
WRITE: / '启动Excel失败。'.
STOP.
ENDIF.
The CALL the METHOD . OF the EXCEL 'WorkBooks' = WorkBook .
The SET PROPERTY . OF the EXCEL 'VISIBLE' = . 1 . "Excel make visible
the SET PROPERTY . OF the EXCEL 'SHEETSINNEWWORKBOOK' = . 1 . " If the read contents excel file is directly open the The first book page
the CALL the METHOD . OF workBook 'the ADD' . "For example: the METHOD. OF the EXCEL the CALL 'WorkSheets'. 1 = # = 1. SHEET EXPORTING
LOOP the AT IT_DATA the INTO my_data .
the LINE = the LINE + . 1 . "Excel starting row number. 1
the CALL the METHOD . OF the EXCEL 'the CELLS' = the CELL EXPORTING #. 1 = the LINE # 2 = . 1 . " Specified cell,
the SET PROPERTY . OF the CELL 'of VALUE' = my_data -MANDT . "Values written in
the CALL the METHOD . OF the EXCEL 'the cELLS' = the cELL EXPORTING #. 1 = the LINE # 2 = 2 . "specified cell,
the SET PROPERTY . OF the cELL 'of vALUE' = my_data -BNAME . " written value
CALL The METHOD . OF the EXCEL 'the CELLS' = the CELL EXPORTING #. 1 = the LINE # 2 = . 3 . "Specified cell,
the SET PROPERTY . OF the CELL 'of VALUE' = my_data -ERDAT . " Values written in
the CALL the METHOD . OF the EXCEL 'the CELLS' = the CELL EXPORTING # . 1 = the LINE # 2 = . 4 . "specified cell,
the SET PROPERTY . OF the cELL 'of vALUE' = my_data -LTIME . " written value
ENDLOOP .
the GET PROPERTY OF EXCEL 'ActiveSheet' = SHEET . "Activate the workbook
GET PROPERTY OF EXCEL 'the ActiveWorkbook' = WorkBook . " Activate workspace
CALL the METHOD OF WorkBook 'SAVEAS' EXPORTING # 1 = XLSNAME # 2 = 1 . "Save the excel file
CALL the METHOD OF WorkBook 'CLOSE' . "Close the workspace
the CALL the METHOD OF EXCEL 'the qUIT' . " exit excel
* WRITE:. / XLSNAME, ' DONE' " quit successfully, the output DONE
the FREE SHEET the OBJECT . " Operation release
the FREE the OBJECT WorkBook .
The FREE the OBJECT EXCEL .
Other methods:
table of Contents
- 1. Function: GUI_DOWNLOAD
- 3. Functions: MS_EXCEL_OLE_STANDARD_DAT
- 4. BE
- 5. Function: XXL_FULL_API
- 6. ABAP2XLSX
- 7. XLSX Workbench
1. Function: GUI_DOWNLOAD
It is a basic method, a corresponding CLASS: CL_GUI_FRONTEND_SERVICES method GUI_DOWNLOAD
Note FILENAME parameter extension should XLS, so when you save can be saved as EXCEL format. Examples are as follows:
1 DATA:itab LIKE TABLE OF mara WITH HEADER LINE.
And leading 0 will output characters. 26 codepage = '8404'
27 TABLES
28 data_tab = itab
29 fieldnames = t_fieldnames.
This function is a relatively simple, but when FILETYPE DAT and ASC, the resulting file is opened with EXCEL following dialog box will pop up:
The reason: to keep the ASC and DAT formats TXT format tables in fact, inconsistent with the EXCEL format, the extension to TXT, will be very easy to see this.
2. Functions: SAP_CONVERT_TO_XLS_FORMAT
This FM function GUI_DOWNLOAD similar support xlsx format:
SELECT * FROM sflight INTO TABLE @DATA(gt_temp) UP TO 10 ROWS.
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
i_filename = 'C:\SAP\QQ1.XLSX'
TABLES
i_tab_sap_data = gt_temp
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
3. Functions: MS_EXCEL_OLE_STANDARD_DAT
But also directly open EXCEL file. Not only can this data to the FM EXCEL,
But this function has a drawback: When the Chinese want to import data into EXCEL and you will have a GUI login Chinese run the program, otherwise the Chinese will become a '#' sign.
1 DATA:BEGIN OF it_fieldnames OCCURS 0,
2
3 name TYPE char20,
4
5 END OF it_fieldnames.
6
7 it_fieldnames-name = 'Company Code'.
8
9 APPEND it_fieldnames.
10
11 it_fieldnames-name = 'Function Area'.
12
13 APPEND it_fieldnames.
14
15 it_fieldnames-name = '5'. "针对非C、N、D、T类型列的列名,函数有Bug
16
17 APPEND it_fieldnames.
18
19 CALL FUNCTION 'MS_EXCEL_OLE_STANDARD_DAT'
20 EXPORTING
21 file_name = 'C:\johnson.XLSX'
22 * CREATE_PIVOT = 0
23 * DATA_SHEET_NAME = ' '
24 * PIVOT_SHEET_NAME = ' '
25 * PASSWORD = ' '
26 * pASSWORD_OPTION = 0
27 TABLES
28 * PIVOT_FIELD_TAB =
29 data_tab = itab
30 fieldnames = it_fieldnames
31 * EXCEPTIONS
32 * FILE_NOT_EXIST = 1
33 * FILENAME_EXPECTED = 2
34 * cOMMUNICATION_ERROR = 3
35 * oLE_OBJECT_METHOD_ERROR = 4
36 * OLE_OBJECT_PROPERTY_ERROR = 5
37 * INVALID_PIVOT_FIELDS = 6
38 * DOWNLOAD_PROBLEM = 7
39 * OTHERS = 8
Description: This FM application problems should be noted:
1, Fieldname data type will be forced into a corresponding column of the type, if the conversion fails Dump
2, if two or more times in a row or more calls, the column name will go to the data area
There seems to be a function of its predecessor, the name is EXCEL_OLE_STANDARD_DAT, to achieve a similar method. But this function on some tables will be reported "data can not converted in unicode program" error, so it is recommended using the latest functions.
4. BE
This embodiment is advantageous in that the above comparison can customize several EXCEL form, but more cumbersome.
Implementation See this article: http://www.cnblogs.com/hhelibeb/p/5787396.html
5. Function: XXL_FULL_API
The same can be achieved EXCEL format, and OLE speed advantage compared. There are several reference examples SE38: XXLTTEST, XXLSTEST, XXLFTEST
6. ABAP2XLSX
This package is a class, a group developed a dedicated fan for introducing data into EXCEL ABAP classes, including alignment, set combined, the font, coloring and the like, as is object-oriented, as compared to simple OLE mode, programming will be more effort.
It may be used when you need to customize EXCEL format.
Specifically refer to this article
7. XLSX Workbench
A visual form generation tool, compared to the OLE complex, reports can be generated with a small amount of code + EXCEL Some drag configuration and better performance. XLSX Workbench according to the document describes, it has nine characteristics:
- No need ABAP programming skills
- Visual design approach
- high performance
- Support background processing
- Powerful features support form format
- Support formula
- Support picture
- Chart Support
- Support tree
The official document: https://sites.google.com/site/sapxlwb/home
XLSX Workbench is the preferred solution in my eyes, because it's visual editor is really convenient. With this tool, the developer simply designed into the context data structure, and binds to a specific cell in the visualization tool in the context, you can generate the EXCEL file:
DATA: l_data TYPE zcontext.
CALL FUNCTION 'ZXLWB_CALLFORM'
EXPORTING
iv_formname = 'ZXLWBFORM'
iv_context_ref = l_data
EXCEPTIONS
OTHERS = 2.
IF and subrc NE 0.
MESSAGE ID and-msgid TYPE and Sgt NUMBER msgno
WITH side msgv1 side msgv2 side msgv3 side msgv4.
ENDIF .