*Excel File Name Variables DATA: GV_TITLE TYPE STRING, GV_COL TYPE I, GV_ROW TYPE I.
TYPES: BEGIN OF TY_LONG, DATA TYPE C LENGTH 8192, END OF TY_LONG.
*Excel related variables OLE DATA: GT_DOW_DATA TYPE TABLE OF TY_LONG. DATA: GO_EXCEL TYPE OLE2_OBJECT, GO_BOOKS TYPE OLE2_OBJECT, GO_SHEET TYPE OLE2_OBJECT, GO_COLUMNS TYPE OLE2_OBJECT, GO_CELLINT TYPE OLE2_OBJECT, GO_RANGE TYPE OLE2_OBJECT, GO_BORDER TYPE OLE2_OBJECT. *Excel related variables DOI DATA: GO_CONTROL TYPE REF TO I_OI_CONTAINER_CONTROL, GO_DOCU_PROXY TYPE REF TO I_OI_DOCUMENT_PROXY, GO_DOISHEET TYPE REF TO I_OI_SPREADSHEET, GO_ERROR TYPE REF TO I_OI_ERROR.
2.获取下载文件的路径
(1)获取下载路径与检测文件存在与否模板
1 2 3 4 5 6 7 8 9 10 11
PERFORM GET_FILE_PATH CHANGING LV_FULLPATH.
FORM GET_FILE_PATH CHANGING VALUE(PV_FULLPATH) TYPE STRING. CLEAR: GV_MESSAGE. "Get download path PERFORM GET_DOWNLOAD_FILE CHANGING PV_FULLPATH. CHECK GV_MESSAGE IS INITIAL. "Check that download files exist,and if they do,delete PERFORM CHECK_FILE_EXIS USING PV_FULLPATH. ENDFORM.
FORM GET_DOWNLOAD_FILE CHANGING VALUE(PV_PATH) TYPE STRING. *---------------------------Variables----------------------------* CONSTANTS: LC__ TYPE C LENGTH 1 VALUE '_'. DATA: LV_FNAME TYPE STRING, LV_PATH TYPE STRING, LV_FILENAME TYPE STRING, LV_TYPENAME TYPE STRING VALUE 'xlsx', LV_TITLE TYPE STRING VALUE 'Excel Downolad'. *-----------------------------Logic------------------------------* "Set Download file Name CONCATENATE GV_TITLE SY-DATUM SY-UZEIT INTO LV_FILENAME SEPARATED BY LC__.
"Gets the current default path. CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_GET_CURRENT CHANGING CURRENT_DIRECTORY = PV_PATH.
FORM CHECK_FILE_EXIS USING VALUE(PV_PATH) TYPE STRING. *---------------------------Variables----------------------------* CONSTANTS: LC_I TYPE C LENGTH 1 VALUE 'I'. DATA: LV_RC TYPE I, LV_RES TYPE C. *-----------------------------Logic------------------------------* "Does the verification file exist? CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST EXPORTING FILE = PV_PATH RECEIVING RESULT = LV_RES EXCEPTIONS CNTL_ERROR = 1 ERROR_NO_GUI = 2 WRONG_PARAMETER = 3 NOT_SUPPORTED_BY_GUI = 4 OTHERS = 5.
CONSTANTS: LC_I TYPE C LENGTH 1 VALUE 'I', LC_MI TYPE C LENGTH 2 VALUE 'MI'. DATA: LV_FNAME TYPE RLGRAP-FILENAME, LV_KEY TYPE WWWDATATAB, LV_RC TYPE I, LV_OBJID TYPE WWWDATA-OBJID VALUE '上传时的名称'.
SELECT SINGLE RELID FROM WWWDATA INTO @DATA(LV_RELID) WHERE RELID = @LC_MI AND OBJID = @LV_OBJID AND SRTF2 = 0. IF SY-SUBRC <> 0. "Template file download faild. MESSAGE TEXT-E29 TYPE LC_I DISPLAY LIKE GC_E. EXIT. ENDIF.
LV_FNAME = PV_PATH. CONCATENATE LC_MI LV_OBJID INTO LV_KEY.
CALL METHOD OF GO_RANGE 'Select'. CALL METHOD OF GO_SHEET 'Paste'.
(4)设置单元格格式
①单元格公式
行合计:PV_VALUE = ‘=SUM(RC[1]:RC[7])’ 合计第一列到第七列的数据
列合计:PV_VALUE = ‘=SUM(R[1]:R[7]C)’ 合计第一行到第七行数据
②设置单个单元格数据
1 2 3 4 5 6
CALL METHOD OF GO_EXCEL 'Cells' = LO_CELL EXPORTING #1 = PV_ROW #2 = PV_COL. SET PROPERTY OF LO_CELL 'Value' = PV_VAL. FREE OBJECT LO_CELL.
③设置数据类型
‘@’:常规
‘#,##0’:金额
1
SET PROPERTY OF GO_RANGE ‘NumberFormat’ = '@'.
③对齐方式
2:左对齐
3:居中
4:右对齐
1
SET PROPERTY OF GO_RANGE 'HorizontalAlignment' = 2.
④颜色
色图编号图:
1 2
CALL METHOD OF GO_RANGE 'Interior' = GO_CELLINT. SET PROPERTY OF GO_CELLINT 'Colorindex' = PV_COLOR.
⑤边框
1 2 3
CALL METHOD OF GO_RANGE 'Borders' = GO_BORDER. "实例化边框对象 SET PROPERTY OF GO_BORDER 'Weight' = 2. "设置边框宽度 SET PORPERTY OF GO_BORDER 'Linestyle' = 1. "设置边框类型(1实线)
⑥自适应宽度
1 2
CALL METHOD OF GO_EXCEL ' Columns' = GO_COLUMNS. CALL METHOD OF GO_COLUMNS 'AutoFit'.
⑦执行宏
1 2 3
CALL METHOD OF GO_EXCEL 'RUN' EXPORTING #1 = 'ZMAC'.
FROM DOWNLOAD_EXCEL_OLE CHANGING VALUE(PV_FULLPATH) TYPE STRING. "Get Download data PERFORM GET_DOWNLOAD_DATA USING PV_FULLPATH. "Download EXCEL PERFORM DOWNLOAD_FILE USING PV_FULLPATH. "Store data stream and download data PERFORM DOWNLOAD_DATA USING PV_FULLPATH. ENDFORM.
FORM GET_DOWNLOAD_DATA USING VALUE(PV_FULLPATH). *---------------------------Variables----------------------------* CONSTANTS: LC_FU TYPE C LENGTH 1 VALUE '-', LC_D TYPE C LENGTH 1 VALUE 'D', LC_DIAN TYPE C LENGTH 1 VALUE ','. DATA: LV_INDEX TYPE I VALUE 1, LV_DATA TYPE STRING, LV_YEAR TYPE C LENGTH 4, LV_MONTH TYPE C LENGTH 2, LV_DAY TYPE C LENGTH 2, LV_LENGTH TYPE I, LS_DOW TYPE TY_LONG, LT_FIELDCATALOG LIKE GT_FIELDCATALOG, LS_FIELDCATALOG LIKE LINE OF GT_FIELDCATALOG, LT_TABLE LIKE GT_TABLE. FIELD-SYMBOLS:<FS_FIELDNAME>. *-----------------------------Logic------------------------------* REFRESH: GT_DOW_DATA. "Set the column name of Excel APPEND LINES OF GT_FIELDCATALOG TO LT_FIELDCATALOG. DELETE LT_FIELDCATALOG WHERE TECH = GC_X. LOOP AT LT_FIELDCATALOG INTO LS_FIELDCATALOG. IF SY-TABIX EQ 1. LS_DOW = LS_FIELDCATALOG-COLTEXT. ELSE. CONCATENATE LS_DOW LS_FIELDCATALOG-COLTEXT INTO LS_DOW SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB. ENDIF. CLEAR: LS_FIELDCATALOG. ENDLOOP. APPEND LS_DOW TO GT_DOW_DATA. "Fill in the data to be saved APPEND LINES OF GT_TABLE TO LT_TABLE. LOOP AT LT_TABLE INTO GS_TABLE. LOOP AT LT_FIELDCATALOG INTO LS_FIELDCATALOG. LV_INDEX = SY-TABIX. ASSIGN COMPONENT LS_FIELDCATALOG-FIELDNAME OF STRUCTURE GS_TABLE TO <FV_VALUE>. IF SY-SUBRC EQ 0. LV_DATA = <FV_VALUE>. "Format date LV_LENGTH = STRLEN( LV_DATA ). IF LS_FIELDCATALOG-INTTYPE EQ LC_D AND LV_LENGTH EQ 8 AND <FV_VALUE> IS NOT INITIAL. LV_YEAR = LV_DATA+0(4). LV_MONTH = LV_DATA+4(2). LV_DAY = LV_DATA+6. CONCATENATE LV_YEAR LV_MONTH LV_DAY INTO LV_DATA SEPARATED BY LC_DIAN. ELSEIF LS_FIELDCATALOG-INTTYPE EQ LC_D AND <FV_VALUE> IS INITIAL. CLEAR LV_DATA. ENDIF. IF LV_INDEX EQ 1. LS_DOW = LV_DATA. ELSE. CONCATENATE LS_DOW LV_DATA INTO LS_DOW SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB. ENDIF. ENDIF. CLEAR: LV_DATA,LS_FIELDCATALOG. ENDLOOP. APPEND LS_DOW TO GT_DOW_DATA. CLEAR: GS_TABLE,LS_DOW. ENDLOOP. "Set the total number of rows and columns GV_ROW = LINES( GT_DOW_DATA ). GV_COL = LINES( LT_FIELDCATALOG ). FREE: LT_FIELDCATALOG,LT_TABLE. ENDFORM.
FORM SET_ALIGNMENT. *---------------------------Variables----------------------------* CONSTANTS: LC_L TYPE C LENGTH 1 VALUE 'L', LC_C TYPE C LENGTH 1 VALUE 'C', LC_R TYPE C LENGTH 1 VALUE 'R'. DATA: LO_CELL1 TYPE OLE2_OBJECT, LO_CELL2 TYPE OLE2_OBJECT. DATA: LV_JUST TYPE I, LV_COL TYPE I VALUE 1, LS_FIELDCATALOG LIKE LINE OF GT_FIELDCATALOG. *-----------------------------Logic------------------------------* LOOP GT_FIELDCATALOG INTO LS_FIELDCATALOG WHERE TECH <> GC_X. "Select Action Column FREE OBJECT: LO_CELL1, LO_CELL2, GO_RANGE. CALL METHOD OF GO_EXCEL 'Cells' = LO_CELL1 EXPORTING #1 = 1 #2 = LV_COL. CALL METHOD OF GO_EXCEL 'Cells' = LO_CELL2 EXPORTING #1 = GV_ROW #2 = GV_COL. CALL METHOD OF GO_EXCEL 'Range' = GO_RANGE EXPORTING #1 = LO_CELL1 #2 = LO_CELL2. "Set alignment. CASE LS_FIELDCATALOG-JUST. WHEN LC_L. LV_JUST = 2. WHEN LC_C. LV_JUST = 3. WHEN OTHERS. LV_JUST = 4. ENDCASE. "Reflect alignment to Excel SET PROPERTY OF GO_RANGE 'HorizontalAlignment' = LV_JUST. LV_COL = LV_COL + 1. CLEAR: LS_FIELDCATALOG. ENDLOOP. ENDFORM.
DATA: LO_BDS_DOCUMTNTS TYPE REF TO CL_DBS_DOCUMENT_SET, LV_CLASSNAME TYPE SBDST_CLASSNAME VALUE 'HRFPM_EXCEL_STANDARD', LV_CLASSTYPE TYPE SBDST_CLASSTYPE VALUE 'OT', LV_OBJECTKEY TYPE SBDST_OBJECT_KEY VALUE 'DOITEST', LV_DOC_COMPONENTS TYPE SBDST_COMPONENTS, LV_DOC_SIGNATURE TYPE SBDST_SIGNATURE.
DATA: LT_BDS_URIS TYPE SBDST_URI, LS_BDS_URL LIKE LINE OF LT_BDS_URIS, LV_URL TYPE C LENGTH 256. CREATE OBJECT LO_BDS_DOCUMENTS.
"Excel Object DATA: GO_CONTROL TYPE REF TO I_OI_CONTAINER_CONTROL, GO_ERROR TYPE REF TO I_OI_ERROR, GO_DOCU_PROXY TYPE REF TO I_OI_DOCUMENT_PROXY, GO_DOISHEET TYPE REF TO I_OI_SPREADSHEET.
FROM DOWNLOAD_EXCEL_DOI CHANGING VALUE(PV_FULLPATH) TYPE STRING. "Create Container PERFORM CREATE_DOI_CONTAINER. "Create a blank workbook PERFORM CREATE_BLANK_WORKBOOK. "Open the workbook and instantiate the table object. PERFORM OPEN_WORKBOOK. CHECK GV_MESSAGE IS INITIAL. "Set Excel Header Title PERFORM SET_HEADER_TITLE. "Set Excel Table Value PERFORM GET_DOWNLOAD_DATA_DOI. "Set Excel Style PERFORM SET_EXCEL_DOI_STYLE. "Save Data To Local File PERFORM SAVE_DATA_TO_FILE USING PV_FULLPATH. "Close File And Free Object PERFORM FREE_OBJECT. ENDFORM.
FORM OPEN_WORKBOOK. *---------------------------Variables----------------------------* DATA: LV_TEMP TYPE I. *-----------------------------Logic------------------------------* CLEAR: GV_MESSAGE. "Indicates Whether Interface Is Supported For This Doc. Type GO_DOCU_PROXY->HAS_SPREADSHEET_INTERFACE( IMPORTING IS_AVAILABLE = LV_TEMP ). IF LV_TEMP NE 1. GV_MESSAGE = GC_X. EXIT. ENDIF. "Returns an Interface Reference GO_DOCU_PROXY->GET_SPREADSHEET_INTERFACE( IMPORTING ERROR = GO_ERROR SHEET_INTERFACE = GO_DOISHEET ). IF GO_ERROR->HAS_FAILED = GC_X. GO_ERROR->RAISE_MESSAGE( EXPORTING TYPE = GC_E ). ENDIF. ENDFORM.
FORM SAVE_DATA_TO_FILE USING VALUE(PV_FULLPATH) TYPE STRING. *---------------------------Variables----------------------------* DATA: LV_FILENAME TYPE C LENGTH 255. *-----------------------------Logic------------------------------* LV_FILENAME = PV_FULLPATH. IF GO_DOCU_PROXY IS BOUND. GO_DOCU_PROXY->SAVE_AS( EXPORTING FILE_NAME = LV_FILENAME IMPORTING ERROR = GO_ERROR ). ENDIF. ENDFORM.
(8)关闭文件并释放创建的DOI对象(FREE_OBJECT)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
FORM FREE_OBJECT. *---------------------------Variables----------------------------* DATA: LV_FILENAME TYPE C LENGTH 255. *-----------------------------Logic------------------------------* IF GO_DOCU_PROXY IS NOT INITIAL. "关闭操作好的文档 GO_DOCU_PROXY->CLOSE_DOCUMENT( ). FREE: GO_DOCU_PROXY. ENDIF. IF GO_CONTROL IS NOT INITIAL. "释放资源 GO_CONTROL->DESTROY_CONTROL( ). FREE: GO_CONTROL. ENDIF. ENDFORM.
DATA: LS_XML_CHOICE TYPE IF_SALV_BS_XML=>S_TYPE_XML_CHOICE, LT_XML_CHOICE TYPE IF_SALV_BS_XML=>T_TYPE_XML_CHOICE, LV_XML TYPE XSTRING. R_RESULT_DATA TYPE REF TO CL_SALV_EX_RESULT_DATA_TABLE, LO_DREF TYPE REF TO DATA, LV_FLAVOUR TYPE STRING, LV_VERSION TYPE STRING.
SORT LT_XML_CHOICE BY XML_TYPE. READ TABLE LT_XML_CHOICE INTO LS_XML_CHOICE WITH KEY XML_TYPE = '10' BINARY SEARCH.
(2)根据ALV相关设置,设置数据格式
1 2 3 4 5 6
GET PEFERENCE OF GT_TABLE INTO LO_DREF. R_RESULT_DATA = CL_SALV_EX_UTIL=>FACTORY_RESULT_DATA_TABLE( R_DATA = LO_DREF S_LAYOUT = GS_LAYOUT T_FIELDCATALOG = GT_FIELDCAT T_SORT = GT_SORT ).
(3)转化数据为XML格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
CASE CL_SALV_EX_BASE=>GET_VERSION( ). WHEN IF_SALV_BS_XML=>VERSION_25. LV_VERSION = IF_SALV_BS_XML=>VERSION_25. WHEN IF_SALV_BS_XML=>VERSION_26. LV_VERSION = IF_SALV_BS_XML=>VERSION_26. ENDCASE.