T-CODE: SMW0
在这里只介绍二进制数据,HTML模板的上传也一样。
另外也可以用CBO TABLE管理文件 可以看我另一个博文:CBO TABLE管理文件上传下载
选择 二进制
写包名:
进入后点击新建
输入对象名称和描述之后 按输入按钮
选择文件
上传完毕。
如果提示以下信息。
可以维护MIME类型。
在菜单中:
点击新建按钮。
保存即可。
下载:
DATA: LS_WWWDATA_ITEM LIKE WWWDATATAB, "
LV_FILE TYPE RLGRAP-FILENAME ."
LV_FILE = 'C:\a.doc'.
SELECT SINGLE *
INTO CORRESPONDING FIELDS OF LS_WWWDATA_ITEM
FROM WWWDATA WHERE OBJID = 'ZARA002' .
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' "
EXPORTING
KEY = LS_WWWDATA_ITEM
DESTINATION = LV_FILE.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_EXPORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_data_export.
DATA: l_objid TYPE w3objid VALUE 'ZAMRP016',
l_ftemp TYPE string,
l_fpath TYPE string VALUE '固定资产报废申请表'.
CHECK NOT gt_data IS INITIAL.
DESCRIBE TABLE gt_data LINES g_row_count. " 条目数
* Excel 输出准备
PERFORM frm_excel_prepare USING l_objid l_fpath l_ftemp.
* 填充汇总表
PERFORM frm_sheet_name USING space 1." 激活第一个SHEET
* 合计行
PERFORM frm_write_cell USING 07 'I' g_orivz.
PERFORM frm_write_cell USING 07 'J' g_netvz.
PERFORM frm_write_cell USING 07 'K' g_sumvz.
g_row_index = 8. " 从第九行开始写数据(根据模板设计)
LOOP AT gt_data INTO gs_data.
g_row_serno = g_row_serno + 1.
g_row_index = g_row_index + 1.
PERFORM frm_write_cell USING g_row_index 'A' g_row_serno.
PERFORM frm_write_cell USING g_row_index 'B' gs_data-anln1.
PERFORM frm_write_cell USING g_row_index 'C' gs_data-txt50.
PERFORM frm_write_cell USING g_row_index 'D' gs_data-mspec.
PERFORM frm_write_cell USING g_row_index 'E' gs_data-herst.
PERFORM frm_write_cell USING g_row_index 'F' gs_data-ansdt.
PERFORM frm_write_cell USING g_row_index 'G' gs_data-ndjar.
PERFORM frm_write_cell USING g_row_index 'H' gs_data-years.
PERFORM frm_write_cell USING g_row_index 'I' gs_data-orivz.
PERFORM frm_write_cell USING g_row_index 'J' gs_data-netvz.
PERFORM frm_write_cell USING g_row_index 'K' gs_data-sumvz.
ENDLOOP.
g_sheet_num = 1. " 从第二个SHEET开始填充(根据模板设计)
LOOP AT gt_data INTO gs_data.
g_sheet_num = g_sheet_num + 1.
CONCATENATE gs_data-bukrs '-' gs_data-anln1 INTO g_sheet_nam.
PERFORM frm_sheet_name USING g_sheet_nam g_sheet_num.
PERFORM frm_write_cell USING 05 'A' gs_data-anln1.
PERFORM frm_write_cell USING 05 'B' gs_data-txt50.
PERFORM frm_write_cell USING 05 'D' gs_data-mspec.
PERFORM frm_write_cell USING 05 'E' gs_data-herst.
PERFORM frm_write_cell USING 07 'A' gs_data-orivz.
PERFORM frm_write_cell USING 07 'B' gs_data-netvz.
PERFORM frm_write_cell USING 07 'B' gs_data-inbdt.
PERFORM frm_write_cell USING 07 'D' gs_data-costl.
PERFORM frm_write_cell USING 07 'E' gs_data-costv.
ENDLOOP.
* Excel output
PERFORM frm_excel_output USING l_fpath.
ENDFORM. " FRM_DATA_EXPORT
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_PREPARE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_excel_prepare USING f_objid f_fpath f_ftemp.
* 取得模板文件和文件夹路径
CONCATENATE f_fpath '-模板' INTO f_ftemp.
PERFORM frm_excel_template USING f_objid f_ftemp.
PERFORM frm_excel_create_obj.
* 以模板生成文件
PERFORM frm_excel_open USING f_ftemp 0.
* 为每个公司代码生成一个sheet
g_sheet_num = g_sheet_num - 1.
IF g_sheet_num > 0.
PERFORM frm_sheet_create USING g_sheet_num.
ENDIF.
ENDFORM. " FRM_EXCEL_PREPARE
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_TEMPLATE
*&---------------------------------------------------------------------*
* text EXCEL模板下载
*----------------------------------------------------------------------*
FORM frm_excel_template USING f_objid f_fname.
DATA: ls_objs TYPE wwwdatatab.
DATA: l_destz TYPE rlgrap-filename,
l_rcode TYPE sy-subrc,
l_ltext TYPE string.
SELECT SINGLE relid objid FROM wwwdata
INTO CORRESPONDING FIELDS OF ls_objs
WHERE relid = 'MI'
AND objid = f_objid " EXCEL模板名称
AND srtf2 = 0.
* 检查表wwwdata中是否存在所指定的模板文件
IF sy-subrc NE 0 OR ls_objs-objid EQ space."如果不存在,则给出错误提示
CONCATENATE '模板文件' '不存在' INTO l_ltext.
MESSAGE l_ltext TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
CONCATENATE 'C:\TEMP\' f_fname '.xls' INTO l_destz.
* 如果存在下载模板到上述路径下
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_objs
destination = l_destz
IMPORTING
rc = l_rcode.
IF l_rcode NE 0.
CONCATENATE '模板文件:' l_destz '下载失败' INTO l_ltext.
MESSAGE l_ltext TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
f_fname = l_destz.
ENDFORM. "FRM_EXCEL_TEMPLATE
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_CREATE_OBJ
*&---------------------------------------------------------------------*
* text 创建EXCEL对象
*----------------------------------------------------------------------*
FORM frm_excel_create_obj.
CREATE OBJECT g_excel_obj 'Excel.Application'.
IF sy-subrc NE 0.
MESSAGE s796(f9) DISPLAY LIKE 'E' WITH '创建Excel对象失败!'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " FRM_EXCEL_CREATE_OBJ
*&---------------------------------------------------------------------*
*& Form FRM_SHEET_NAME
*&---------------------------------------------------------------------*
* text 修改/激活SHEET
*----------------------------------------------------------------------*
FORM frm_sheet_name USING f_sheet_nam f_sheet_num.
CALL METHOD OF g_wbook_obj 'Sheets' = g_sheet_obj
EXPORTING #1 = f_sheet_num.
IF f_sheet_nam NE space.
SET PROPERTY OF g_sheet_obj 'Name' = f_sheet_nam.
ENDIF.
CALL METHOD OF g_sheet_obj 'Activate' .
ENDFORM. " FRM_SHEET_NAME
*&---------------------------------------------------------------------*
*& Form FRM_WRITE_CELL
*&---------------------------------------------------------------------*
* text 填充单元格
*----------------------------------------------------------------------*
FORM frm_write_cell USING f_rowid f_colid f_value.
DATA: lo_cell TYPE ole2_object.
CHECK NOT f_value IS INITIAL.
CALL METHOD OF g_excel_obj 'Cells' = lo_cell
EXPORTING
#1 = f_rowid
#2 = f_colid.
SET PROPERTY OF lo_cell 'Value' = f_value.
ENDFORM. "FRM_WRITE_CELL
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_excel_output USING f_fpath.
* 文件路径设置
PERFORM frm_excel_path USING f_fpath.
* 文件另存为
PERFORM frm_excel_save USING f_fpath.
* 打开此文件
PERFORM frm_excel_open USING f_fpath 1.
ENDFORM. " FRM_EXCEL_OUTPUT
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_PATH
*&---------------------------------------------------------------------*
* text 保存路径
*----------------------------------------------------------------------*
FORM frm_excel_path USING f_fpath.
DATA: l_xpath TYPE string,
l_fpath TYPE string,
l_flnam TYPE string.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = '请选择文件路径'
default_extension = 'XLS'
default_file_name = f_fpath
initial_directory = 'C:\TEMP\DATA\'
CHANGING
filename = l_flnam
path = l_xpath
fullpath = l_fpath.
f_fpath = l_fpath.
ENDFORM. " FRM_EXCEL_PATH
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_SAVE
*&---------------------------------------------------------------------*
* text 文件另存为
*----------------------------------------------------------------------*
FORM frm_excel_save USING f_file_name.
SET PROPERTY OF g_excel_obj 'DisplayAlerts' = 0.
CALL METHOD OF g_wbook_obj 'SAVEAS' "另存
EXPORTING
#1 = f_file_name
#2 = 1.
ENDFORM. "FRM_EXCEL_SAVE
*&---------------------------------------------------------------------*
*& Form FRM_EXCEL_OPEN
*&---------------------------------------------------------------------*
* text 打开文件
*----------------------------------------------------------------------*
FORM frm_excel_open USING f_file_name f_propertys.
CALL METHOD OF g_excel_obj 'Workbooks' = g_wbook_obj.
CALL METHOD OF g_wbook_obj 'Open' = g_wbook_obj
EXPORTING #1 = f_file_name.
SET PROPERTY OF g_excel_obj 'Visible' = f_propertys.
CALL METHOD OF g_wbook_obj 'Sheets' = g_sheet_obj
EXPORTING #1 = 1.
ENDFORM. " FRM_EXCEL_OPEN