OLE填充EXCEL
先把基本的FORM写好:
1.行列属性
FORM row_column USING p_r p_width p_type.
CASE p_type.
WHEN 'R'. "行高
CALL METHOD OF gs_excel 'CELLS' = gs_cells EXPORTING # = p_r # = .
GET PROPERTY OF gs_cells 'rows' = gs_rows.
SET PROPERTY OF gs_rows 'rowheight' = p_width.
WHEN 'C'. "列宽
CALL METHOD OF gs_excel 'CELLS' = gs_cells EXPORTING # = # = p_r.
GET PROPERTY OF gs_cells 'columns' = gs_columns .
SET PROPERTY OF gs_columns 'columnwidth' = p_width.
ENDCASE.
FREE OBJECT gs_rows.
FREE OBJECT gs_columns.
FREE OBJECT gs_cells.
ENDFORM. " PRM_EXPORT_EXCEL1
2.指定行列边框,字体
FORM merged USING x1 y1 x2 y2 p_linestyle.
*--Selecting cell area to be merged.
CALL METHOD OF gs_excel 'Cells' = gs_cell1
EXPORTING
# = x1
# = y1.
CALL METHOD OF gs_excel 'Cells' = gs_cell2
EXPORTING
# = x2
# = y2.
CALL METHOD OF gs_excel 'Range' = gs_range
EXPORTING
# = gs_cell1
# = gs_cell2.
CALL METHOD OF gs_range 'Select'.
*--Merging
CALL METHOD OF gs_range 'Merge' . GET PROPERTY OF gs_range 'borders' = gs_borders .
SET PROPERTY OF gs_borders 'weight' = ''.
SET PROPERTY OF gs_borders 'linestyle' = p_linestyle. FREE OBJECT gs_range.
FREE OBJECT gs_borders. ENDFORM. " MERGED
3.单元格填充
FORM fill_cell_gs USING p_r1
p_r2
p_bold
p_size
p_linestyle
p_value.
CALL METHOD OF gs_excel 'CELLS' = gs_cells EXPORTING # = p_r1 # = p_r2.
* 设置被选中单元格的对齐方式
" && 水平方向 2左对齐,3居中,4右对齐
SET PROPERTY OF gs_cells 'horizontalAlignment' = .
"&& 垂直方向 1靠上 ,2居中,3靠下
SET PROPERTY OF gs_cells 'VerticalAlignment' = .
"自动换行
SET PROPERTY OF gs_cells 'WrapText' = .
"设置边框属性
GET PROPERTY OF gs_cells 'borders' = gs_borders .
SET PROPERTY OF gs_borders 'weight' = ''.
SET PROPERTY OF gs_borders 'linestyle' = p_linestyle.
"设置字体属性
CALL METHOD OF gs_cells 'FONT' = gs_font.
SET PROPERTY OF gs_font 'BOLD' = p_bold. "1:粗体 0:普通字体
SET PROPERTY OF gs_font 'SIZE' = p_size. "字体大小
"设置单元格的值
SET PROPERTY OF gs_cells 'VALUE' = p_value. FREE OBJECT gs_font.
FREE OBJECT gs_borders.
FREE OBJECT gs_cells. ENDFORM.
3.2居中填充单元格
FORM fill_cell_gs2 USING p_r1
p_r2
p_bold
p_size
p_linestyle
p_value.
CALL METHOD OF gs_excel 'CELLS' = gs_cells EXPORTING # = p_r1 # = p_r2.
* 设置被选中单元格的对齐方式
" && 水平方向 2左对齐,3居中,4右对齐
SET PROPERTY OF gs_cells 'horizontalAlignment' = .
"&& 垂直方向 1靠上 ,2居中,3靠下
SET PROPERTY OF gs_cells 'VerticalAlignment' = .
"自动换行
SET PROPERTY OF gs_cells 'WrapText' = .
"设置边框属性
GET PROPERTY OF gs_cells 'borders' = gs_borders .
SET PROPERTY OF gs_borders 'weight' = ''.
SET PROPERTY OF gs_borders 'linestyle' = p_linestyle.
"设置字体属性
CALL METHOD OF gs_cells 'FONT' = gs_font.
SET PROPERTY OF gs_font 'BOLD' = p_bold. "1:粗体 0:普通字体
SET PROPERTY OF gs_font 'SIZE' = p_size. "字体大小
"设置单元格的值
SET PROPERTY OF gs_cells 'VALUE' = p_value. FREE OBJECT gs_font.
FREE OBJECT gs_borders.
FREE OBJECT gs_cells. ENDFORM.
4.插入行:
*&---------------------------------------------------------------------*
*& 向excel中的指定行插入N行
*&---------------------------------------------------------------------*
FORM excel_row_insert USING lcobj_sheet
lc_row
lc_count.
DATA lc_range TYPE ole2_object.
DATA h_borders TYPE ole2_object.
DATA:p_row TYPE i. p_row = lc_row - .
DO lc_count TIMES.
CALL METHOD OF lcobj_sheet 'Rows' = lc_range
EXPORTING # = p_row.
CALL METHOD OF lc_range 'Copy'.
"copy第6行插入一个新行
CALL METHOD OF lcobj_sheet 'Rows' = lc_range
EXPORTING # = p_row.
CALL METHOD OF lc_range 'Insert'.
CALL METHOD OF lc_range 'ClearContents'. "是否需要清空Cell
ENDDO.
ENDFORM. "excel_row_insert
5.正式的数据填充。excel打开,关闭,保存
CREATE OBJECT gs_excel 'EXCEL.APPLICATION'.
*设置EXCEL可见
SET PROPERTY OF gs_excel 'Visible' = .
*创建工作表
CALL METHOD OF gs_excel 'Workbooks' = gs_workbook. CALL METHOD OF gs_workbook 'Open'
EXPORTING
# = gv_file3. "打开上面下载路径下的excel文件
*选中相应sheet设置名称
CALL METHOD OF gs_excel 'WORKSHEETS' = gs_sheet
EXPORTING
# = .
CALL METHOD OF gs_sheet 'Select' .
CALL METHOD OF gs_sheet 'ACTIVATE'. ls_head-ktext = m_kunnr."利润中心先赋值
SELECT SINGLE name1 INTO ls_head-m_kunnr
FROM kna1 WHERE kunnr = s_kunnr-low. CONCATENATE s_gjahr-low+() '年' s_monat-low+() '月对账单' INTO lv_str1.
PERFORM fill_cell_gs2 USING: lv_str1."抬头填充
PERFORM fill_cell_gs2 USING: ls_head-m_kunnr,"购买方
s_kunnr-low,"客户编码
* 4 4 0 11 0 s_kunnr-low,"客户编码
m_bukrs."供货方 CLEAR:gs_line1.
CONCATENATE '一、' s_gjahr-low '年' s_monat-low '月期间,我司按约定为贵司送货明细如下:' INTO gs_line1.
PERFORM fill_cell_gs2 USING: gs_line1." lv_cur_line = . SORT gt_shows BY wadat_ist ASCENDING.
LOOP AT gt_shows INTO gw_show.
"电话联系人
"ZM业务助理 供货方
IF ls_head-g_cname = '' OR ls_head-ghf_tel = ''.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_vbpa
FROM vbpa
WHERE vbeln = gw_show-vbeln
AND parvw = 'ZM'.
IF sy-subrc = .
SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_pa0002
FROM pa0002
WHERE pernr = gs_vbpa-pernr.
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_pa0105
FROM pa0105 WHERE pernr = gs_vbpa-pernr AND subty IN ('','').
IF gs_pa0002-nachn <> ''.
ls_head-g_cname = gs_pa0002-nachn.
ENDIF.
SORT gt_pa0105 BY pernr subty ASCENDING .
IF ls_head-ghf_tel IS INITIAL .
LOOP AT gt_pa0105.
IF gt_pa0105-subty = ''.
ls_head-ghf_tel = gt_pa0105-usrid.
ELSEIF gt_pa0105-subty = ''.
IF gt_pa0105-usrid_long+() <> ''.
gt_pa0105-usrid_long+() = '-'.
CONCATENATE ls_head-ghf_tel gt_pa0105-usrid_long INTO ls_head-ghf_tel SEPARATED BY '/'.
CONDENSE ls_head-ghf_tel NO-GAPS.
ELSE.
CONDENSE gt_pa0105-usrid_long NO-GAPS.
CONCATENATE ls_head-ghf_tel gt_pa0105-usrid_long INTO ls_head-ghf_tel SEPARATED BY '/'.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
"ZP 联系人 购货方
IF ls_head-m_name = '' OR ls_head-m_tel = ''.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_vbpa
FROM vbpa
WHERE vbeln = gw_show-vgbel
AND parvw = 'ZP'.
IF sy-subrc = .
SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_knvk
FROM knvk
WHERE parnr = gs_vbpa-parnr
AND kunnr = gw_show-kunnr. SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_adcp
FROM adcp
WHERE persnumber = gs_vbpa-adrnp.
IF gs_knvk-name1 <> ''.
ls_head-m_name = gs_knvk-name1.
ENDIF.
IF gs_adcp-tel_number <> ''.
ls_head-m_tel = gs_adcp-tel_number.
ENDIF.
IF gs_adcp-fax_number <> ''.
CONCATENATE ls_head-m_tel gs_adcp-fax_number INTO ls_head-m_tel SEPARATED BY '/'.
ENDIF.
ENDIF.
ENDIF.
"电话联系人 PERFORM fill_cell_gs USING:lv_cur_line gw_show-wadat_ist,
lv_cur_line gw_show-bstkd,
lv_cur_line gw_show-vbeln,
lv_cur_line gw_show-arktx,
lv_cur_line gw_show-zgg,
lv_cur_line gw_show-vrkme,
lv_cur_line gw_show-lfimg,
lv_cur_line gw_show-zdj,
lv_cur_line gw_show-kzwi1.
lv_count = lv_count + gw_show-kzwi1.
lv_cur_line = lv_cur_line + .
AT LAST.
gv_flag = 'X'.
PERFORM fill_cell_gs USING:lv_cur_line lv_count."汇总金额
lv_cur_line = lv_cur_line + .
ENDAT. IF gv_flag = ''.
PERFORM excel_row_insert USING:gs_sheet lv_cur_line ."未结束,插入空白行 等待下一循环
ENDIF.
ENDLOOP. "set
PERFORM fill_cell_gs2 USING: ls_head-g_cname."供货方联系人信息
PERFORM fill_cell_gs2 USING: ls_head-ghf_tel."
PERFORM fill_cell_gs2 USING: ls_head-m_name."购货方联系人信息
PERFORM fill_cell_gs2 USING: ls_head-m_tel."
"set "截止,货款余额
READ TABLE gt_kxmx WITH KEY kunnr = gw_show-kunnr.
IF sy-subrc = .
gt_kxmx-dm1 = gt_kxmx-dm1 + lv_count.
WRITE gt_kxmx-dm1 TO lv_swzf.
WRITE gt_kxmx-wgq TO lv_wgq.
WRITE gt_kxmx-ygq TO lv_ygq.
ENDIF.
CLEAR:gs_line1.
CONCATENATE '二、截止' s_bldat-high+() '年' s_bldat-high+() '月' s_bldat-high+() '日, '
ls_head-m_kunnr '尚未支付广东正业科技股份有限公司' ls_head-ktext '货款 ' lv_swzf ' 元,'
'其中本月未过期货款 ' lv_wgq ' 元,已到期货款 ' lv_ygq ' 元,具体明细如下:'
INTO gs_line1.
* CONDENSE gs_line1 NO-GAPS."去空格
PERFORM fill_cell_gs2 USING:lv_cur_line gs_line1."
lv_cur_line = lv_cur_line + ."未清清单开始需加2 CLEAR:gv_flag.
SORT ct_bsid BY gjahr ASCENDING monat ASCENDING.
LOOP AT ct_bsid.
PERFORM fill_cell_gs USING:lv_cur_line ct_bsid-gjahr,"年
lv_cur_line ct_bsid-monat,"月
lv_cur_line ct_bsid-dmbtr."未清合计
lv_cur_line = lv_cur_line + .
AT LAST.
gv_flag = 'X'.
ENDAT.
IF gv_flag = ''.
PERFORM excel_row_insert USING:gs_sheet lv_cur_line ."未结束,插入空白行 等待下一循环
ENDIF.
ENDLOOP. IF ct_bsid[] IS INITIAL..
lv_cur_line = lv_cur_line + ."add by ly 20160216
ENDIF. CLEAR gs_line1.
CONCATENATE '请 ' ls_head-m_kunnr '收到本对账单之日起于5个工作日内予以签章确认,并回传至广东正业科技股份有限公司。'
'否则,广东正业科技股份有限公司视同贵司对本账单确认无误!' INTO gs_line1.
PERFORM fill_cell_gs2 USING:lv_cur_line gs_line1."
PERFORM merged USING lv_cur_line lv_cur_line ."不知道什么原因,多出个边框 lv_cur_line = lv_cur_line + .
PERFORM fill_cell_gs2 USING:lv_cur_line ls_head-g_cname."供货方制表
PERFORM fill_cell_gs2 USING:lv_cur_line ls_head-m_kunnr."购货方 lv_cur_line = lv_cur_line + .
PERFORM fill_cell_gs2 USING:lv_cur_line sy-datum."制表日期 GET PROPERTY OF gs_excel 'ActiveWorkbook' = gs_workbook.
CALL METHOD OF gs_workbook 'SAVE'.
IF sy-subrc = .
MESSAGE '数据已导出' TYPE 'S'.
ELSE.
MESSAGE '数据导出失败' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
*
CALL METHOD OF gs_workbook 'CLOSE'.
CALL METHOD OF gs_excel 'QUIT'.
FREE OBJECT gs_sheet.
FREE OBJECT gs_workbook.
FREE OBJECT gs_excel.
剩下的自己研究。。。
OLE填充EXCEL的更多相关文章
- OLE填充EXCEL 多SHEET
"1 设置行高 "参数说明:行/列号.行高/列宽.R-行 C-列 FORM row_column USING p_r p_width p_type. CASE p_type. WH ...
- (原创)带模板的OLE输出EXCEL
其实带模板的OLE输出EXCEL就是将要输出的EXCEL中一些拥有固定值(如标题,表头行等)的单元格先填充好数据和设置好格式后作为模板上传到SAP 中.这样后续在输出EXCEL时只需从SAP中将模板下 ...
- OLE操作Excel编译错误处理
Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误.这几天研究了下OLE操作Excel.环境:VS2008 SP1+Excel 2007 加入OLE Type Li ...
- 2018年,请不要再使用OLE生成EXCEL文件
输出EXCEL文件是ABAP开发工作中的常见需求,为了学习相关技术,我翻译过一篇文章:使用OLE2对象创建EXCEL文件,并且一度乐在其中. 最近几个月,经过与若干EXCEL打印程序的艰苦斗争,以及对 ...
- C# 填充Excel
1.添加引用 Microsoft.Office.Interop.Excel; 2.使用命名空间 using Microsoft.Office.Interop.Excel; 3.填充EXCEL单元格方法 ...
- (原创)不带模板的OLE输出EXCEL
目前我已知的EXCEL输出方式有3种: 1.GUI_DOWNLOAD函数输出(适用于简单无格式要求的输出). 2.OLE输出(适用于对EXCEL格式输出有特殊要求的,但是因其填充数据和设置格式是基于一 ...
- 使用Win32::OLE操作Excel——Excel对象模型
像VBA操作Excel一样,Win32::OLE模块也是通过对象操作来控制Excel. 如果想自动化操作和控制Excel应用程序,则必须要与Excel对象模型所提供的对象进行交互.理解和熟悉Excel ...
- C# 填充Excel图表、图例背景色
填充背景色,一般可以选择多种不同样式来填充背景,包括填充为纯色背景.渐变背景.图片背景或者纹理背景等.下面的内容将分别介绍通过C#来设置Excel中图表背景色.以及图表中的图例背景色的方法. 使用工具 ...
- c#用object将datatable快速填充excel后下载表格后打不开的问题
最近在用c#的asp.net,需要批量导出数据.原本用的是stringbuilder逐个填充,但是只能做到html强制格式转换为xls,这不是真正的excel表格,所以在网上找了datatable快速 ...
随机推荐
- [POJ2182]Lost Cows(树状数组,二分)
题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...
- [SAP ABAP开发技术总结]字符串处理函数、正则表达式
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 19 sql语句1
创建 欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com . 过错 create table tableName (字段名 字段类型 pr ...
- iOS - OC NSKeyedArchiver 数据归档
前言 @interface NSKeyedArchiver : NSCoder @interface NSKeyedUnarchiver : NSCoder 在 OC 语言中,归档是一个过程,即用某种 ...
- fFFmpeg 命令、案例、测试集中营
gitbook: https://www.gitbook.com/book/xdsnet/other-doc-cn-ffmpeg/details ffmpeg [全局选项] {[输入文件选项] -i ...
- [转载] TLS协议分析 与 现代加密通信协议设计
https://blog.helong.info/blog/2015/09/06/tls-protocol-analysis-and-crypto-protocol-design/?from=time ...
- [转载] 新兵训练营系列课程——平台服务部署及Web框架
原文: http://weibo.com/p/1001643875679132642345 大纲 微博平台主要负责微博基础功能.接下来将会介绍 平台的作用,以及服务提供的形式 平台Web服务的部署 平 ...
- 线程高级应用-心得6-java5线程并发库中同步工具类(synchronizers),新知识大用途
1.新知识普及 2. Semaphore工具类的使用案例 package com.java5.thread.newSkill; import java.util.concurrent.Executor ...
- Python中的join()函数split()函数
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的 ...
- 基础1 JavaSe基础
JavaSe基础 1. 九种基本数据类型的大小,以及他们的封装类 boolean 无明确指定 Boolean char 16bits Character byte 8bits Byte short 1 ...