types: begin of xml_line,
data(255) type x,
end of xml_line.
data:l_xml_table type table of xml_line.
data:ls_itab type typ_itab.
types: begin of xml_line,
data(256) type x,
end of xml_line.
data: l_ixml type ref to if_ixml,
l_streamfactory type ref to if_ixml_stream_factory,
l_ostream type ref to if_ixml_ostream,
l_renderer type ref to if_ixml_renderer,
l_document type ref to if_ixml_document.
data:
l_element_root type ref to if_ixml_element,
ns_attribute type ref to if_ixml_attribute,
r_element_properties type ref to if_ixml_element,
r_element type ref to if_ixml_element,
r_worksheet type ref to if_ixml_element,
r_table type ref to if_ixml_element,
r_column type ref to if_ixml_element,
r_row type ref to if_ixml_element,
r_styles type ref to if_ixml_element,
r_style type ref to if_ixml_element,
r_borders type ref to if_ixml_element,
r_border type ref to if_ixml_element,
r_format type ref to if_ixml_element,
r_cell type ref to if_ixml_element,
r_data type ref to if_ixml_element,
* l_xml_table TYPE TABLE OF xml_line,
l_xml_size type i,
l_rc type i,
l_text(255) type c,
l_value type string,
l_type type string,
p_type .
field-symbols:
type any,
type any,
type any,
type any .
data:p_tab like dd02l-tabname .
data:
data_tab type ref to data,
gotstate type ddgotstate,
dd03p_tab type table of dd03p,
dd03p type dd03p.
field-symbols:
type standard table.
field-symbols:
like line of dd03p_tab.
clear l_xml_table.
define add_fieldcol.
unassign: .
clear dd03p.
dd03p-fieldname = &1.
dd03p-ddtext = &2.
dd03p-outputlen = &3.
dd03p-inttype = &4.
dd03p-decimals = &5.
dd03p-reptext = &6.
dd03p-position = &7.
append dd03p to dd03p_tab assigning .
end-of-definition.
add_fieldcol 'PRUEFLOS' '检验批' '8' 'C' '0' '' ''.
add_fieldcol 'WERK' '工厂' '4' 'C' '0' '' ''.
add_fieldcol 'CHARG' '批次' '6' 'C' '0' '' ''.
add_fieldcol 'MATNR' '物料编码' '10' 'C' '0' '' ''.
add_fieldcol 'MAKTX' '物料描述' '35' 'C' '0' '' ''.
add_fieldcol 'LOSMENGE' '交检数量' '8' 'P' '3' '' ''.
add_fieldcol 'GESSTICHPR' '抽检数量' '8' 'P' '3' '' ''.
add_fieldcol 'ERSTELDAT' '收货日期' '8' 'C' '0' '' ''.
add_fieldcol 'BUDAT' '过账日期' '8' 'C' '0' '' ''.
add_fieldcol 'LAGORTCHRG' '库位' '4' 'C' '0' '' ''.
add_fieldcol 'OBJNR' '工作指示编号' '8' 'C' '0' '' ''.
add_fieldcol 'ERSTELLER' '创建者' '8' 'C' '0' '' ''.
add_fieldcol 'TXT04' '系统状态' '35' 'C' '0' '' ''.
* SORT dd03p_tab BY position .
create data data_tab type standard table of typ_itab."STANDARD TABLE OF (P_TAB).
* CREATE DATA DATA_TAB TYPE STANDARD TABLE OF (P_TAB).
assign data_tab->* to .
loop at gt_itab into ls_itab .
append ls_itab to .
endloop.
* Creating a ixml factory
l_ixml = cl_ixml=>create( ).
* Creating the dom object model
l_document = l_ixml->create_document( ).
* Create root node 'Workbook'
l_element_root = l_document->create_simple_element(
name = 'Workbook'
* uri = 'urn:schemas-microsoft-com:office:spreadsheet'
parent = l_document ).
l_element_root->set_attribute(
name = 'xmlns'
value = 'urn:schemas-microsoft-com:office:spreadsheet' ).
ns_attribute = l_document->create_namespace_decl(
name = 'ss'
prefix = 'xmlns'
uri = 'urn:schemas-microsoft-com:office:spreadsheet' ).
l_element_root->set_attribute_node( ns_attribute ). ns_attribute = l_document->create_namespace_decl(
name = 'x'
prefix = 'xmlns'
uri = 'urn:schemas-microsoft-com:office:excel' ).
l_element_root->set_attribute_node( ns_attribute ).
* Create node for document properties.
r_element_properties = l_document->create_simple_element(
name = 'DocumentProperties'
parent = l_element_root ).
l_value = sy-uname.
l_document->create_simple_element(
name = 'Author'
value = l_value
parent = r_element_properties ).
* Styles
r_styles = l_document->create_simple_element(
name = 'Styles'
parent = l_element_root ).
***** 抬头
* Style for Header - Ice Blue, BOLD
r_style = l_document->create_simple_element(
name = 'Style'
parent = r_styles ).
r_style->set_attribute_ns(
name = 'ID'
prefix = 'ss'
value = 'Header' ).
r_format = l_document->create_simple_element(
name = 'Font'
parent = r_style ).
r_format->set_attribute_ns(
name = 'Bold'
prefix = 'ss'
value = '1' ).
r_format = l_document->create_simple_element(
name = 'Interior'
parent = r_style ).
r_format->set_attribute_ns(
name = 'Color'
prefix = 'ss'
value = '#F2F2F2' ).
r_format->set_attribute_ns(
name = 'Pattern'
prefix = 'ss'
value = 'Solid' ).
r_format = l_document->create_simple_element(
name = 'Alignment'
parent = r_style ).
r_format->set_attribute_ns(
name = 'Horizontal'
prefix = 'ss'
value = 'Center' ).
r_format->set_attribute_ns(
name = 'Vertical'
prefix = 'ss'
value = 'Center' ).
r_format->set_attribute_ns(
name = 'WrapText'
prefix = 'ss'
value = '1' ).
**** 设置颜色格式
***** 设置 合计行格式
r_style = l_document->create_simple_element(
name = 'Style'
parent = r_styles ).
r_style->set_attribute_ns(
name = 'ID'
prefix = 'ss'
value = 'RLine' ).
r_format = l_document->create_simple_element(
name = 'Interior'
parent = r_style ).
r_format->set_attribute_ns(
name = 'Color'
prefix = 'ss'
value = '#FF0000' ).
r_format->set_attribute_ns(
name = 'Pattern'
prefix = 'ss'
value = 'Solid' ).
* r_borders = l_document->create_simple_element(
* name = 'Borders'
* parent = r_style ).
* r_format = l_document->create_simple_element(
* name = 'Border'
* parent = r_borders ).
* r_format->set_attribute_ns(
* name = 'Position'
* prefix = 'ss'
* value = 'Bottom' ).
* r_format->set_attribute_ns(
* name = 'LineStyle'
* prefix = 'ss'
* value = 'Dash' ).
*
* r_format = l_document->create_simple_element(
* name = 'Border'
* parent = r_borders ).
* r_format->set_attribute_ns(
* name = 'Position'
* prefix = 'ss'
* value = 'Right' ).
* r_format->set_attribute_ns(
* name = 'LineStyle'
* prefix = 'ss'
* value = 'Dash' ).
*
* r_format = l_document->create_simple_element(
* name = 'Border'
* parent = r_borders ).
* r_format->set_attribute_ns(
* name = 'Position'
* prefix = 'ss'
* value = 'Top' ).
* r_format->set_attribute_ns(
* name = 'LineStyle'
* prefix = 'ss'
* value = 'Dash' ).
* r_format = l_document->create_simple_element(
* name = 'Font'
* parent = r_style ).
* r_format->set_attribute_ns(
* name = 'Bold'
* prefix = 'ss'
* value = '1' ).
****** 设置颜色行是 黄色
r_style = l_document->create_simple_element(
name = 'Style'
parent = r_styles ).
r_style->set_attribute_ns(
name = 'ID'
prefix = 'ss'
value = 'YLine' ).
r_format = l_document->create_simple_element(
name = 'Interior'
parent = r_style ).
r_format->set_attribute_ns(
name = 'Color'
prefix = 'ss'
value = '#FFFF00' ).
r_format->set_attribute_ns(
name = 'Pattern'
prefix = 'ss'
value = 'Solid' ).
* Style for Data
loop at dd03p_tab into dd03p where fieldname <> 'MANDT'.
case dd03p-inttype.
when 'I' or 'N'.
* General format
when 'P' or 'F'.
* Numeric with specific number of decimals
r_style = l_document->create_simple_element(
name = 'Style'
parent = r_styles ).
l_value = dd03p-fieldname.
r_style->set_attribute_ns(
name = 'ID'
prefix = 'ss'
value = l_value ).
r_format = l_document->create_simple_element(
name = 'NumberFormat'
parent = r_style ).
if dd03p-decimals > 0.
l_value = '0.'.
do dd03p-decimals times.
concatenate l_value '0' into l_value.
enddo.
else.
l_value = ''.
endif.
* l_value = '#,##0' .
r_format->set_attribute_ns(
name = 'Format'
prefix = 'ss'
value = l_value ).
* WHEN 'D'.
** General format
when others.
* Fixed text
r_style = l_document->create_simple_element(
name = 'Style'
parent = r_styles ).
l_value = dd03p-fieldname.
r_style->set_attribute_ns(
name = 'ID'
prefix = 'ss'
value = l_value ).
r_format = l_document->create_simple_element(
name = 'NumberFormat'
parent = r_style ).
l_value = '@'.
r_format->set_attribute_ns(
name = 'Format'
prefix = 'ss'
value = l_value ).
endcase.
endloop .
* Worksheet
r_worksheet = l_document->create_simple_element(
name = 'Worksheet '
parent = l_element_root ).
r_worksheet->set_attribute_ns(
name = 'Name'
prefix = 'ss'
value = 'Sheet1' ).
* TABLE
*
r_table = l_document->create_simple_element(
name = 'Table'
parent = r_worksheet ).
r_table->set_attribute_ns(
name = 'FullColumns'
prefix = 'x'
value = '1' ).
r_table->set_attribute_ns(
name = 'FullRows'
prefix = 'x'
value = '1' ).
* Column formatting ==>给对应列赋STYLE和WIDE
loop at dd03p_tab into dd03p where fieldname <> 'MANDT'.
* COLUMNN
r_column = l_document->create_simple_element(
name = 'Column'
parent = r_table ).
case dd03p-inttype.
when 'I' or 'N'.
* General format
when 'P' or 'F'.
l_value = dd03p-fieldname.
r_column->set_attribute_ns(
name = 'StyleID'
prefix = 'ss'
value = l_value ).
* WHEN 'D'.
** General format
when others.
l_value = dd03p-fieldname.
r_column->set_attribute_ns(
name = 'StyleID'
prefix = 'ss'
value = l_value ).
* ENDIF.
endcase.
l_value = ( dd03p-outputlen + 5 ) * 5.
condense l_value no-gaps.
r_column->set_attribute_ns(
name = 'Width'
prefix = 'ss'
value = l_value ).
endloop.
"抬头
* Column Headers Row
r_row = l_document->create_simple_element(
name = 'Row'
parent = r_table ).
r_row->set_attribute_ns(
name = 'StyleID'
prefix = 'ss'
value = 'Header' ).
r_row->set_attribute_ns(
name = 'AutoFitHeight'
prefix = 'ss'
value = '1' ).
** Column name
loop at dd03p_tab into dd03p where fieldname <> 'MANDT'.
*
r_cell = l_document->create_simple_element(
name = 'Cell'
parent = r_row ).
* ENDIF.
l_value = dd03p-ddtext. "fieldname, scrtext_m etc.
r_data = l_document->create_simple_element(
name = 'Data'
value = l_value
parent = r_cell ).
r_data->set_attribute_ns(
name = 'Type'
prefix = 'ss'
value = 'String' ).
endloop.
loop at assigning .
assign component 'LIGHT' of structure to .
* ROW
r_row = l_document->create_simple_element(
name = 'Row'
parent = r_table ).
loop at dd03p_tab into dd03p where fieldname <> 'MANDT'.
assign component dd03p-fieldname of structure to .
check sy-subrc is initial.
*
r_cell = l_document->create_simple_element(
name = 'Cell'
parent = r_row ).
case dd03p-inttype.
when 'I' or 'P' or 'F' or 'N'.
l_type = 'Number'.
* L_VALUE = .
* CONDENSE L_VALUE NO-GAPS.
"负数时,将负号提前
if < 0.
l_text = abs( ).
condense l_text.
concatenate '-' l_text into l_value.
* L_VALUE = L_TEXT.
condense l_value no-gaps.
else.
l_value = .
if l_value is initial or l_value = 0.
l_value = '-' .
endif.
condense l_value no-gaps.
endif.
when 'D' or 'T'.
l_type = 'String'.
if = '00000000'.
l_value = ''.
else.
write to l_text.
l_value = l_text.
endif.
when others.
write to l_text.
shift l_text left deleting leading space.
l_value = l_text.
l_type = 'String'.
endcase.
**
if = icon_led_red. " 异常通知单 红色显示
r_cell->set_attribute_ns(
name = 'StyleID'
prefix = 'ss'
value = 'RLine' ).
else.
r_cell->set_attribute_ns(
name = 'StyleID'
prefix = 'ss'
value = 'YLine' ).
endif.
r_data = l_document->create_simple_element(
name = 'Data'
value = l_value
parent = r_cell ).
* Cell format
r_data->set_attribute_ns(
name = 'Type'
prefix = 'ss'
value = l_type ).
endloop.
endloop.
* CREATING A STREAM FACTORY
l_streamfactory = l_ixml->create_stream_factory( ).
* Connect internal XML table to stream factory
l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table ).
* RENDERING THE DOCUMENT
l_renderer = l_ixml->create_renderer( ostream = l_ostream
document = l_document ).
l_rc = l_renderer->render( ).
* SAVING THE XML DOCUMENT
l_xml_size = l_ostream->get_num_written_raw( ).
***下载本地文件
call method cl_gui_frontend_services=>gui_download
exporting
bin_filesize = l_xml_size
filename = 'D:\xml\ZQMR022.xls'
filetype = 'BIN'
changing
data_tab = l_xml_table
exceptions
others = 24.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
*****发送邮件
data: objpack like sopcklsti1 occurs 2 with header line.
data: objhead like solisti1 occurs 1 with header line.
* DATA: OBJBIN LIKE SOLIX OCCURS 10 WITH HEADER LINE.
data: objtxt like solisti1 occurs 10 with header line.
data: reclist like somlreci1 occurs 5 with header line.
data: objhex like solix occurs 10 with header line.
data: doc_chng like sodocchgi1.
data: tab_lines like sy-tabix.
data: l_num(3).
data: subj_date(10) type c.
data:lv_char type string .
data:mailtitle(100) type c.
data:g_vendername like lfa1-name1.
data g_lifnr like ekko-lifnr.
data: g_adrnr like lfa1-adrnr.
data:sender_address like soextreci1-receiver.
data:wa_xml type xml_line.
data:lt_mail like standard table of zcfg_mail with header line .
data :begin of lt_address occurs 0 ,
smtp_addr type ad_smtpadr ,
end of lt_address .
data :begin of lt_ccaddress occurs 0 ,
smtp_addr type ad_smtpadr ,
end of lt_ccaddress .
*** Mail Object
* CONCATENATE lv_str 'Weekly Sales Report' INTO doc_chng-obj_descr SEPARATED BY space.
doc_chng-obj_descr = '检验批监控' .
*** 邮件正文
clear objtxt.
objtxt = '你好:'.
append objtxt.
clear objtxt.
append objtxt.
clear objtxt.
objtxt = '附件是存在异常检验批监控数据,请查收并及时处理,谢谢 '.
append objtxt.
clear objtxt.
lv_char = '其中异常总数量: ' && gv_line && ';' && '未检验数量:' && gv_wj
&& ';' && '异常数量:' && gv_yc .
objtxt = lv_char .
append objtxt.
describe table objtxt lines tab_lines.
read table objtxt index tab_lines.
doc_chng-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).
* Packing List For the E-mail Body
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
append objpack.
**内作为邮件附件
** Creation of the Document Attachment
loop at l_xml_table into wa_xml.
clear objhex.
objhex-line = wa_xml-data.
append objhex.
endloop.
describe table objhex lines tab_lines."objbin
objhead = '检验批监控'.
append objhead.
* Packing List For the E-mail Attachment
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-obj_descr = '检验批监控异常数据'.
objpack-obj_name = '检验批监控.xls' .
objpack-doc_type = 'XLS'.
objpack-doc_size = tab_lines * 255.
append objpack.
**** 取发件人和收件人以及抄送人,
sender_address = 'account@teamwisepower.com.cn' .
select *
into corresponding fields of table lt_mail
from zcfg_mail
where progname = 'ZQMR022'.
if sy-subrc = 0 .
read table lt_mail index 1 .
split lt_mail-recname at '/' into table lt_address .
split lt_mail-ccname at '/' into table lt_ccaddress .
endif.
loop at lt_address.
clear reclist .
reclist-receiver = lt_address-smtp_addr.
* RECLIST-EXPRESS = 'X'. "发送快件
reclist-rec_type = 'U'.
translate reclist-receiver to upper case.
append reclist.
endloop.
loop at lt_ccaddress.
clear reclist .
reclist-receiver = lt_ccaddress-smtp_addr.
reclist-copy = 'X'. "抄送
reclist-rec_type = 'U'.
translate reclist-receiver to upper case.
append reclist.
endloop.
call function 'SO_DOCUMENT_SEND_API1'
exporting
document_data = doc_chng
put_in_outbox = 'X'
sender_address = sender_address "'qyyu@teamwisepower.com.cn'
sender_address_type = 'SMTP'"B
commit_work = 'X'
tables
packing_list = objpack
object_header = objhead
* contents_bin = objbin
contents_txt = objtxt
contents_hex = objhex
* OBJECT_PARA =
* OBJECT_PARB =
receivers = reclist
exceptions
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
others = 8.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
else.
wait up to 2 seconds.
submit rsconn01 with mode = 'INT' and return.
message '发送成功' type 'S'.
endif.
本文档为【SAP 将EXCEL作为附件发送Email】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
|