ABAP发送报表邮件

运行效果:

代码:

*&---------------------------------------------------------------------*
*& Report ZPPRTEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZPPRTEST. TABLES: AFKO. DATA: BEGIN OF gt_data OCCURS ,
aufnr LIKE afko-aufnr,
plnbez LIKE afko-plnbez,
gstrp LIKE afko-gstrp,
gltrp LIKE afko-gltrp,
END OF gt_data. DATA: BEGIN OF gt_stock OCCURS ,
matnr LIKE marc-matnr,
lgort LIKE mard-lgort,
labst LIKE mard-labst,
END OF gt_stock. DATA:g_string TYPE string,
g_string2 TYPE string,
g_mail_address TYPE ad_smtpadr,
g_def_sel_ord TYPE c. CONSTANTS: gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf. TYPE-POOLS : slis. DATA: gs_layout TYPE slis_layout_alv,
gt_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv. SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-B01.
SELECT-OPTIONS: s_aufnr FOR afko-aufnr.
SELECTION-SCREEN END OF BLOCK B1. SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-B02.
PARAMETERS: c_m1 AS CHECKBOX,
p_t1 TYPE so_obj_des.
SELECT-OPTIONS: s_m1 FOR g_mail_address NO INTERVALS.
SELECTION-SCREEN END OF BLOCK B2. START-OF-SELECTION.
PERFORM get_data.
PERFORM display_data.
END-OF-SELECTION. FORM get_data.
*-->工单
SELECT afko~aufnr afko~plnbez afko~gstrp afko~gltrp
INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM afko
WHERE aufnr IN s_aufnr.
*-->库存
SELECT afko~plnbez AS matnr mard~lgort mard~labst
INTO CORRESPONDING FIELDS OF TABLE gt_stock
FROM afko
INNER JOIN mard ON afko~plnbez EQ mard~matnr
WHERE aufnr IN s_aufnr
AND mard~werks = ''.
ENDFORM. FORM display_data.
DATA: l_repid TYPE sy-repid,
l_inclname TYPE trdir-name,
l_idx LIKE sy-tabix.
CLEAR: gt_fieldcat[], wa_fieldcat. l_repid = l_inclname = sy-cprog. gs_layout-colwidth_optimize = 'X'. CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = l_repid
I_INTERNAL_TABNAME = 'GT_DATA'
I_INCLNAME = l_inclname
CHANGING
CT_FIELDCAT = gt_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE =
PROGRAM_ERROR =
OTHERS = . LOOP AT gt_fieldcat INTO wa_fieldcat.
l_idx = sy-tabix. CASE wa_fieldcat-fieldname.
* WHEN 'sortl'.
* wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
* wa_fieldcat-seltext_l = '供应商简称'.
* wa_fieldcat-ddictxt = 's'.
ENDCASE. CLEAR wa_fieldcat-key. MODIFY gt_fieldcat FROM wa_fieldcat INDEX l_idx.
ENDLOOP.
IF c_m1 = 'X'.
PERFORM concatenate_data.
PERFORM concatenate_data_stock.
PERFORM send_mail TABLES s_m1
USING p_t1 'zpprtest_list'.
ENDIF.
PERFORM alv_show.
ENDFORM. FORM alv_show. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = sy-repid
IS_LAYOUT = gs_layout
IT_FIELDCAT = gt_fieldcat[]
* I_CALLBACK_PF_STATUS_SET = 'ALV_PF_STATUS' " 触发事件调用子程序
* I_CALLBACK_USER_COMMAND = 'ALV_USER_COMMAND' " 鼠标事件操作子程序
I_SAVE = 'A'
TABLES
T_OUTTAB = GT_DATA
EXCEPTIONS
PROGRAM_ERROR =
OTHERS = . ENDFORM. FORM concatenate_data.
DATA: l_line TYPE string,
l_gstrp(),
l_gltrp().
CLEAR g_string.
*-->Header Line
CONCATENATE '工单'
'物料'
'基本开工日'
'基本完工日'
INTO g_string SEPARATED BY gc_tab.
*-->断行
CONCATENATE g_string l_line gc_crlf INTO g_string.
*-->Data
LOOP AT gt_data.
CLEAR: l_gstrp,l_gltrp. CONCATENATE gt_data-gstrp+() '/' gt_data-gstrp+()
'/' gt_data-gstrp+() INTO l_gstrp. CONCATENATE gt_data-gltrp+() '/' gt_data-gltrp+()
'/' gt_data-gltrp+() INTO l_gltrp. CONCATENATE gt_data-aufnr
gt_data-plnbez
l_gstrp
l_gltrp
INTO l_line SEPARATED BY gc_tab.
*-->断行
CONCATENATE g_string l_line gc_crlf INTO g_string.
ENDLOOP. ENDFORM. FORM concatenate_data_stock.
DATA: l_line TYPE string,
l_labst().
CLEAR g_string2.
CONCATENATE '物料'
'仓库'
'库存'
INTO g_string2 SEPARATED BY gc_tab.
CONCATENATE g_string2 l_line gc_crlf INTO g_string2.
LOOP AT gt_stock.
CLEAR: l_labst.
WRITE gt_stock-labst TO l_labst. "数字要转换成字符
CONCATENATE gt_stock-matnr
gt_stock-lgort
l_labst
INTO l_line SEPARATED BY gc_tab.
CONCATENATE g_string2 l_line gc_crlf INTO g_string2.
ENDLOOP.
ENDFORM. FORM send_mail TABLES lt_mail_addr STRUCTURE s_m1
USING in_subject in_filename.
DATA send_request TYPE REF TO cl_bcs.
DATA document TYPE REF TO cl_document_bcs.
DATA recipient TYPE REF TO if_recipient_bcs.
DATA bcs_exception TYPE REF TO cx_bcs. DATA main_text TYPE bcsy_text.
DATA: binary_content TYPE solix_tab,
binary_content2 TYPE solix_tab.
DATA: size TYPE so_obj_len,
size2 TYPE so_obj_len.
DATA sent_to_all TYPE os_boolean.
DATA: l_filename TYPE sood-objdes,
l_filename2 TYPE sood-objdes. CONCATENATE in_filename '_' sy-datum INTO l_filename. CONCATENATE '库存' '_' sy-datum INTO l_filename2. * --------------------------------------------------------------
* convert the text string into UTF-16LE binary data including
* byte-order-mark. Mircosoft Excel prefers these settings
* all this is done by new class cl_bcs_convert (see note 1151257) * 工单
TRY.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = g_string
iv_codepage = '' "suitable for MS Excel, leave empty
iv_add_bom = 'X' "for other doc types
IMPORTING
et_solix = binary_content
ev_size = size ).
CATCH cx_bcs.
MESSAGE e445(so).
ENDTRY. * 库存
TRY.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = g_string2
iv_codepage = '' "suitable for MS Excel, leave empty
iv_add_bom = 'X' "for other doc types
IMPORTING
et_solix = binary_content2
ev_size = size2 ).
CATCH cx_bcs.
MESSAGE e445(so).
ENDTRY. TRY. * -------- create persistent send request ------------------------
send_request = cl_bcs=>create_persistent( ). * -------- create and set document with attachment ---------------
* create document object from internal table with text
* append 'Hello world!' to main_text. "#EC NOTEXT
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = main_text
i_subject = in_subject ). * add the spread sheet as attachment to document object * 工单
document->add_attachment(
i_attachment_type = 'xls' "#EC NOTEXT
i_attachment_subject = l_filename
i_attachment_size = size
i_att_content_hex = binary_content ).
* 库存
document->add_attachment(
i_attachment_type = 'xls' "#EC NOTEXT
i_attachment_subject = l_filename2
i_attachment_size = size2
i_att_content_hex = binary_content2 ). * add document object to send request
send_request->set_document( document ). * --------- add recipient (e-mail address) -----------------------
LOOP AT lt_mail_addr.
* create recipient object
recipient =
cl_cam_address_bcs=>create_internet_address( lt_mail_addr-low ).
* add recipient object to send request
send_request->add_recipient( recipient ).
ENDLOOP. * ---------- send document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ). COMMIT WORK. IF sent_to_all IS INITIAL.
MESSAGE i500(sbcoms).
ELSE.
MESSAGE s022(so).
ENDIF. * ------------ exception handling ----------------------------------
* replace this rudimentary exception handling with your own one !!!
CATCH cx_bcs INTO bcs_exception.
MESSAGE i865(so) WITH bcs_exception->error_type.
ENDTRY.
ENDFORM.

其他博主的好文——SAP压缩excel并发送mail案例

ALV报表——发送Excel报表邮件的更多相关文章

  1. Excel报表

    Excel报表 1.Excel报表导入到GridView protected void Page_Load(object sender, EventArgs e) { string path = Se ...

  2. 通过邮箱发送html报表

    前言 需求是发送邮件时, 可以将报表正文贴到邮件里, 可以正常复制选中报表内容. 目前的做法是简单粗暴的转成了一张图片, 这样效果显然是很糟糕的. 今天看到邮箱里可以预览Word, Excel, F1 ...

  3. 使用 shell / python 进行sql的excel报表导出

    如果要求你进行一个表数据的导出,如果使用shell的话,很容易做到,即执行一下 select 语句就可以拿到返回结果了! 如下: /usr/bin/mysql -u"${username}& ...

  4. 从数据库中导出excel报表

    通常需要将后台数据库中的数据集或者是其他列表等导出excel 报表,这里主要引用了Apose.cells dll 类库, (1)直接上主要代码: protected void txtExport_Cl ...

  5. 在.NET中使用EPPlus生成Excel报表 .

    --摘抄自:http://blog.csdn.net/zhoufoxcn/article/details/14112473 在开发.NET应用中可能会遇到需要生成带图表(Chart)的Excel报表的 ...

  6. IIS下打印报表到Excel

    阅读本文之前,请先看上一篇文章<.NET下Excel报表的打印>. 上一篇文章<.NET下Excel报表的打印>介绍了关于报表打印到Excel文件中的方法.若要把项目通过IIS ...

  7. poi导出Excel报表多表头双层表头、合并单元格

    效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @ ...

  8. Report_客制化以PLSQL输出XLS标记实现Excel报表(案例)

    2015-02-12 Created By BaoXinjian

  9. java导出excel报表

    1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...

随机推荐

  1. 【JZOJ6232】【20190625】喜欢最最痛

    题目 \(n\)个节点的树,边权为正整数. 从1 号点开始走一个路径并最终回到 1 号点,且这条路径经过了所有的边. 一条路径的代价就是它经过的边的边权之和. 可以加若干条额外边,第 i 条加的额外边 ...

  2. 第02组 Alpha冲刺(4/4)

    队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...

  3. python url码转换 chr()码

    爬虫生成带搜索词语的网址 1.字符串转为url编码 import urllib poet_name = "李白" url_code_name = urllib.quote(poet ...

  4. NOIP 2013货车运输

    当然这题有很多做法,但是我看到没有人写DSU的很惊奇 按照之前做连双向边题的经验,这题可以用并查集维护联通 然后对于每个询问\(x,y\),考虑启发式合并 当两个点集\(x,y\)合并时,一些涉及到其 ...

  5. 市值TOP10,人类进化及中美坐标

    题记:观察人类进化,以及各国.各民族在这个进化中所起的作用.所处的位置,市值 TOP 10 的变迁,会是一个再好不过的指标! 2008年,经历了全球金融危机后,原油期货一路飙升,创出了147.27美元 ...

  6. Spark2.x(六十一):在Spark2.4 Structured Streaming中Dataset是如何执行加载数据源的?

    本章主要讨论,在Spark2.4 Structured Streaming读取kafka数据源时,kafka的topic数据是如何被执行的过程进行分析. 以下边例子展开分析: SparkSession ...

  7. yapi内网部署 centos

    1.部署方案 官方说明: https://hellosean1025.github.io/yapi/devops/index.html 2.需要注意的点 (1)在centos等服务启上最好使用“命令行 ...

  8. android --------System.err: java.net.UnknownServiceException: CLEARTEXT .....

    1,当调用远程接口进行网络通信时,报如下错误: W/System.err: java.net.UnknownServiceException: CLEARTEXT communication to 1 ...

  9. springboot响应消息(http)的编码设置

    一.方式一 在单个REST接口上设置 @ResponseBody @RequestMapping(value = "sys/getTree1",method = RequestMe ...

  10. MYSQL定时任务-定时清除备份数据

    背景 由于项目需要,每个月的历史存量数据需要进行一个归档和备份操作,以及一些日志表需要进行一个明细字段清除,让mysql数据库磁盘节省空间.则需要一些定时任务来定时清理这些数据. 技术选型 Java ...