EXCEL导入与下载

目录结构

[TOC]

 

EXCEL导入与下载

EXCEL上载与下载是SAP开发中经常会用到的技术,本文在此总结一下使用场景以及命令。

EXCEL导入

一般应用于数据的批量导入。用户把数据写入到EXCEL中,通过导入写入数据库或者实现某些功能 比如物料批次的创建。

用户选择EXCEL文件目录

"定义选择屏幕
PARAMETERS  p_file   TYPE localfile. 

"选择屏幕控制
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. "选择屏幕搜索帮助
	PERFORM frm_f4_file.  "弹出文件选择框
	
FORM frm_f4_file .
  DATA: l_filename LIKE rlgrap-filename.
"调用函数"
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = ',EXCEL FILE,*.XLS;*.XLSX;'
      mode             = 'O'        "S为保存,O为打开
    IMPORTING
      filename         = l_filename
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
  IF sy-subrc = 0.
    p_file = l_filename.
  ENDIF.
ENDFORM.

读取EXCEL数据到内表

用系统自带的函数 ALSM_EXCEL_TO_INTERNAL_TABLE 有限制。限制原因以及如果修改限制点击这里

这里用ZALSM_EXCEL_TO_INTERNAL_TABLE。

*"定义接收EXCEL数据的类型  ly_excel 实际项目中可以根据业务需求来设置"
TYPES : BEGIN OF  ly_excel ,
          col1 TYPE  string,
          col2 TYPE  string,
          col3 TYPE  string,
        END OF ly_excel .
*"定义带表头的内表"
DATA : lt_excel TYPE TABLE OF ly_excel WITH HEADER LINE .

FIELD-SYMBOLS: <fs>.   "创建指针字段"
IF itab[] IS NOT INITIAL. "如果读取的excel不为空
"itab的结构是  row col value 
"			 0001 0001  第一行第一列 
"			 0001 0002  第一行第二列
  LOOP AT itab.
    ON CHANGE OF itab-row.   "行号发生了改变
      IF sy-tabix NE 1.      "而且是第一列 比如跳转到了第二行row=2 col=1.
        APPEND lt_excel.     "添加到内表
        CLEAR  lt_excel.	 "清空工作区
      ENDIF.
    ENDON.
    "itab-col列号 
    ASSIGN COMPONENT itab-col OF STRUCTURE lt_excel TO <fs>.
    <fs> = itab-value.
  ENDLOOP.
  APPEND lt_excel.
  CLEAR  lt_excel.
ENDIF.

EXCEL下载

EXCEL下载的方式有2中:

  1. 先在SW01中上传EXCEL模板
  2. 调用OLE实现写入excel

第一种:先SW01上传模板 然后下载

  • SMW0 上传模板

  • 代码
    REPORT YEXCELTEST11.
    
    "表
    TABLES:SSCRFIELDS.
    
    "定义选择屏幕
    PARAMETERS: p_file   TYPE localfile.
    SELECTION-SCREEN FUNCTION KEY 1 .
    
    "初始化处理
    INITIALIZATION .
    SSCRFIELDS-FUNCTXT_01 = '@49@下载导入模板' .
    
    
    AT SELECTION-SCREEN.
      "用户操作判断
      IF sscrfields-ucomm = 'FC01'.
          PERFORM  frm_down_template .
      ENDIF.
    *&---------------------------------------------------------------------*
    *& Form frm_down_template
    *&---------------------------------------------------------------------*
    *& 模板下载
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM frm_down_template .
    
    data : ls_wwwdata_item like wwwdatatab,
           lv_file          type rlgrap-filename.
    
    "获取用户桌面路径
      "定义桌面路径
      DATA l_desktopdirectory TYPE string.
      CALL METHOD cl_gui_frontend_services=>get_desktop_directory
        CHANGING
          desktop_directory = l_desktopdirectory.
      CALL METHOD cl_gui_cfw=>update_view.
      "获取函数导入参数  ls_wwwdata_item
       SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_wwwdata_item
        FROM wwwdata
      WHERE objid = 'SAPBOY' . "这里填写导入的模板名称
    
       "这里可以再此让用户选择保存的路径  我这里直接保存到桌面
         CONCATENATE l_desktopdirectory '\' '模板下载测试SAPBOY' '.XLSX' INTO lv_file.
    
         CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
           EXPORTING
             key               = ls_wwwdata_item
             DESTINATION       = lv_file
                   .
    ENDFORM.
    

    第二种:调用OLE实现模板下载

  • 留坑,以后再补上了。 下面是之前写的OLE调用的
TYPE-POOLS:ole2.
TABLES : sscrfields .
INCLUDE:<icon>.
*--->数据定义
"EXCEL操作相关数据定义
DATA:
  v_excel     TYPE ole2_object, "EXCLE
  v_sheet     TYPE ole2_object, "工作页签
  v_sheet1    TYPE ole2_object, "工作页签
  v_sheet2    TYPE ole2_object, "工作页签
  v_workbooks TYPE ole2_object, "工作区
  v_cell      TYPE ole2_object, "单元格属性
  v_font      TYPE ole2_object, "单元格属性样式
  v_range     TYPE ole2_object, "选择的单元格范围
  v_color     TYPE ole2_object, "单元格颜色
  v_column    TYPE ole2_object, "列宽
  v_border    TYPE ole2_object, "单元格边框
  v_interior  TYPE ole2_object, "单元格内部属性--用于颜色
  v_type      TYPE ole2_object,
  v_pics      TYPE ole2_object,
  v_pic       TYPE ole2_object
  .
DATA  filepath TYPE string .

*--------------->子例程说明
DATA : gv_filepath  TYPE string .
"EXCEL的打开创建

SELECTION-SCREEN  BEGIN OF  BLOCK  b1 WITH FRAME  TITLE TEXT-001.
  SELECTION-SCREEN SKIP .

  SELECTION-SCREEN :
  PUSHBUTTON 01(10) but0 USER-COMMAND  e_open ,    "打开EXCEL按钮
  PUSHBUTTON 15(10) but1 USER-COMMAND  e_save ,   "保存EXCEL按钮
  PUSHBUTTON 30(10) but2 USER-COMMAND  e_new .  "创建一个新的EXCEL

  SELECTION-SCREEN SKIP .

  SELECTION-SCREEN BEGIN OF LINE .
    SELECTION-SCREEN   PUSHBUTTON 01(20) but3 USER-COMMAND  e_file .  "设置EXCEL路径   调用函数获取全路径
    PARAMETERS   p_file  TYPE  string VISIBLE LENGTH 30.            "设置文件路径输入框
  SELECTION-SCREEN END OF LINE .

SELECTION-SCREEN END OF BLOCK  b1 .

"获取单元格信息
SELECTION-SCREEN  BEGIN OF  BLOCK  b2 WITH FRAME  TITLE TEXT-002.
  SELECTION-SCREEN BEGIN OF LINE .

    SELECTION-SCREEN   PUSHBUTTON 01(15) but4 USER-COMMAND  gsheet .  "获取SHEET
    PARAMETERS   p_sheet1  TYPE  string DEFAULT  1 VISIBLE LENGTH 15.            "条件输入 位置 或者 SHEET名字

    SELECTION-SCREEN   PUSHBUTTON 35(15) but5 USER-COMMAND  grange .  "获取RANGE
    PARAMETERS   p_range1  TYPE  string DEFAULT 'A1' VISIBLE LENGTH 7.           "单元格第一个
    PARAMETERS   p_range2  TYPE  string DEFAULT 'E1' VISIBLE LENGTH 7.            "单元格最后一个

    SELECTION-SCREEN   PUSHBUTTON 70(15) but6 USER-COMMAND  gcell .  "获取CELL   定义参数要为I类型
    PARAMETERS   p_cell1  TYPE  i DEFAULT '2' VISIBLE LENGTH 7.            "行
    PARAMETERS   p_cell2  TYPE  i DEFAULT '2' VISIBLE LENGTH 7.            "列

  SELECTION-SCREEN END OF LINE .
SELECTION-SCREEN END OF BLOCK  b2 .

"单元格设置 P_TYPE P_BOLD P_SIZE P_COLOR P_UNDERLINE P_ITALIC
SELECTION-SCREEN  BEGIN OF  BLOCK  b3 WITH FRAME  TITLE TEXT-003.
  SELECTION-SCREEN BEGIN OF LINE .
    SELECTION-SCREEN   PUSHBUTTON 01(15) but7 USER-COMMAND  setcell .  "设置单元格
  SELECTION-SCREEN END OF LINE .
  SELECTION-SCREEN BEGIN OF LINE .

  SELECTION-SCREEN END OF LINE .
  SELECTION-SCREEN BEGIN OF LINE .
    SELECTION-SCREEN   COMMENT 01(10)   text1  .  "RANGE内容
    PARAMETERS   p_fill  TYPE  string DEFAULT '单元格内容' VISIBLE LENGTH 10 .

    SELECTION-SCREEN   COMMENT 30(5) text2. "字体粗细
    PARAMETERS   p_bold  TYPE  char5  DEFAULT  1 VISIBLE LENGTH 10.

    SELECTION-SCREEN   COMMENT 50(5) text3. "大小
    PARAMETERS   p_size  TYPE  string DEFAULT '10' VISIBLE LENGTH 10 OBLIGATORY .
  SELECTION-SCREEN END OF LINE .

  SELECTION-SCREEN BEGIN OF LINE .
    SELECTION-SCREEN   COMMENT 01(10)  text4.  "颜色
    PARAMETERS   p_color  TYPE  string DEFAULT '3' VISIBLE LENGTH 10.

    SELECTION-SCREEN   COMMENT 30(5)  text5.  "下划线
    PARAMETERS   p_line  TYPE  string DEFAULT '1' VISIBLE LENGTH 10.

    SELECTION-SCREEN   COMMENT 50(5)  text6.  "斜体
    PARAMETERS   p_italic    TYPE  string DEFAULT '1' VISIBLE LENGTH 10.


  SELECTION-SCREEN END OF LINE .


SELECTION-SCREEN END OF BLOCK  b3 .



INITIALIZATION. "屏幕开始前

*&定义按钮图标信息宏 没有info信息
  DEFINE  setput.   "&1是ICON图标   &2是按钮   &3是要添加的描述
    WRITE &1 AS ICON TO &2.
    CONCATENATE &2  &3 INTO &2 SEPARATED BY space.
  END-OF-DEFINITION.
PERFORM frm_dilog_text_set .






AT SELECTION-SCREEN.
  "  AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_bold .
  "按钮事件
  CASE  sscrfields-ucomm .
    WHEN 'E_OPEN'  .                                        "打开excel
  PERFORM FRM_EXCEL_open USING p_file .
    WHEN 'E_SAVE' .                                        "保存excel
      PERFORM frm_excel_save USING  v_sheet p_file  .
    WHEN 'E_NEW' .                                        "创建一个新的excel
      PERFORM frm_excel_creat .
    WHEN 'E_FILE' .                                        "设置excel路径
      PERFORM frm_get_filepath USING p_file  .
    WHEN 'GSHEET' .                                        "获取sheet位置
      PERFORM frm_excel_sheet_get USING p_sheet1  .
    WHEN 'GRANGE' .                                        "获取range位置
      PERFORM frm_excel_range_get USING p_range1 p_range2 .
    WHEN 'GCELL' .                                        "获取cell位置
      PERFORM frm_excel_cell_get USING p_cell1 p_cell2 .
      IF  sy-subrc EQ 0 .
        MESSAGE '获取cell位置成功' TYPE 'S' .
      ELSE .
        MESSAGE '获取cell位置失败' TYPE 'W' .

      ENDIF .
    WHEN 'SETCELL' .
      PERFORM frm_excel_cell_fill USING v_cell  p_fill . "设置单元格内容
      PERFORM frm_excel_cell_font USING v_cell   p_bold p_size p_color p_line p_italic .
  ENDCASE.

  "PERFORM  frm_but_help .










*&---------------------------------------------------------------------*
*& FORM FRM_CREAT_EXCEL
*&---------------------------------------------------------------------*
*& 创建EXCEL设置为前台
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
FORM frm_excel_creat .

  "创建一个EXCEL对象
  CREATE OBJECT v_excel 'EXCEL.APPLICATION'.

  "1前台  0 后台  设置EXCEL前台可见
  SET PROPERTY OF v_excel 'VISIBLE' = 1.

  "在WORKBOOK中添加一个WORKSHEET
  CALL METHOD OF v_excel  'WORKBOOKS' = v_workbooks.

  "新建一个EXCEL
  CALL METHOD OF v_workbooks 'ADD'.

ENDFORM.
*------------------------针对单元格的操作-------------------------------------*
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_CELL_GET
*&---------------------------------------------------------------------*
*& 获取单元格位置  CELL
*&---------------------------------------------------------------------*
*& 参数 行 列
*&---------------------------------------------------------------------*
FORM frm_excel_cell_get   USING p_row p_col  .
  CALL METHOD OF v_excel 'CELLS' = v_cell "单元格位置
  EXPORTING
  #1 = p_row   "行
  #2 = p_col . "列
ENDFORM.

*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_RANGE_GET
*&---------------------------------------------------------------------*
*& 获取单元格范围位置  RANGE
*&---------------------------------------------------------------------*
*& 参数 首个单元格  最后一个单元格
*&---------------------------------------------------------------------*
FORM frm_excel_range_get USING  p_start_cell  p_end_cell .
  CALL METHOD OF  v_excel
'RANGE' = v_range
EXPORTING             "输入参数
  #1 = p_start_cell   "起始单元格
  #2 = p_end_cell     "最后单元格
  .
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_CELL_FILL
*&---------------------------------------------------------------------*
*& 对单元格内容设置
*&---------------------------------------------------------------------*
*& 参数 TYPE=RANGE / CELL 内容
*&---------------------------------------------------------------------*
FORM frm_excel_cell_fill  USING  p_type  p_data.
  SET PROPERTY OF p_type 'VALUE' = p_data. "单元格内容
ENDFORM .
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_CELL_FONT
*&---------------------------------------------------------------------*
*& 对单元格样式设置
*&---------------------------------------------------------------------*
*& 参数 TYPE=RANGE / CELL 内容
*&---------------------------------------------------------------------*
FORM frm_excel_cell_font USING p_type p_bold p_size p_color p_underline p_italic .
  "设置单元格字体样式  FONT = V_FONT
  CALL METHOD OF p_type 'FONT' = v_font. "字体方法

  "针对FONT的粗细  大小做设置
  SET PROPERTY OF v_font 'BOLD' = p_bold. "粗体   0不粗 1粗

  SET PROPERTY OF v_font 'SIZE' = p_size. "字体大小

  SET PROPERTY OF v_font 'COLOR' = p_color .  "颜色

  SET PROPERTY OF v_font 'UNDERLINE' = p_underline.   "2下划线   1没有下划线

  SET PROPERTY OF v_font 'ITALIC' = p_italic.   "倾斜   0不倾斜   1倾斜
ENDFORM.

*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_CELL_MEREGE
*&---------------------------------------------------------------------*
*& 合并单元格   针对RANGE操作
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
FORM frm_excel_cell_merege  .
  SET PROPERTY OF v_range  'MERGECELLS' = 1.  "1是合并  默认0 不合并。
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_CELL_COLOR
*&---------------------------------------------------------------------*
*& 背景颜色设置
*&---------------------------------------------------------------------*
*& 参数 TYPE = CELL / RANGE     COLOR
*&---------------------------------------------------------------------*
FORM frm_excel_cell_color USING p_type  p_color .
  CALL METHOD OF p_type
      'INTERIOR' = v_interior.
  SET PROPERTY OF v_interior 'COLORINDEX' = p_color .
*   BLACK 黑色  1
*   WHITE 白色  2
*   RED 红色  3
*   GREEN 绿色  4
*   BLUE  蓝色  5
*   YELLOW  黄色  6
*   MEGENTA	品红色	7
*   CYAN  青绿色 8
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_CELL_COLOR
*&---------------------------------------------------------------------*
*& 设置单元格对齐方式
*&---------------------------------------------------------------------*
*& 参数 TYPE = CELL / RANGE
*&  -4160  -4108    -4107     -4131   -4108     -4152 -4130     -4117
*&顶端对齐  垂直居中  底端对齐  左对齐 水平居中  右对齐 两端对齐  分散对齐
*&---------------------------------------------------------------------*
FORM frm_excel_center  USING p_type p_align.
  SET PROPERTY OF p_type 'HORIZONTALALIGNMENT' =   p_align  .

ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_BORDER
*&---------------------------------------------------------------------*
*& 边框设置
*&---------------------------------------------------------------------*
*& 参数 TYPE = CELL / RANGE    方向(1-左 2-右 3-上 4-下) 线条格式 线条粗细 线条颜色
*&---------------------------------------------------------------------*
FORM  frm_excel_border USING p_type p_orientation p_linestyle  p_weight  p_colorindex .
  CALL METHOD OF  p_type 'BORDERS' = v_border
     EXPORTING
       #1  = p_orientation.   "这里1-LEFT  2-RIGHT 3-TOP 4-BOTTOM

  SET PROPERTY OF v_border  'LINESTYLE'    =  p_linestyle.   "线条格式
  SET PROPERTY OF v_border  'WEIGHT'   =  p_weight .       "线条粗细
  SET PROPERTY OF v_border  'COLORINDEX'   =  p_colorindex.   "线条颜色
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_RANGE_COPY
*&---------------------------------------------------------------------*
*& 复制单元格/区域  到指定单元格位置
*&---------------------------------------------------------------------*
*& P_TYPE=CELL / RANGE (可以是单元格或者范围)  P_CELL指定的单元格位置
*&---------------------------------------------------------------------*
FORM  frm_excel_range_copy USING p_type p_cell .
  CALL METHOD OF p_type 'COPY '
    EXPORTING
      #1 = p_cell. "复制到的单元格
ENDFORM.
*--------------------------------------------------------------------------*
*--------------------------------------------------------------------------*
*--------------------------------------------------------------------------*

*--------------针对EXCEL的操作---------------------------------------------*
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_SHEET_GET
*&---------------------------------------------------------------------*
* 定位SHEET 或者 SHEETNAME
*----------------------------------------------------------------------*
*& 参数  SHEET 名字(字符串)  或者  位置(数字)
*&---------------------------------------------------------------------*
FORM frm_excel_sheet_get  USING p_sheetname  .
call METHOD  OF v_excel 'SHEETS' = v_sheet
  EXPORTING
    #1 = p_sheetname .
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_SHEET_SETNAME
*&---------------------------------------------------------------------*
* 设置SHEET名字
*----------------------------------------------------------------------*
*& 参数  SHEET    SHEETNAME
*&---------------------------------------------------------------------*
FORM frm_excel_sheet_setname  USING p_sheet p_sheetname .
  SET PROPERTY OF p_sheet 'NAME' =  p_sheetname .
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_SHEET_COPY
*&---------------------------------------------------------------------*
* COPY  复制SHEET
*----------------------------------------------------------------------*
*& 参数   SHEET1 复制在某个之前   "SHEET2 复制在某个之后 不会调用
*&---------------------------------------------------------------------*
FORM frm_excel_sheet_copy USING p_sheet p_sheet1 . " P_SHEET2.
  CALL METHOD OF p_sheet 'COPY'
    EXPORTING
      #1 = p_sheet1  "复制在SHEET1之前
    " #2 = P_SHEET2 "不知道如何跳过#1输入在什么之后的参数
    .
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_SHEET_MOVE
*&---------------------------------------------------------------------*
* MOVE  移动SHEET
*----------------------------------------------------------------------*
*& 参数   SHEET1 复制在某个之前   "SHEET2 复制在某个之后 不会调用
*&---------------------------------------------------------------------*
FORM frm_excel_sheet_move USING p_sheet p_sheet1 . " P_SHEET2.
  CALL METHOD OF p_sheet 'MOVE'
    EXPORTING
      #1 = p_sheet1  "移动在SHEET1之前
    " #2 = P_SHEET2 "不知道如何跳过#1输入在什么之后的参数
    .
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_SAVE
*&---------------------------------------------------------------------*
* SAVE  EXCEL 另存为
*----------------------------------------------------------------------*
*& 参数 P_SHEET      P_FILEPATH 保存路径
*&---------------------------------------------------------------------*
FORM frm_excel_save USING p_sheet p_filepath . " P_SHEET2.
  CALL METHOD OF p_sheet 'SAVEAS'
    EXPORTING
      #1 = p_filepath
      #2 = 1.
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_EXCEL_open
*&---------------------------------------------------------------------*
* SAVE  EXCEL 打开
*----------------------------------------------------------------------*
*& 参数 P_SHEET      P_FILEPATH 保存路径
*&---------------------------------------------------------------------*
FORM FRM_EXCEL_open USING  p_filepath . " P_SHEET2.
    "创建一个EXCEL对象
  CREATE OBJECT v_excel 'EXCEL.APPLICATION'.

  "1前台  0 后台  设置EXCEL前台可见
  SET PROPERTY OF v_excel 'VISIBLE' = 1.

  "在WORKBOOK中添加一个WORKSHEET
  CALL METHOD OF v_excel  'WORKBOOKS' = v_workbooks.

  CALL METHOD OF v_workbooks 'Open'
    EXPORTING
    #1 = p_filepath.
ENDFORM.
*&------------------------主程序-----------------------------------
*&---------------------------------------------------------------------*
*& Form frm_but_help
*&---------------------------------------------------------------------*
*& 字段的选择帮助自定义
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_but_help .
  DATA :BEGIN OF  ls_cell_font_bold ,  "字体粗细的自定义帮助
          v1 TYPE char5,
          v2 TYPE char5.
  DATA END OF ls_cell_font_bold .

  DATA lt_cell_font_bold  LIKE TABLE OF  ls_cell_font_bold .

  lt_cell_font_bold = VALUE #(
  ( v1 = '0'  v2 = '细体' )
  ( v1 = '1'  v2 = '粗体' )   ).

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield         = 'V1'       "内表字段
      dynpprog         = sy-repid   "程序
      dynpnr           = sy-dynnr   "返回屏幕
      dynprofield      = 'P_BOLD'     "选择屏幕按钮
      value_org        = 'S'
      callback_program = sy-repid
  "   callback_form    = 'CB_FORM'
    TABLES
      value_tab        = lt_cell_font_bold
"     return_tab       = return_tab
    EXCEPTIONS
      parameter_error  = 1
      no_values_found  = 2
      OTHERS           = 3.



ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_filepath
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_filepath  USING  p_filepath.
  "定义数据
  DATA : lv_filename TYPE string ,   "文件名字
         lv_path     TYPE string,      "路径 默认桌面路径
         lv_fullpath TYPE string .   "全路径

  "文件名字
  lv_filename = 'FAN_EXCEL_'  && sy-datum && sy-uzeit .

* 获取桌面路径
  CALL METHOD cl_gui_frontend_services=>get_desktop_directory
    CHANGING
      desktop_directory    = lv_path
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = '请选择excel路径'  "GUI标题
      default_extension = 'xls'          "文件默认拓展名  .xls
      default_file_name = lv_filename          "指定保存文件名
*     with_encoding     =
*     file_filter       = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL
      initial_directory = lv_path
*     prompt_on_overwrite       = 'X'
    CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath
*     user_action       =
*     file_encoding     =
*  EXCEPTIONS
*     cntl_error        = 1
*     error_no_gui      = 2
*     not_supported_by_gui      = 3
*     invalid_default_file_name = 4
*     others            = 5
    .


  IF sy-subrc <> 0.
    MESSAGE  '选择路径失败' TYPE 'I' .
  ENDIF.

  IF lv_fullpath IS NOT INITIAL . "判断用户是否选择了保存路径
    MESSAGE '选择路径成功' TYPE 'S' .
    p_filepath =  lv_fullpath  .
  ELSE.
    MESSAGE '选择路径失败' TYPE 'W' .
    EXIT .
  ENDIF.






ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_dilog_text_set
*&---------------------------------------------------------------------*
*& 按钮 图标 文本设置
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_dilog_text_set .
  "针对文本信息做修改
  text1 = '内容'.
  text2 = '粗体'.
  text3 = '大小'.
  text4 = '颜色'.
  text5 = '下划线'.
  text6 = '斜体'.
*&-----------第一种方法--定义按钮图标和信息 无INFO----------
  setput  icon_system_okay but0 '打开'  .
  setput  icon_system_save but1 '保存'  .
  setput  icon_create but2 '创建'  .
  setput  icon_system_settings but3 '设置EXCEL路径'  .
  setput  icon_generate but4 '获取SHEET'  .
  setput  icon_generate but5 '获取RANGE'  .
*&-----------第二种方法--定义按钮图标和信息 有详细信息--F:ICON_CREATE--------
*    setput  icon_generate but6 '获取CELL'  .
*  setput  icon_execute_object but7 '设置单元格'  .
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name                  = icon_generate  "图标名称
      text                  = '获取CELL'   "按钮名称
      info                  = 'get '       "按钮文本信息
"     ADD_STDINF            = 'X'
    IMPORTING
      result                = but6
    EXCEPTIONS
      icon_not_found        = 1
      outputfield_too_short = 2
      OTHERS                = 3.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name                  = icon_execute_object  "图标名称
      text                  = '设置单元格 '   "按钮名称
      info                  = 'set'       "按钮文本信息
*     ADD_STDINF            = 'X'
    IMPORTING
      result                = but7
    EXCEPTIONS
      icon_not_found        = 1
      outputfield_too_short = 2
      OTHERS                = 3.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.

 

版权声明:
作者:FAN
链接:https://sapboy.com/index.php/2022/06/29/excel%e5%af%bc%e5%85%a5%e4%b8%8e%e4%b8%8b%e8%bd%bd/
来源:SAPBoy
文章版权归作者所有,未经允许请勿转载。

THE END
分享
EXCEL导入与下载
目录结构 [TOC]   EXCEL导入与下载 EXCEL上载与下载是SAP开发中经常会用到的技术,本文在此总结一下使用场景以及命令。 EXCEL导入 一般应用于数据的批量……
<<上一篇
下一篇>>
文章目录
关闭
目 录