1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
*&---------------------------------------------------------------------*
*& Report ZTEST_EXCEL
*& ABAP 导出到EXCEL
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
ZTEST_EXCEL1.
INCLUDE
OLE2INCL.
DATA
: EXCEL_OBJ
TYPE
OLE2_OBJECT,
BOOK_OBJ
TYPE
OLE2_OBJECT,
SHEET_OBJ
TYPE
OLE2_OBJECT,
CELL_OBJ
TYPE
OLE2_OBJECT,
ROW_OBJ
TYPE
OLE2_OBJECT,
COLUMN_OBJ
TYPE
OLE2_OBJECT,
RANGE_OBJ
TYPE
OLE2_OBJECT,
BORDERS_OBJ
TYPE
OLE2_OBJECT,
INT_OBJ
TYPE
OLE2_OBJECT,
FONT_OBJ
TYPE
OLE2_OBJECT.
DATA
: G_TITLE(20),
G_NAME(30),
G_DATE(20).
DATA
:
BEGIN
OF
ITAB
OCCURS
0,
COL1
TYPE
I
,
COL2
TYPE
I
,
COL3
TYPE
I
,
END
OF
ITAB.
*DATA: G_LINES TYPE I,
* G_LINES2 TYPE I.
START-
OF
-
SELECTION
.
G_TITLE =
'TEST'
.
G_NAME = SY-UNAME.
G_DATE = SY-DATUM.
DO
10 TIMES.
ITAB-COL1 = ITAB-COL1 + 1.
ITAB-COL2 = ITAB-COL1
* ITAB-COL1.
ITAB-COL3 = ITAB-COL1
* ITAB-COL1 * ITAB-COL1.
APPEND
ITAB.
ENDDO
.
PERFORM
CREAT_EXCEL.
"创建EXCEL表单
PERFORM
FORMAT_EXCEL.
"设置EXCEL格式
PERFORM
PRM_OUTPUT_EXCEL.
"用来显示EXCEL表单
*&---------------------------------------------------------------------*
*& Form CREAT_EXCEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM
CREAT_EXCEL.
CREATE OBJECT EXCEL_OBJ
'excel.APPLICATION'
.
IF SY-SUBRC NE 0.
MESSAGE
'EXCEL创建错误'
TYPE
'S'
DISPLAY
LIKE
'E'
.
STOP
.
ENDIF
.
CALL METHOD
OF
EXCEL_OBJ
'WORKBOOKS'
= BOOK_OBJ .
SET
PROPERTY
OF
EXCEL_OBJ
'VISIBLE'
= 1.
SET
PROPERTY
OF
EXCEL_OBJ
'SheetsInNewWorkbook'
= 1.
CALL METHOD
OF
BOOK_OBJ
'ADD'
= SHEET_OBJ.
CALL METHOD
OF
SHEET_OBJ
'ACTIVATE'
.
FREE OBJECT SHEET_OBJ.
"OK
ENDFORM
.
"CREAT_EXCEL
*&---------------------------------------------------------------------*
*& Form FORMAT_EXCEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM
FORMAT_EXCEL.
PERFORM
ROWHEIGHT
USING
'1'
'40'
.
PERFORM
MERGERANGE
USING
'A1:F1'
.
PERFORM
MERGERANGE
USING
'D3:F3'
.
PERFORM
COLUMNWIDTH
USING
'A'
'12'
.
PERFORM
COLUMNWIDTH
USING
'B'
'11'
.
PERFORM
COLUMNWIDTH
USING
'C'
'13'
.
* PERFORM CELLTYPE USING 'A:B' '@'.
* PERFORM CELLTYPE USING 'C:K' '0.00'.
* PERFORM CELLTYPE USING '1:4' '@'.
PERFORM
FONT
USING
'A1'
1
'22'
'隶书'
.
PERFORM
CENTERCELL
USING
'A1'
.
PERFORM
CENTERCELL
USING
'D3'
.
PERFORM
WRITECELL
USING
3 4
'COL'
.
PERFORM
WRITECELL
USING
4 4
'COL1'
.
PERFORM
WRITECELL
USING
4 5
'COL2'
.
PERFORM
WRITECELL
USING
4 6
'COL3'
.
ENDFORM
.
"FORMAT_EXCEL
*&---------------------------------------------------------------------*
*& Form PRM_OUTPUT_EXCEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM
PRM_OUTPUT_EXCEL.
DATA
I_VTEXT
TYPE
VTXTK.
DATA
I_LEN
TYPE
I
.
DATA
: I_JS(4)
TYPE
N
.
DATA
I_END(11)
TYPE
N
.
I_JS = 4.
LOOP
AT
ITAB.
I_JS = I_JS + 1.
PERFORM
WRITECELL
USING
I_JS 4 ITAB-COL1.
PERFORM
WRITECELL
USING
I_JS 5 ITAB-COL2.
PERFORM
WRITECELL
USING
I_JS 6 ITAB-COL3.
ENDLOOP
.
PERFORM
WRITECELL
USING
1 1 G_TITLE.
PERFORM
WRITECELL
USING
2 1 G_NAME.
PERFORM
WRITECELL
USING
2 7 G_DATE.
FREE OBJECT BORDERS_OBJ.
FREE OBJECT CELL_OBJ.
* CONCATENATE 'A3:K' I_JS INTO I_END.
* PERFORM BORDERRANGE USING I_END.
* PERFORM COLUMNWIDTH USING 'A:B' ''.
ENDFORM
.
"PRM_OUTPUT_EXCEL
*&---------------------------------------------------------------------*
*& Form writecell
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->X text
* -->Y text
* -->V text
*----------------------------------------------------------------------*
FORM
WRITECELL
USING
ROW COL VAL.
CALL METHOD
OF
EXCEL_OBJ
'CELLS'
= CELL_OBJ
EXPORTING
#1 = ROW
#2 = COL.
SET
PROPERTY
OF
CELL_OBJ
'VALUE'
= VAL.
FREE OBJECT CELL_OBJ.
ENDFORM
.
"writecell
*&---------------------------------------------------------------------*
*& Form mergerange
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RANGE text
*----------------------------------------------------------------------*
FORM
MERGERANGE
USING
RANGE.
CALL METHOD
OF
EXCEL_OBJ
'RANGE'
= CELL_OBJ
EXPORTING
#1 = RANGE.
CALL METHOD
OF
CELL_OBJ
'MERGE'
.
FREE OBJECT CELL_OBJ.
ENDFORM
.
"mergerange
*&---------------------------------------------------------------------*
*& Form celltype
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RANGE text
*----------------------------------------------------------------------*
FORM
CELLTYPE
USING
RANGE
TYPE
.
CALL METHOD
OF
EXCEL_OBJ
'RANGE'
= CELL_OBJ
EXPORTING
#1 = RANGE.
SET
PROPERTY
OF
CELL_OBJ
'NumberFormatLocal'
=
TYPE
.
FREE OBJECT CELL_OBJ.
ENDFORM
.
"celltype
*&--------------------------------------------------------------------*
*& Form rowheight
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->HEIGHT text
*---------------------------------------------------------------------*
FORM
ROWHEIGHT
USING
ROW HEIGHT .
CALL METHOD
OF
EXCEL_OBJ
'ROWS'
= ROW_OBJ
EXPORTING
#1 = ROW.
SET
PROPERTY
OF
ROW_OBJ
'RowHeight'
= HEIGHT .
FREE OBJECT ROW_OBJ.
ENDFORM
.
"rowheight
*&--------------------------------------------------------------------*
*& Form ColumnWidth
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->WIDTH text
*---------------------------------------------------------------------*
FORM
COLUMNWIDTH
USING
COLUMN WIDTH .
CALL METHOD
OF
EXCEL_OBJ
'COLUMNS'
= COLUMN_OBJ
EXPORTING
#1 = COLUMN.
IF WIDTH =
''
.
CALL METHOD
OF
COLUMN_OBJ
'AutoFit'
.
ELSE
.
SET
PROPERTY
OF
COLUMN_OBJ
'columnwidth'
= WIDTH .
ENDIF
.
FREE OBJECT COLUMN_OBJ.
ENDFORM
.
"columnwidth
*&--------------------------------------------------------------------*
*& Form borderrange
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->WE text
*---------------------------------------------------------------------*
FORM
BORDERRANGE
USING
RANGE.
CALL METHOD
OF
EXCEL_OBJ
'RANGE'
= CELL_OBJ
EXPORTING
#1 = RANGE.
DO
4 TIMES .
CALL METHOD
OF
CELL_OBJ
'BORDERS'
= BORDERS_OBJ
EXPORTING
#1 = SY-INDEX.
SET
PROPERTY
OF
BORDERS_OBJ
'LineStyle'
=
'1'
.
SET
PROPERTY
OF
BORDERS_OBJ
'WEIGHT'
=
'2'
.
SET
PROPERTY
OF
BORDERS_OBJ
'ColorIndex'
=
'1'
.
FREE OBJECT BORDERS_OBJ.
ENDDO
.
FREE OBJECT BORDERS_OBJ.
FREE OBJECT CELL_OBJ.
ENDFORM
.
"borderrange
*---------------------------------------------------------------------*
* FORM font *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> bold *
* --> size *
*---------------------------------------------------------------------*
FORM
FONT
USING
RANGE BOLD SIZE NAME.
CALL METHOD
OF
EXCEL_OBJ
'RANGE'
= CELL_OBJ
EXPORTING
#1 = RANGE.
CALL METHOD
OF
CELL_OBJ
'FONT'
= FONT_OBJ.
SET
PROPERTY
OF
FONT_OBJ
'BOLD'
= BOLD.
SET
PROPERTY
OF
FONT_OBJ
'SIZE'
= SIZE.
SET
PROPERTY
OF
FONT_OBJ
'NAME'
= NAME.
FREE OBJECT FONT_OBJ.
FREE OBJECT CELL_OBJ.
ENDFORM
.
"font
*&---------------------------------------------------------------------*
*& Form CENTERCELL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RANGE text
*----------------------------------------------------------------------*
FORM
CENTERCELL
USING
RANGE.
CALL METHOD
OF
EXCEL_OBJ
'RANGE'
= CELL_OBJ
EXPORTING
#1 = RANGE.
SET
PROPERTY
OF
CELL_OBJ
'HorizontalAlignment'
= 3.
FREE OBJECT CELL_OBJ.
ENDFORM
.
"CENTERCELL
http://sap.bloggersonline.com/post/2009/04/04/ABAP-%e5%af%bc%e5%87%ba%e5%88%b0EXCEL(%e6%a8%a1%e6%9d%bf%e4%ba%8c).aspx --ABAP 导出到EXCEL(模板二)
|