人法地,地法天,天法道,道法自然
分类:
2009-04-08 11:15:08
通过粘贴把内表数据写入Excel
同样是要用到OLE,把内表数据写成行,用分隔符隔开,把数据拷贝到剪切板,通过粘贴把数据输出到Excel中,这种方式比逐个单元格写入更快。
效果如下(图S-1):
导出数据后(图S-2):
相关代码处理:
FORM exp_excel .
if isSlinkee <> 'X' .
PERFORM FRM_SELSHEET . "打开文件保存对话框
perform process_data. "数据处理
PERFORM create_excel_obj . "创建excel应用程序
PERFORM create_sheet_obj. "创建sheet并为之命名
PERFORM fill_title . "创建表头和标题
perform fill_sheet. "复制数据
perform fill_cell_color.
perform fill_ok . "处理完毕,并显示excel
endif.
ENDFORM .
FORM create_excel_obj . "创建excel应用程序
FORM create_sheet_obj. "创建sheet并为之命名
form fill_ok . "处理完毕,并显示excel
上面三个Form参考:ABAP将内表数据写入Excel 2003的通用方法
FORM fill_title . "set title
perform fill_cell using 1 5 3 ' ' .
perform set_font_style using 1 5 20 0.
perform set_cell_style using 1 5 28 3.
perform fill_cell using 2 5 2 '供应商物料清单' .
perform set_font_style using 2 5 15 0.
perform set_cell_style using 2 5 20 3.
perform fill_cell using 4 1 1 '物料编码'.
perform fill_cell using 4 2 1 '物料描述'.
perform fill_cell using 4 3 1 '移动类型'(003) .
perform fill_cell using 4 4 1 '物料凭证'(004) .
perform fill_cell using 4 5 1 '记帐日期'(005) .
perform fill_cell using 4 6 1 '数量'(006) .
perform fill_cell using 4 7 1 '单位'(007) .
perform fill_cell using 4 8 1 '采购订单'(008) .
perform fill_cell using 4 9 1 '备注'(009) .
perform fill_cell using 4 10 1 '工厂'(010) .
perform fill_cell using 4 11 1 '供应商'(011) .
perform set_cell_style using 4 1 14 3.
perform set_cell_style using 4 2 40 3.
perform set_cell_style using 4 3 10 3.
perform set_cell_style using 4 4 11 3.
perform set_cell_style using 4 5 11 3.
perform set_cell_style using 4 6 10 3.
perform set_cell_style using 4 7 5 3.
perform set_cell_style using 4 8 11 3.
perform set_cell_style using 4 9 20 3.
perform set_cell_style using 4 10 8 3.
perform set_cell_style using 4 11 8 3.
* perform set_cell_border using 4 1 2.
* perform set_cell_border using 4 2 2.
* perform set_cell_border using 4 3 2.
* perform set_cell_border using 4 4 2.
* perform set_cell_border using 4 5 2.
* perform set_cell_border using 4 6 2.
* perform set_cell_border using 4 7 2.
* perform set_cell_border using 4 8 2.
* perform set_cell_border using 4 9 2.
* perform set_cell_border using 4 10 2.
* perform set_cell_border using 4 11 2.
ENDFORM.
FORM fill_sheet.
* 定义分隔符
CLASS CL_ABAP_CHAR_UTILITIES DEFINITION LOAD.
LD_SEPARATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
data tem_menge type char17.
clear EXCEL_TAB[].
loop at item.
if item-menge < 0 .
item-menge = -1 * item-menge.
tem_menge = item-menge.
concatenate '-' tem_menge into tem_menge.
else.
tem_menge = item-menge.
endif.
condense tem_menge no-gaps.
if item-matnr = '合计:' .
concatenate '合计:' item-maktx item-bwart item-mblnr '' tem_menge item-meins item-ebeln item-mmtxt item-werks item-lifnr
into tem_data-lines SEPARATED BY ld_separator .
else.
concatenate item-matnr item-maktx item-bwart item-mblnr item-budat tem_menge item-meins item-ebeln item-mmtxt item-werks item-lifnr
into tem_data-lines SEPARATED BY ld_separator .
endif.
append tem_data to excel_tab.
endloop.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_EXPORT
IMPORTING
DATA = EXCEL_TAB
CHANGING
RC = LD_RC
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
GET PROPERTY OF excel_obj 'ACTIVESHEET' = sheet_obj.
CALL METHOD OF sheet_obj 'Cells' = cell_obj EXPORTING #1 = 5 #2 = 1.
CALL METHOD OF cell_obj 'SELECT' .
CALL METHOD OF sheet_obj 'PASTE'.
ENDFORM.
form fill_cell_color.
data : h type i,
s type i,
e type i,
start(9) type c, "起始值
end(9) type c, "结束值
formula type string .
h = 4 .
s = 5 .
e = 4 .
loop at item.
h = h + 1.
e = e + 1.
if item-matnr = '合计:' .
e = e - 1 .
start = s .
end = e .
condense start no-gaps.
condense end no-gaps.
concatenate '=SUM(F' start ':' 'F' end ')' into formula.
condense formula no-gaps.
perform fill_cell using h 1 1 '合计:' .
perform fill_cell using h 6 1 formula . "采用动态求和公式来计算合计
if item-menge < 0 .
perform set_font_style using h 6 11 -16776961 . "红色
else.
perform set_font_style using h 6 11 -11489280 . "绿色
endif.
s = e + 2 .
e = e + 1 .
endif.
endloop.
endform.