标签:
用OLE 下载 SWM0里面的模板到本地,并进行数据填充 。这里好像上传不了模板 直接上代码了。
REPORT ZJ_TE1. INCLUDE OLE2INCL. " 时间: 2015/01/05 BY:CHARLES " 描述:通过OLE下载服务器文件模板到本地 DATA:GC_FIEL TYPE STRING. "下载路径 DATA GC_NAME TYPE STRING. " 文件名 DATA: EXCEL TYPE OLE2_OBJECT, WORKBOOK TYPE OLE2_OBJECT, SHEET TYPE OLE2_OBJECT, CELL TYPE OLE2_OBJECT, * cell1 type ole2_object, COLUMN TYPE OLE2_OBJECT, RANGE TYPE OLE2_OBJECT, BORDERS TYPE OLE2_OBJECT, BUTTON TYPE OLE2_OBJECT, INT TYPE OLE2_OBJECT, FONT TYPE OLE2_OBJECT, ROW TYPE OLE2_OBJECT. DATA: APPLICATION TYPE OLE2_OBJECT, BOOK TYPE OLE2_OBJECT, BOOKS TYPE OLE2_OBJECT, OLE_BOOK TYPE OLE2_OBJECT. START-OF-SELECTION. PERFORM GET_FIEL USING GC_FIEL. "选择路径 PERFORM GET_MODULE USING ‘TEST.XLS‘ GC_FIEL. "下载模板 PERFORM FRM_PROCESS_INDCATOR USING ‘程序正在打开excel文档‘ 0 . "左下角显示进度条 PERFORM FRM_CREATE_EXCEL USING GC_FIEL. " 打开已下载的模板 PERFORM FRM_FILL_SHEET. "进行数据填充 PERFORM FRM_FREE_OBJECT. *&---------------------------------------------------------------------* *& Form get_fiel *&---------------------------------------------------------------------* * text 下载路径 *----------------------------------------------------------------------* FORM GET_FIEL USING GC_FIEL. GC_NAME = ‘D_NAME‘. " 下载后文件名 CALL FUNCTION ‘WS_FILENAME_GET‘ EXPORTING DEF_FILENAME = GC_NAME MASK = ‘,*.XLS,*.XLS,*.XLT,*.XLT,所有文件,*.*. ‘ MODE = ‘O‘ TITLE = ‘选择输出文件‘ IMPORTING FILENAME = GC_FIEL EXCEPTIONS INV_WINSYS = 1 NO_BATCH = 2 SELECTION_CANCEL = 3 SELECTION_ERROR = 4 OTHERS = 5. CASE SY-SUBRC. WHEN 0. WHEN OTHERS. EXIT. ENDCASE. ENDFORM. "get_fiel *&---------------------------------------------------------------------* *& Form get_module *&---------------------------------------------------------------------* * text 获取下载模板 *----------------------------------------------------------------------* FORM GET_MODULE USING L_NAME L_FILE. DATA: LO_OBJDATA LIKE WWWDATATAB, LS_OBJNAM TYPE STRING, "模板文件名 LS_DESTINATION LIKE RLGRAP-FILENAME. " DATA:LI_RC LIKE SY-SUBRC. CONCATENATE L_NAME ‘.XLS‘ INTO LS_OBJNAM. CONDENSE LS_OBJNAM NO-GAPS. "查询数据库是否存在 SELECT SINGLE RELID OBJID FROM WWWDATA INTO CORRESPONDING FIELDS OF LO_OBJDATA WHERE SRTF2 = 0 AND RELID = ‘MI‘ AND OBJID = L_NAME. IF SY-SUBRC NE 0 OR LO_OBJDATA-OBJID EQ SPACE. MESSAGE E600(ZDEV) WITH ‘模板文件不存在请用SMW0进行加载‘. ENDIF. "模板下载 LS_DESTINATION = L_FILE. CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT‘ EXPORTING KEY = LO_OBJDATA DESTINATION = LS_DESTINATION IMPORTING RC = LI_RC. IF LI_RC NE 0 . MESSAGE E600(ZDEV) WITH ‘模板下载失败‘. ENDIF. ENDFORM. "get_module *&---------------------------------------------------------------------* *& Form FRM_PROCESS_INDCATOR *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_TEXT text * -->P_PERCENTAGE text *----------------------------------------------------------------------* FORM FRM_PROCESS_INDCATOR USING P_TEXT P_PERCENTAGE. CALL FUNCTION ‘SAPGUI_PROGRESS_INDICATOR‘ EXPORTING PERCENTAGE = P_PERCENTAGE TEXT = P_TEXT. ENDFORM. "FRM_PROCESS_INDCATOR *&---------------------------------------------------------------------* *& Form create_excel *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM FRM_CREATE_EXCEL USING P_FILE. CREATE OBJECT EXCEL ‘EXCEL.APPLICATION‘. IF SY-SUBRC NE 0. EXIT. ELSE. SET PROPERTY OF EXCEL ‘VISIBLE‘ = 1. CALL METHOD OF EXCEL " Create wbook object ‘Workbooks‘ = WORKBOOK. " 打开下载的模板 CALL METHOD OF WORKBOOK ‘Open‘ = WORKBOOK EXPORTING #1 = P_FILE. ENDIF. ENDFORM. "create_excel *&---------------------------------------------------------------------* *& Form FRM_FILL_SHEET *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM FRM_FILL_SHEET. CALL METHOD OF EXCEL ‘WORKSHEETS‘ = SHEET EXPORTING #1 = 1. "打开第一个SHEET CALL METHOD OF "显示当前选择的SHEET SHEET ‘ACTIVATE‘. " 抬头信息 " perform FRM_LINE_RANGE using 1 7 ‘订单号:41088888‘. “按行 列进行填充 PERFORM FRM_VALUE_RANGE USING ‘P‘ 1 ‘Z‘ 1 ‘订单号:41088888‘." 按照范围进行填充 "----------行项目信息 " 插入空行 DO 3 TIMES. PERFORM FRM_INSERT_ROW USING 14 SHEET CHANGING RANGE. "14 为被COPY的行 ENDDO. " 赋值 PERFORM FRM_VALUE_RANGE USING ‘A‘14 ‘B‘ 14 ‘TEST1‘. PERFORM FRM_VALUE_RANGE USING ‘A‘ 15 ‘B‘ 15 ‘TEST2‘. " 资源释放 FREE OBJECT:BUTTON, FONT, INT, CELL, RANGE, BORDERS, COLUMN, ROW,SHEET. ENDFORM. "FRM_FILL_SHEET *&---------------------------------------------------------------------* *& Form FRM_VALUE_RANGE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM FRM_LINE_RANGE USING P_LINE P_CELL P_VALUE. CALL METHOD OF EXCEL ‘Cells‘ = CELL EXPORTING #1 = P_LINE " 行 #2 = P_CELL. " SET PROPERTY OF CELL ‘Value‘ = P_VALUE. ENDFORM. "FRM_VALUE_RANGE *&---------------------------------------------------------------------* *& Form FRM_VALUE_RANGE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_0337 text * -->P_1 text * -->P_0339 text * -->P_2 text * -->P_LV_VALUE text *----------------------------------------------------------------------* FORM FRM_VALUE_RANGE USING P_LEFT P_TOP P_RIGHT P_BOTTOM P_VALUE. "data lv_area1 type char3. "data lv_area2 type char3. "data lv_char2 type char2. DATA LV_AREA1 TYPE CHAR4. DATA LV_AREA2 TYPE CHAR4. DATA LV_CHAR2 TYPE CHAR3. LV_CHAR2 = P_TOP. CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘ "补零 EXPORTING INPUT = LV_CHAR2 IMPORTING OUTPUT = LV_CHAR2. CLEAR:LV_AREA1,LV_AREA2. CONCATENATE P_LEFT LV_CHAR2 INTO LV_AREA1. LV_CHAR2 = P_BOTTOM. CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘ EXPORTING INPUT = LV_CHAR2 IMPORTING OUTPUT = LV_CHAR2. CONCATENATE P_RIGHT LV_CHAR2 INTO LV_AREA2. " 设定操作范围 CALL METHOD OF EXCEL ‘RANGE‘ = RANGE EXPORTING #1 = LV_AREA1 #2 = LV_AREA2. SET PROPERTY OF RANGE ‘VALUE‘ = P_VALUE. " PERFORM FRM_SET_FONT USING 1 ‘11‘ ‘1‘ CHANGING RANGE. ENDFORM. " FRM_VALUE_RANGE *&---------------------------------------------------------------------* *& Form FRM_INSERT_ROW *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_LV_ROW text * <--P_RANGE text *----------------------------------------------------------------------* FORM FRM_INSERT_ROW USING P_ROW P_SHEET CHANGING P_RANGE. CALL METHOD OF P_SHEET ‘Rows‘ = P_RANGE EXPORTING #1 = P_ROW. CALL METHOD OF "COPY 当前行 P_RANGE ‘Copy‘. CALL METHOD OF P_SHEET ‘Rows‘ = P_RANGE EXPORTING #1 = P_ROW. CALL METHOD OF "在当前行插入 P_RANGE ‘Insert‘. CALL METHOD OF P_RANGE ‘ClearContents‘. "是否需要清空Cell ENDFORM. " FRM_INSERT_ROW *&---------------------------------------------------------------------* *& Form FRM_FREE_OBJECT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM FRM_FREE_OBJECT . FREE OBJECT SHEET. FREE OBJECT WORKBOOK. FREE OBJECT EXCEL. ENDFORM. " FRM_FREE_OBJECT *&---------------------------------------------------------------------* *& Form FRM_SET_FONT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_BOLD text * -->P_SIZE text * -->P_ULINE text * -->P_CELL text * 设置字体是否加粗显示 *----------------------------------------------------------------------* FORM FRM_SET_FONT USING P_BOLD P_SIZE P_ULINE CHANGING P_CELL . DATA LR_FONT TYPE OLE2_OBJECT. CALL METHOD OF P_CELL ‘FONT‘ = LR_FONT. SET PROPERTY OF LR_FONT ‘BOLD‘ = P_BOLD. SET PROPERTY OF LR_FONT ‘SIZE‘ = P_SIZE. SET PROPERTY OF LR_FONT ‘UNDERLINE‘ = P_ULINE. FREE OBJECT LR_FONT. ENDFORM. "font *
标签:
原文地址:http://www.cnblogs.com/charles-guo/p/4203086.html