有一个用PCUI下载CSV的需求.
要求下载的CSV文件,既有Head的字段,又有Item的字段.
标准的PCUI下载功能(CRM5.0),允许下载CSV XML
等类型的问,单格式比较死板,不能满足需求.
只能自己动手了.
写一个BSP程序,这个BSP程序只有一个Controller,没有View.
功能比较单一,根据URL传来的参数,在浏览器的新窗口中打开文件下载
对话框,下载CSV文件.
CRM PCUI中Toolbar中加个Button.这个Button功能也很"单纯".
就是通过URL调用上面的BSP程序.
一呼一应,手工打造的PCUI下载功能就完成了.
BSP的代码:
method DO_REQUEST.
*CALL METHOD SUPER->DO_REQUEST
* .
*CALL METHOD SUPER->DO_HANDLE_EVENT
* EXPORTING
* EVENT =
* HTMLB_EVENT =
** htmlb_event_ex =
* GLOBAL_MESSAGES =
* RECEIVING
* GLOBAL_EVENT =
* .
data: l_per type string.
TYPE-POOLS: abap.
*IMPORT gt_data to gt_data FROM MEMORY ID sy-uname.
DATA: BEGIN OF wa,
f1(30),
f2(30),
END OF wa.
DATA: itab TYPE TABLE OF string.
DATA:l_string type string.
* data: L_per type string.
L_per =
request->get_form_field( 'P1' ).
*LOOP AT gt_data INTO ls_data.
CONCATENATE 'Input peremeters:' l_PER
CL_ABAP_CHAR_UTILITIES=>CR_LF
into l_string.
CONCATENATE l_string '-------Format 1-------'
CL_ABAP_CHAR_UTILITIES=>CR_LF
into l_string.
CONCATENATE l_string 'FIELDNAME1'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE1'
cl_abap_char_utilities=>horizontal_tab
INTO l_string
*
.
* 换行
CONCATENATE l_string CL_ABAP_CHAR_UTILITIES=>CR_LF
INTO l_string.
*第二行
CONCATENATE l_string 'FIELDNAME2'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE2'
cl_abap_char_utilities=>horizontal_tab
INTO l_string
* SEPARATED BY cl_abap_char_utilities=>horizontal_tab
.
* 换行
CONCATENATE l_string CL_ABAP_CHAR_UTILITIES=>CR_LF
INTO l_string.
*第三行
CONCATENATE l_string 'FIELDNAME3'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE3'
INTO l_string
* SEPARATED BY cl_abap_char_utilities=>horizontal_tab
.
* 换行
CONCATENATE l_string CL_ABAP_CHAR_UTILITIES=>CR_LF
INTO l_string.
CONCATENATE l_string '-------Format 2-------'
CL_ABAP_CHAR_UTILITIES=>CR_LF
INTO l_string.
CONCATENATE l_string
'FIELDNAME1'
cl_abap_char_utilities=>horizontal_tab
'FIELDNAME2'
cl_abap_char_utilities=>horizontal_tab
'FIELDNAME3'
cl_abap_char_utilities=>horizontal_tab
'FIELDNAME4'
cl_abap_char_utilities=>horizontal_tab
'FIELDNAME5'
CL_ABAP_CHAR_UTILITIES=>CR_LF
INTO l_string
* SEPARATED BY cl_abap_char_utilities=>horizontal_tab
.
CONCATENATE l_string
'FIELDVALUE1'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE2'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE3'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE4'
cl_abap_char_utilities=>horizontal_tab
'FIELDVALUE5'
CL_ABAP_CHAR_UTILITIES=>CR_LF
INTO l_string
* SEPARATED BY cl_abap_char_utilities=>horizontal_tab
.
concatenate l_string '------Out put End-----The show over Thanks---'
into l_string.
*ENDLOOP.
* 'application/vnd.ms-excel'
DATA : w_type(50) TYPE c.
* w_type = 'APPLICATION/MSEXCEL'.
* w_type = 'APPLICATION/VND.MS-EXCEL'.
w_type = 'APPLICATION/MSEXCEL;charset=utf-16le'.
DATA: l_xstring TYPE xstring.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = l_string
mimetype = w_type
IMPORTING
buffer = l_xstring
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc EQ 0.
CONCATENATE cl_abap_char_utilities=>byte_order_mark_little
l_xstring INTO l_xstring IN BYTE MODE.
* some browsers have caching problems when loading excel*
* response->delete_header_field(
* name = if_http_header_fields=>cache_control ).
* response->delete_header_field(
* name = if_http_header_fields=>expires ).
* response->delete_header_field(
* name = if_http_header_fields=>pragma ).
*
runtime->server->response->set_data( l_xstring ).
response->set_header_field(
name = 'content-disposition'
value = 'attachment; filename=WINCHILL.CSV' ).
DATA l_len TYPE i.
l_len = XSTRLEN( l_xstring ).
response->set_data( data = l_xstring
length = l_len ).
* application.
* navigation->response_complete( ).
ENDIF.
* reponse->
endmethod.
要点:
1.字符的操作,这个例子中主要是回车符和换行符.
2.BSP的系统类的功能.主要是Respons.
3.致命设置.页面(或者.do控制类)的Browser Cache 改为1,默认值是0.
否则直接在IE中输入 URL 就会提示和服务器的链接出错,或者Excel格式不正确.你的工作将功亏一篑.
阅读(2265) | 评论(1) | 转发(0) |