ABAP_EXCEL导出数据
用OLE DOI 实现ABAP导出EXCEL表格
OLE导出:sap中abap 导出excel的方法很多,ole是其中一种,但其导出速度比较慢,一般只应用于对excel格式有特别要求的场所REPORT YGL_MYOLE.INCLUDE OLE2INCL.* OLE OBJECTDA TA:MYEXCEL TYPE OLE2_OBJECT,MYSHEET TYPE OLE2_OBJECT,MYCELL TYPE OLE2_OBJECT,MYWORKBOOK TYPE OLE2_OBJECT. .................................*创建excel进程CREATE OBJECT MYEXCEL 'EXCEL.APPLICATION'.* 创建工作表CALL METHOD OF MYEXCEL 'WORKBOOKS' = MYWORKBOOK.* 创建sheet并添加到工作表SET PROPERTY OF MYEXCEL 'SHEETSINNEWWORKBOOK' = 1.CALL METHOD OF MYWORKBOOK 'ADD'.* 选中excel中的cell,第一行的第二列。
CALL METHOD OF MYEXCEL 'CELLS' = MYCELL EXPORTING #1 = 1 #2 = 2.* 设置被选中的cell的值SET PROTERTY OF MYCELL 'V ALUE' = 'HELLO WORD'. "你要输出的内容GET PROPERTY OF MYEXCEL 'ACTIVESHEET' = MYSHEET.GET PROPERTY OF MYEXCEL 'ACTIVEWORKBOOK' = MYWORKBOOK.* 保存文件CALL METHOD OF MYWORKBOOK 'SAVEAS' EXPORTING #1 = 'C:\TMP\AA.XLS' #2 = 1. CALL METHOD OF MYWORKBOOK 'CLOSE'.* 退出excelCALL METHOD OF MYEXCEL 'QUIT'.* 释放对象FREE OBJECT MYSHEET.FREE OBJECT MYWORKBOOK.FREE OBJECT MYEXCEL.******************************************************************************* 实例:TYPE -POOLS: ole2,slis.DEFINE add_field.wa_field-fieldname = &1 .wa_field-reptext_ddic = &2 .wa_field-no_zero = 'X' . "去掉0* WA_FIELD_EIDT = 'X'.append wa_field to it_field.END -OF -DEFINITION .* 定义变量和字段组DA TA : g_repid TYPE sy-repid,gs_layout TYPE slis_layout_alv,wa_field TYPE slis_fieldcat_alv,it_field TYPE slis_t_fieldcat_alv.******************************************************************************** OLE EXCEL变量DA TA : 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.* 填充单元格FORM fill_cell USING i j val.* CALL METHOD OF EXCEL 'COLUMNS' = COLUMN.* CALL METHOD OF COLUMN 'AUTOFIT'. "导出EXCEL自动适应宽度CALL METHOD OF excel 'CELLS' = cell NO FLUSH EXPORTING #1 = i #2 = j.SET PROPERTY OF cell 'V ALUE' = val no flush.FREE OBJECT cell NO FLUSH.* GET PROPERTY OF CELL 'FONT' = FONT.* SET PROPERTY OF FONT 'BOLD' = BOLD .ENDFORM . "FILL_CELL* 处理进度条FORM p_process USING i_processed LIKE sy-tabixi_total LIKE sy-tabix.DATA :i_percent TYPE i ,i_text2(72 ) TYPE c ,i_times TYPE i ,i_proc(10 ) TYPE c .i_times = 0 .i_percent = ( i_processed * 100 ) DIV i_total.i_times = ( i_percent - STRLEN ( i_proc ) ) DIV 10 .DO i_times TIMES .CONCATENATE i_proc '■' INTO i_proc.CONDENSE i_proc.ENDDO .IF i_percent > 0 .WRITE i_percent TO i_text2.CONDENSE i_text2.CONCATENATE i_text2 '%' i_proc INTO i_text2.ENDIF .CONCATENATE '正在导出EXCEL,目前的进度为' i_text2 INTO i_text2 SEPARATED BY space. "合并文本CONDENSE i_text2. "缩进文本* 启动指示钟CALL FUNCTION 'SAPGUI_PROGRESS_INDICA TOR'EXPORTING* PERCENTAGE = I_PERCENTtext = i_text2EXCEPTIONSOTHERS = 1.ENDFORM . "P_PROCESSDOI:推荐用DOI方式导数据到Excel,而不要用OLE,DOI是调用SAP自带的类来操作的,可控性更高,并且可以把Excel窗口当作一个片屏幕控件放入屏幕的容器中。
ABAP_EXCEL导入内表并ALV显示
*&---------------------------------------------------------------------**& Report Z_TZTEST8*&*&本程序用来把EXCEL导入到内表,再转换一下格式之后,通过ALV显示出来*&本文最后列出导入的EXCEL格式,供大家测试和ALV输出之后的效果*&哥们,兄弟只能帮你到这了**&我的QQ:840560948,大家一起加油吧,吼吼*REPORT Z_TZTEST8.type-pools:slis.data: begin of iexcel occurs 0.include structure alsmex_tabline.data: end of iexcel.data: begin of itab occurs 0,c1(12) type c,c2(4) type c,c3(10) type c,c4(10) type c,c5(6) type c,c6(4) type c,c7(8) type c,c8(8) type c,c9(1) type c,end of itab.data:fieldcat type slis_t_fieldcat_alv with header line. " 定义ALV的有关数据data:layout type slis_layout_alv. " 定义ALV的有关数据data:ivariant like disvariant. " 定义ALV的有关数据data:i_repid like sy-repid. " 定义ALV的有关数据parameters: p_fname like rlgrap-filename memory id m01. " 获取文件路径at selection-screen on value-request for p_fname.perform get_p_fname. " 把需要导入的文件的路径存放在P_name 中start-of-selection.perform upload. " 通过路径把EXCEL中的数据导入到内表iexcel中perform alv.form get_p_fname. " 获取文件路径call function 'WS_FILENAME_GET'exportingmask = ',Excel Files,*.xls,All Files,*.csv,*.*.'title = '请选择要导入的文件'importingfilename = p_fnameexceptionsinv_winsys = 1no_batch = 2selection_cancel = 3selection_error = 4others = 5.IF sy-subrc <> 0 and sy-subrc <> 3.message e100(zdev) with 'error'(007). ENDIF.endform.form upload.call function 'ALSM_EXCEL_TO_INTERNAL_TABLE' exportingfilename = p_fnamei_begin_col = 1i_begin_row = 1i_end_col = 100i_end_row = 100tablesintern = iexcelexceptionsinconsistent_parameters = 1upload_ole = 2others = 3.IF sy-subrc <> 0.write: / '读入文件错误',p_fname.else.LOOP AT iexcel.CASE iexcel-col.WHEN '0001'.move iexcel-value to itab-c1.WHEN '0002'.move iexcel-value to itab-c2.WHEN '0003'.move iexcel-value to itab-c3.WHEN '0004'.move iexcel-value to itab-c4.WHEN '0005'.move iexcel-value to itab-c5.WHEN '0006'.move iexcel-value to itab-c6.WHEN '0007'.move iexcel-value to itab-c7.WHEN '0008'.move iexcel-value to itab-c8.WHEN '0009'.move iexcel-value to itab-c9.ENDCASE.* write iexcel-value(25).at end of row. " 在每一行结束的时候把工作区域itab中的值写到内表itab[]中append itab.clear itab.endat.ENDLOOP.ENDIF.endform.form alv.perform fieldcat_init.layout-colwidth_optimize = 'X'. "设置自动调整列宽layout-zebra = 'X'. " 设置相邻行颜色不同这样便于观看i_repid = sy-repid.CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'EXPORTINGI_CALLBACK_PROGRAM = i_repidIT_FIELDCAT = fieldcat[]IS_VARIANT = ivariantis_layout = layoutTABLESt_outtab = itabEXCEPTIONSPROGRAM_ERROR = 1OTHERS = 2.endform.form fieldcat_init.perform frm_catlg_set using:'C1' '物料号', " '' 里面的字母需要大写'C2' '工厂','C3' '开始日期','C4' '结束日期','C5' '供应商','C6' '采购组织','C7' '计划协议','C8' '行项目','C9' 'MRP'. endform.form frm_catlg_set using p_field p_text. data: temp_fieldcat type slis_fieldcat_alv. temp_fieldcat-fieldname = p_field. temp_fieldcat-seltext_l = p_text. append temp_fieldcat to fieldcat.clear temp_fieldcat.endform.380903908 2100 28.05.2013 31.12.9999 103225 2100 380903909 2100 28.05.2013 31.12.9999 102141 2100 380903910 2100 28.05.2013 31.12.9999 103231 2100 380903911 2100 28.05.2013 31.12.9999 102328 2100 380903912 2100 28.05.2013 31.12.9999 101435 2100 801969313 2100 28.05.2013 31.12.9999 105188 2100 801969315 2100 28.05.2013 31.12.9999 105188 2100 425500102 2100 28.05.2013 31.12.9999 103230 2100 425500103 2100 28.05.2013 31.12.9999 103230 2100 425500104 2100 28.05.2013 31.12.9999 103230 2100 425500105 2100 28.05.2013 31.12.9999 105692 2100 425500106 2100 28.05.2013 31.12.9999 105692 2100 425500107 2100 28.05.2013 31.12.9999 103227 2100 425500108 2100 28.05.2013 31.12.9999 105470 2100 425500109 2100 28.05.2013 31.12.9999 103227 2100 425500110 2100 28.05.2013 31.12.9999 103230 2100 425500111 2100 28.05.2013 31.12.9999 103245 2100 425500112 2100 28.05.2013 31.12.9999 103231 2100 425500113 2100 28.05.2013 31.12.9999 101457 2100 425500114 2100 28.05.2013 31.12.9999 102189 2100 425500115 2100 28.05.2013 31.12.9999 105486 2100425500116 2100 28.05.2013 31.12.9999 105692 2100 425500117 2100 28.05.2013 31.12.9999 100272 2100 425500118 2100 28.05.2013 31.12.9999 103245 2100 425500119 2100 28.05.2013 31.12.9999 103231 2100 425500120 2100 28.05.2013 31.12.9999 105470 2100 425500121 2100 28.05.2013 31.12.9999 103245 2100 425500122 2100 28.05.2013 31.12.9999 101457 2100 425500123 2100 28.05.2013 31.12.9999 103231 2100。
如何在Excel中进行数据导入和导出
如何在Excel中进行数据导入和导出Excel是一款功能强大的电子表格软件,广泛应用于数据处理和分析。
在Excel中,数据导入和导出是一个常见的操作需求,可以帮助我们快速有效地获取和交换数据。
本文将介绍如何在Excel中进行数据导入和导出的方法与技巧。
一、数据导入1. 从文本文件导入在Excel中,我们可以从文本文件导入数据。
操作步骤如下:1) 打开Excel并新建一个工作簿;2) 点击菜单栏中的“数据”选项卡,找到“从文本”命令并点击;3) 在文件选择对话框中,定位并选择需要导入的文本文件;4) 根据文本导入向导的指示,选择合适的分隔符、文本格式等参数;5) 点击“确定”按钮,完成数据导入。
2. 从数据库导入Excel还支持从数据库导入数据,如从SQL Server、Oracle等数据库。
操作步骤如下:1) 打开Excel并新建一个工作簿;2) 点击菜单栏中的“数据”选项卡,找到“从其他来源”命令并点击;3) 在“从其他来源”子菜单中选择“从SQL Server”或者“从Oracle”等选项;4) 根据数据库连接向导的指示,填写数据库服务器信息、登录凭据等;5) 选择需要导入的表或者查询,点击“确定”,完成数据导入。
3. 从Web页面导入如果需要从Web页面上导入数据到Excel中,可以按照以下步骤进行操作:1) 打开Excel并新建一个工作簿;2) 点击菜单栏中的“数据”选项卡,找到“从Web”命令并点击;3) 在“新建Web查询”对话框中,输入需要导入的Web页面URL;4) 点击“确定”,Excel会尝试解析并加载该Web页面上的表格数据;5) 根据需要选择合适的数据表格,点击“导入”完成数据导入。
二、数据导出1. 导出为文本文件在Excel中,我们可以将数据导出为文本文件,方便在其他应用程序中使用。
操作步骤如下:1) 打开包含需要导出的数据的Excel工作簿;2) 选中需要导出的数据区域,可以是某个表格、整个工作表或者多个工作表;3) 点击菜单栏中的“文件”选项卡,找到“另存为”命令并点击;4) 在“另存为”对话框中,选择需要保存的文件格式为“文本(Tab 分隔)”或者其他格式;5) 指定保存路径和文件名,点击“保存”按钮,完成数据导出。
ABAP_EXCEL_DOI_操作手册V1
[ABAP EXCEL DOI 操作]2012年01月04日目录1引言 (3)1.1目的及范围 (3)1.2参考资料 (3)1.3定义 (3)1.4文档结构 (3)2SAP上传EXCEL文件到R3 (3)2.1FM:TEXT_CONVERT_XLS_TO_SAP (3)2.2FM:ALSM_EXCEL_TO_INTERNAL_TABLE (4)3SAP将R3内容下载到EXCEL文件 (5)3.1FM:SAP_CONVERT_TO_XLS_FORMAT (5)4DOI技术操作EXCEL (6)4.1介绍 (6)4.1.1OLE&DOI两者之间的关系: (6)4.1.2OLE&DOI关于EXCEL的处理: (6)4.1.3DOI优点: (6)4.2DOI实现EXCEL操作 (6)4.2.1上传EXCEL模板 (6)4.2.2DOI操作EXCEL的类 (7)5具体案例 (14)5.1上传EXCEL到内表,下载内表数据到EXCEL(一) (15)5.1.1代码实现 (15)5.1.2本地EXCEL文件及内容 (17)5.1.3执行程序 (18)5.1.4执行结果 (19)5.2按单元格上传EXCEL数据到内表 (21)5.2.1代码实现 (21)5.2.2本地EXCEL文档及内容 (23)5.2.3执行程序 (23)5.2.4执行结果 (24)5.3DOI技术操作EXCEL (25)5.3.1上传EXCEL模板 (25)5.3.2代码实现 (28)5.3.3设计屏幕 (41)5.3.4程序执行 (43)1 引言SAP将EXCEL文件中的数据上传到R3(内表),对数据进行处理后,再下载到EXCEL文件中是经常用到的操作,文档主要介绍了对EXCEL文档的上传与下载功能实现的介绍1.1 目的及范围本文档主要介绍如何实现SAP系统上传与下载EXCEL文件介绍及相应实例,可适用于ABAP开发人员,分析人员使用1.2 参考资料1.3 定义1.4 文档结构文档的各章节介绍的内容如下:•第2节介绍SAP上传EXCEL文件到R3•第3节介绍SAP将R3内容下载到EXCEL文件中•第4节具体案例2 SAP上传EXCEL文件到R3以下将讲解SAP如何上传EXCEL文件到内表:2.1 FM:TEXT_CONVERT_XLS_TO_SAP该Function Module用于将EXCEL文件中的数据上传到内表中,要求内表的列数和要传得EXCEL数据的列数要相同,因为FM是按照列来匹配传值的,单元格文本不能超过256个字符。
Excel数据导入导出
Excel数据导入导出Excel是一款功能强大的电子表格软件,被广泛应用于数据处理、分析和管理。
其中,数据导入和导出是使用Excel的常见操作,本文将介绍如何进行Excel数据导入和导出的方法和技巧。
一、数据导入1. 打开Excel软件,点击“文件”菜单,在下拉菜单中选择“打开”。
2. 在弹出的对话框中,选择要导入的数据文件所在的位置,并选中文件,点击“打开”。
3. Excel将自动识别文件类型,并根据文件内容进行数据导入。
你也可以根据需要,选择“导入向导”或“文本导入向导”等选项,进行更详细的数据导入设置。
4. 在导入数据后,Excel会将数据显示在当前的工作表中。
你可以根据需要进行数据格式调整、删除无用数据列、添加计算公式等操作。
5. 最后,点击“文件”菜单,选择“保存”或“另存为”,将导入的数据保存到本地或云端位置。
二、数据导出1. 在Excel软件中打开已有的工作表或创建新的工作表。
2. 点击“文件”菜单,在下拉菜单中选择“导出”。
3. 在弹出的对话框中,选择导出的文件格式。
常见的文件格式包括Excel、CSV、PDF等,你可以根据需要选择适合的文件格式。
4. 选择导出文件的保存位置,并输入文件名,点击“保存”。
5. Excel将根据你的选择,将工作表中的数据导出为指定格式的文件,并保存到指定位置。
三、导入导出的注意事项与技巧1. 数据格式的处理:在进行数据导入之前,务必确认源数据的格式与目标数据的要求一致。
如果需要,可以在进行数据导入之前先进行数据清洗和整理。
2. 列宽和行高的调整:导入数据后,为了更好地显示和阅读数据,你可以根据需要调整列宽和行高。
只需鼠标拖动列宽或行高边界即可实现。
3. 公式的调整:如果导入的数据需要进行计算,你可以使用Excel 的各种公式和函数。
根据具体需求,可以进行公式的拖拽复制、绝对引用和相对引用等操作。
4. 数据筛选和排序:Excel提供了数据筛选和排序功能,可以帮助你快速找到所需数据并进行排序,提高工作效率。
ABAP将excel导出导入到文件夹中
Code listing for: Z00HRJ_IMPORT_Z00HRTWSSIGN Description: 通过EXCEL文件夹导入Z00HRTSIG表*&---------------------------------------------------------------------**& Report Z00HRJ_IMPORT_Z00HRTWSSIGN*&*&------------------------------------------------------------------- ------------------------------------------------------------------* REPORT Z00HRJ_IMPORT_Z00HRTWSSIGN.DATA: GS_PATH TYPE STRING,GS_FILE TYPE STRING,P_EXPORT TYPE STRING,P_FILE LIKE RLGRAP-FILENAME,OK_CODE LIKE SY-TCODE.TYPES:BEGIN OF TYP_SIGN,MANDT TYPE CHAR3,ZNO TYPE CHAR10,SIGN TYPE NUM2,OTYPE TYPE CHAR2,SPERNR TYPE NUM8,GPERNR TYPE NUM8,ZJZBSID TYPE NUM8,BEGDA TYPE DATS,ENDDA TYPE DATS,UPDDA TYPE DATS,END OF TYP_SIGN.TYPES:BEGIN OF TYP_ZB,MANDT TYPE CHAR3,ZNO TYPE CHAR10,SIGN TYPE CHAR2,OTYPE TYPE CHAR2,SPERNR TYPE NUM8,GPERNR TYPE NUM8,BEGDA TYPE CHAR8,ENDDA TYPE CHAR8,UPDDA TYPE CHAR8,END OF TYP_ZB.DATA: GT_SIGN TYPE TABLE OF TYP_ZB WITH HEADER LINE,GWA_SIGN TYPE TYP_ZB,WA_ZBSIGN TYPE Z00HRTSIGN,IT_ZBSIGN TYPE TABLE OF Z00HRTSIGN,WA_JZSIGN TYPE TYP_SIGN,IT_JZSIGN TYPE TABLE OF TYP_SIGN.DATA: C_QB,P_WSBH(2),R_BACKUP TYPE C VALUE 'X',R_JZBS TYPE C ,R_UPDATE TYPE C,R_EXPORT TYPE C,R_DELETE TYPE C.INITIALIZATION.START-OF-SELECTION.CALL SCREEN 100.*&---------------------------------------------------------------------**& Module STATUS_0100 OUTPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE STATUS_0100 OUTPUT.SET PF-STATUS 'STATUS_100'.SET TITLEBAR '0100_TITLE'.LOOP AT SCREEN.IF R_BACKUP EQ 'X' AND ( ( SCREEN-NAME EQ 'P_FILE' OR SCREEN-NAME EQ 'T-001') OR( SCREEN-NAME EQ 'P_EXPORT' OR SCREEN-NAME EQ 'T-003') ).SCREEN-INPUT = '0'.SCREEN-INVISIBLE = '1'.ENDIF.IF R_UPDATE EQ 'X' AND ( ( SCREEN-NAME EQ 'P_FILE' OR SCREEN-NAME EQ 'T-001') OR( SCREEN-NAME EQ 'P_EXPORT' OR SCREEN-NAME EQ 'T-003') OR( SCREEN-NAME EQ 'C_QB' OR SCREEN-NAME EQ 'T-004') ).SCREEN-INPUT = '0'.SCREEN-INVISIBLE = '1'.ENDIF.IF R_DELETE EQ 'X' AND ( ( SCREEN-NAME EQ 'P_FILE' OR SCREEN-NAME EQ 'T-001') OR( SCREEN-NAME EQ 'P_EXPORT' OR SCREEN-NAME EQ 'T-003') OR( SCREEN-NAME EQ 'C_QB' OR SCREEN-NAME EQ 'T-004')OR ( SCREEN-NAME EQ 'P_WSBH' OR SCREEN-NAME EQ 'T-002') ).SCREEN-INPUT = '0'.SCREEN-INVISIBLE = '1'.ENDIF.IF R_JZBS EQ 'X' AND ( SCREEN-NAME EQ 'P_EXPORT' OR SCREEN-NAME EQ 'T-003' OR ( SCREEN-NAME EQ 'C_QB' OR SCREEN-NAME EQ 'T-004') ).SCREEN-INPUT = '0'.SCREEN-INVISIBLE = '1'.ENDIF.IF R_EXPORT EQ 'X' AND ( SCREEN-NAME EQ 'P_FILE' OR SCREEN-NAME EQ 'T-001' OR ( SCREEN-NAME EQ 'C_QB' OR SCREEN-NAME EQ 'T-004') ).SCREEN-INPUT = '0'.SCREEN-INVISIBLE = '1'.ENDIF.MODIFY SCREEN.ENDLOOP.ENDMODULE. " STATUS_0100 OUTPUT*&---------------------------------------------------------------------**& Module USER_COMMAND_0100 INPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE USER_COMMAND_0100 INPUT.DATA: L_WSBH TYPE CHAR2,LT_SIGN TYPE TABLE OF Z00HRTSIGN WITH HEADER LINE.CASE OK_CODE.WHEN 'BACK' OR 'CANCEL' OR 'EXIT'.LEAVE PROGRAM.WHEN 'DRSIGN'.CLEAR OK_CODE.IF C_QB IS INITIAL AND R_DELETE IS INITIAL.IF P_WSBH IS INITIAL.MESSAGE '请填写编号' TYPE 'E'.ENDIF.ENDIF.IF R_JZBS EQ 'X'.IF P_FILE IS INITIAL.MESSAGE '请填写路径' TYPE 'E'.ENDIF.PERFORM FRM_IMPORT_Z00HRTSIGN.ELSEIF R_UPDATE EQ 'X'.PERFORM FRM_UPDATE_99991231.ELSEIF R_EXPORT EQ 'X'.IF P_EXPORT IS INITIAL.MESSAGE '请填写路径' TYPE 'E'.ENDIF.PERFORM FRM_EXPROT_Z00HRTSIGN.ELSEIF R_BACKUP EQ 'X'.IF C_QB EQ ''.SELECT * FROM Z00HRTSIGN INTO TABLE LT_SIGN WHERE SIGN EQ P_WSBH .IF SY-SUBRC EQ 0.INSERT Z00HRTSIGNBF FROM TABLE LT_SIGN ACCEPTING DUPLICATE KEYS.COMMIT WORK.ENDIF.ELSE.SELECT Z00HRJWSBH INTO L_WSBH FROM Z00HRTBMDY.SELECT * FROM Z00HRTSIGN INTO TABLE LT_SIGN WHERE SIGN EQ L_WSBH.IF SY-SUBRC EQ 0.INSERT Z00HRTSIGNBF FROM TABLE LT_SIGN ACCEPTING DUPLICATE KEYS.CLEAR L_WSBH.CLEAR:LT_SIGN[].ENDIF.ENDSELECT.ENDIF.ELSEIF R_DELETE EQ 'X'.DELETE FROM Z00HRTSIGN WHERE GPERNR EQ '' OR SPERNR EQ ''.IF SY-SUBRC EQ 0.COMMIT WORK.ELSE.MESSAGE '删除失败' TYPE 'E'.ENDIF.ENDIF.MESSAGE '执行完成' TYPE 'S'.WHEN OTHERS.ENDCASE.ENDMODULE. " USER_COMMAND_0100 INPUT*&---------------------------------------------------------------------**& Form FRM_PROCESS_Z00HRTSIGN*&---------------------------------------------------------------------** text 导入Z00HRTSIGN表*----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*FORM FRM_IMPORT_Z00HRTSIGN .DATA: Z_COUNT TYPE I.DATA:ZFILE_TABLE TYPE TABLE OF SDOKPATH WITH HEADER LINE.DATA:ZDIR_TABLE TYPE TABLE OF SDOKPATH WITH HEADER LINE.CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'EXPORTINGDIRECTORY = P_FILEFILTER = '*.XLS'IMPORTINGFILE_COUNT = Z_COUNT* DIR_COUNT =TABLESFILE_TABLE = ZFILE_TABLE[]DIR_TABLE = ZDIR_TABLE[]EXCEPTIONSCNTL_ERROR = 1OTHERS = 2.IF SY-SUBRC <> 0 .MESSAGE '文件路径读取失败' TYPE 'S'.EXIT.ELSEIF ZFILE_TABLE[] IS INITIAL.DATA: P_FILEJ(129) TYPE C.CONCATENATE P_FILE '\' INTO P_FILE.CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'EXPORTINGDIRECTORY = P_FILEFILTER = '*.XLS'IMPORTINGFILE_COUNT = Z_COUNT* DIR_COUNT =TABLESFILE_TABLE = ZFILE_TABLE[]DIR_TABLE = ZDIR_TABLE[]EXCEPTIONSCNTL_ERROR = 1OTHERS = 2.IF SY-SUBRC <> 0 .MESSAGE '文件路径读取失败' TYPE 'S'.EXIT.ENDIF.ENDIF.IF ZFILE_TABLE[] IS NOT INITIAL.DATA:PATHNAME(255) TYPE C.DATA:ZTYPE(5) TYPE C.* DATA:P_FILE LIKE RLGRAP-FILENAME.LOOP AT ZFILE_TABLE.SPLIT ZFILE_TABLE-PATHNAME AT '.' INTO: PATHNAME ZTYPE .CONCATENATE P_FILE '\' ZFILE_TABLE-PATHNAME INTO P_FILE.PERFORM FRM_UPDATE_Z00HRTSIGN USING P_FILE.* CALL FUNCTION 'GUI_EXEC'* EXPORTING* COMMAND = 'TASKKILL'* PARAMETER = 'EXCEL.EXE'** IMPORTING** RETURNCODE =.* WAIT UP TO 10 SECONDS.* SUBMIT Z00HRJ_IMPORT_FILE WITH P_FILE = P_FILE AND RETURN.ENDLOOP.ELSE.MESSAGE '文件路径读取失败' TYPE 'S'.EXIT.ENDIF.ENDFORM. " FRM_PROCESS_Z00HRTSIGN*&---------------------------------------------------------------------**& Module F4_FILE INPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE F4_FILE INPUT.DATA: TMP_FOLDER(255).CALL FUNCTION 'TMP_GUI_BROWSE_FOR_FOLDER'EXPORTINGWINDOW_TITLE = '请选择要上传文档所存储的文件夹'IMPORTINGSELECTED_FOLDER = TMP_FOLDER .IF SY-SUBRC = 0.P_FILE = TMP_FOLDER.ENDIF.ENDMODULE. " F4_FILE INPUT*&---------------------------------------------------------------------**& Form FRM_UPDATE_Z00HRTSIGN*&---------------------------------------------------------------------** text 更新Z00HRTSIGN表*----------------------------------------------------------------------** -->P_P_FILE2 text*----------------------------------------------------------------------*FORM FRM_UPDATE_Z00HRTSIGN USING U_FILE .DATA: L_TIMES TYPE I,LEN_ZNO TYPE I,L_INTER_DATA TYPE DATS.FIELD-SYMBOLS <DYN_FIELD>.DATA: IT_EXCEL TYPE TABLE OF ALSMEX_TABLINE WITH HEADER LINE.CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'EXPORTINGFILENAME = U_FILEI_BEGIN_COL = 1I_BEGIN_ROW = 1I_END_COL = 10I_END_ROW = 9999TABLESINTERN = IT_EXCELEXCEPTIONSINCONSISTENT_PARAMETERS = 1UPLOAD_OLE = 2OTHERS = 3.IF SY-SUBRC <> 0.MESSAGE '导入excel报错' TYPE 'E'.ENDIF.SORT IT_EXCEL BY ROW COL.LOOP AT IT_EXCEL.ASSIGN COMPONENT IT_EXCEL-COL OF STRUCTURE WA_JZSIGN TO <DYN_FIELD>.CHECK SY-SUBRC EQ 0.<DYN_FIELD> = IT_EXCEL-VALUE.AT END OF ROW.APPEND WA_JZSIGN TO IT_JZSIGN.CLEAR WA_JZSIGN.ENDAT.ENDLOOP.LOOP AT IT_JZSIGN INTO WA_JZSIGN.IF WA_JZSIGN-BEGDA EQ '0'.CLEAR WA_JZSIGN-BEGDA.ENDIF.IF WA_JZSIGN-ENDDA EQ '0'.CLEAR WA_JZSIGN-ENDDA.ENDIF.IF WA_JZSIGN-UPDDA EQ '0'.CLEAR WA_JZSIGN-UPDDA.ENDIF.* UPDATE Z00HRTSIGN SET ENDDA = '99991231' WHERE SPERNR = WA_JZSIGN-SPERNR."将对象更新结束日期是99991231* COMMIT WORK.MOVE-CORRESPONDING WA_JZSIGN TO WA_ZBSIGN.CLEAR WA_ZBSIGN-SPERNR.LEN_ZNO = STRLEN( WA_ZBSIGN-ZNO ).IF LEN_ZNO LE 10.L_TIMES = 10 - LEN_ZNO.DO L_TIMES TIMES.CONCATENATE '0' WA_ZBSIGN-ZNO INTO WA_ZBSIGN-ZNO.ENDDO.ENDIF.CONCATENATE 'JZ' WA_ZBSIGN-ZNO+2 INTO WA_ZBSIGN-ZNO.WA_ZBSIGN-SPERNR = WA_JZSIGN-ZJZBSID.WA_ZBSIGN-ENDDA = '88881231'."将集中部署对象更新结束日期是88881231APPEND WA_ZBSIGN TO IT_ZBSIGN.CLEAR:WA_ZBSIGN,WA_JZSIGN,LEN_ZNO,L_TIMES.ENDLOOP.MODIFY Z00HRTSIGN FROM TABLE IT_ZBSIGN.COMMIT WORK.MESSAGE '执行完成' TYPE 'S'.ENDFORM. " FRM_UPDATE_Z00HRTSIGN*&---------------------------------------------------------------------**& Form FRM_UPDATE_99991231*&---------------------------------------------------------------------** text 将Z00HRTSIGN结束日期更新99991231*----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*FORM FRM_UPDATE_99991231 .DATA: LT_SIGN TYPE TABLE OF Z00HRTSIGN WITH HEADER LINE.DO.SELECT * FROM Z00HRTSIGNINTO TABLE LT_SIGN UP TO 5000 ROWSWHERE ENDDA NE '99991231' AND ENDDA NE '88881231' AND SIGN EQ P_WSBH.IF SY-SUBRC NE 0.EXIT.ENDIF.LOOP AT LT_SIGN.LT_SIGN-ENDDA = '99991231'.MODIFY LT_SIGN.ENDLOOP.UPDATE Z00HRTSIGN FROM TABLE LT_SIGN.COMMIT WORK.ENDDO.ENDFORM. " FRM_UPDATE_99991231*&---------------------------------------------------------------------**& Module F4_WSBH INPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE F4_WSBH INPUT.DATA: XDYNPFIELDS TYPE TABLE OF DYNPREAD WITH HEADER LINE,LT_WSBH TYPE TABLE OF Z00HRTBMDY WITH HEADER LINE,SELECTED_TABLE TYPE TABLE OF DDSHRETVAL WITH HEADER LINE.SELECT * FROM Z00HRTBMDY INTO TABLE LT_WSBH.CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'EXPORTINGRETFIELD = 'Z00HRJWSBH'DYNPPROG = SY-CPROGDYNPNR = SY-DYNNRDYNPROFIELD = 'P_WSBH'STEPL = SY-STEPLWINDOW_TITLE = '选择网省代码'VALUE_ORG = 'S'TABLESRETURN_TAB = SELECTED_TABLEVALUE_TAB = LT_WSBHEXCEPTIONSPARAMETER_ERROR = 1NO_VALUES_FOUND = 2OTHERS = 3.IF SY-SUBRC EQ 0.P_WSBH = SELECTED_TABLE-FIELDVAL.ELSE.MESSAGE 'error' TYPE 'E'.ENDIF.REFRESH XDYNPFIELDS.CLEAR XDYNPFIELDS.XDYNPFIELDS-FIELDNAME = 'P_WSBH'.XDYNPFIELDS-FIELDVALUE = P_WSBH.XDYNPFIELDS-FIELDINP = 'X'.APPEND XDYNPFIELDS.CALL FUNCTION 'DYNP_VALUES_UPDATE'EXPORTINGDYNAME = SY-REPIDDYNUMB = SY-DYNNRTABLESDYNPFIELDS = XDYNPFIELDSEXCEPTIONSINVALID_ABAPWORKAREA = 1INVALID_DYNPROFIELD = 2INVALID_DYNPRONAME = 3INVALID_DYNPRONUMMER = 4INVALID_REQUEST = 5NO_FIELDDESCRIPTION = 6UNDEFIND_ERROR = 7OTHERS = 8.ENDMODULE. " F4_WSBH INPUT*&---------------------------------------------------------------------**& Form FRM_EXPROT_Z00HRTSIGN*&---------------------------------------------------------------------** text 导出Z00HRTSIGN表*----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*FORM FRM_EXPROT_Z00HRTSIGN .DATA: LV_FILE LIKE RLGRAP-FILENAME,LV_NAME(200) TYPE C,LV_NUM(4) TYPE N,LV_FLAG TYPE C,LV_NEXT TYPE I VALUE '1',LV_TABIX TYPE I,LT_DOWN TYPE TABLE OF Z00HRTSIGN WITH HEADER LINE,LWA_SIGN TYPE Z00HRTSIGN,LT_Z00HRTSIGN TYPE TABLE OF Z00HRTSIGN WITH HEADER LINE.SELECT * FROM Z00HRTSIGN INTO TABLE LT_Z00HRTSIGN WHERE SIGN EQ P_WSBH AND ENDDA EQ '99991231'.IF SY-SUBRC NE 0.MESSAGE '没有查询到数据' TYPE 'E'.ENDIF.LOOP AT LT_Z00HRTSIGN INTO LWA_SIGN.CLEAR:LT_DOWN,LV_FLAG.LV_TABIX = LV_TABIX + 1.LV_NEXT = LV_NEXT + 1.IF LV_TABIX < 9000.READ TABLE LT_Z00HRTSIGN INDEX LV_NEXT.IF SY-SUBRC NE 0.LV_FLAG = 'X'.ENDIF.ELSE.LV_FLAG = 'X'.ENDIF.APPEND LWA_SIGN TO GT_SIGN.CLEAR LWA_SIGN.IF LV_FLAG EQ 'X'.LV_FILE = P_EXPORT.LV_NUM = LV_NUM + 1.CONCATENATE LV_FILE 'ZB-Z00HRTSIGN-' LV_NUM '.XLS' INTO LV_FILE. * REPLACE FIRST OCCURRENCE OF '.XLS' IN LV_FILE WITH LV_NAME. CALL FUNCTION 'WS_DOWNLOAD'EXPORTINGFILENAME = LV_FILEFILETYPE = 'DAT'TABLESDATA_TAB = GT_SIGN[]EXCEPTIONSFILE_OPEN_ERROR = 1FILE_WRITE_ERROR = 2INVALID_FILESIZE = 3INVALID_TYPE = 4NO_BATCH = 5UNKNOWN_ERROR = 6INVALID_TABLE_WIDTH = 7GUI_REFUSE_FILETRANSFER = 8CUSTOMER_ERROR = 9NO_AUTHORITY = 10OTHERS = 11.IF SY-SUBRC = 0.MESSAGE '下载完毕' TYPE 'S'.ELSEIF SY-SUBRC <> 0.MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNOWITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.CLEAR:LV_TABIX,GT_SIGN[].ENDIF.ENDLOOP.MESSAGE '执行结束' TYPE 'S'.ENDFORM. " FRM_EXPROT_Z00HRTSIGN*&---------------------------------------------------------------------**& Module F4_EXPORT INPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE F4_EXPORT INPUT.CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOGEXPORTINGWINDOW_TITLE = '请选择文件保存路径:'FILE_FILTER = '*.xls|*.XLS'PROMPT_ON_OVERWRITE = ' 'CHANGINGFILENAME = GS_FILEPATH = P_EXPORTFULLPATH = GS_PATH.ENDMODULE. " F4_EXPORT INPUT*GUI Texts*----------------------------------------------------------。
Excel数据导入导出掌握这个技巧数据互通无障碍
Excel数据导入导出掌握这个技巧数据互通无障碍在日常工作中,我们经常会遇到需要大量的数据记录、整理和处理的情况。
而Excel作为一款强大的数据处理工具,为我们提供了很多方便快捷的功能,其中包括数据导入和导出。
掌握这些技巧,能够使我们在处理数据时更加高效,并且实现数据的互通无障碍。
一、数据导入技巧1. 打开Excel软件,选择“文件”-“打开”,找到需要导入的数据文件并选择打开;2. 在打开的Excel表格中,找到想要导入数据的位置,点击该单元格位置,将光标移动到该单元格;3. 选择“数据”-“从文本”命令,打开“导入文本向导”对话框;4. 在“导入文本向导”对话框中,选择需要导入的文件类型,如CSV 文件、文本文件等,然后点击“下一步”按钮;5. 在第二步选择分隔符的界面上,确保选中正确的分隔符类型,并预览导入的数据,确认无误后点击“下一步”按钮;6. 在第三步中可以进行列数据格式的设置,包括日期格式、数值格式等,设置完成后点击“下一步”按钮;7. 在第四步中选择数据的导入位置和导入的工作表名称,点击“完成”按钮,完成数据的导入操作。
二、数据导出技巧1. 打开Excel软件,并打开包含需要导出数据的工作表;2. 在工作表中,选择需要导出的数据范围,可以是一列、一行或者一段区域;3. 选择“文件”-“另存为”,在弹出的对话框中选择要保存的文件路径和文件名,并选择保存文件的格式,如CSV文件、文本文件等;4. 点击“保存”按钮,完成数据的导出操作。
通过以上的简单步骤,我们可以轻松地实现Excel数据的导入和导出,从而实现不同系统之间的数据互通。
在实际应用中,我们还可以根据需要进行一些额外的操作,以满足特定的需求。
三、导入导出技巧的应用场景1. 数据更新:当需要将外部数据导入到Excel中进行更新时,可以使用数据导入技巧。
比如,某公司的销售数据存储在数据库中,我们可以将数据导入到Excel表格中进行分析和处理,然后再将结果导出到数据库中进行更新;2. 数据备份:在数据处理过程中,为了防止数据丢失或者出错,我们通常需要对数据进行备份。
【ABAP】OLE2的使用 操作EXCEL把内表中的数据在EXCEL中显示出来
* 输出到EXCEL
LW_S = '4'.
LOOP AT TD_BKPF INTO TH_BKPF.
LW_H = 'A'.
PERFORM FRM_FILL_CELL USING LW_H LW_S TH_BKPF-BUKRS.
WHERE BUKRS = P_BUKRS
AND GJAHR = P_GJAHR.
IF SY-SUBRC <> 0.
* MESSAGE S001 DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " FRM_GET_BKPF_DATA
CALL METHOD OF W_BOOKS CNS_OPEN = W_BOOK
EXPORTING
#1 = CNS_DOWN_PATH.
CALL METHOD OF W_BOOK CNS_WORKSHEETS = W_SHEET
EXPORTING
#1 = 'Sheet1'.
*&-----ቤተ መጻሕፍቲ ባይዱ---------------------------------------------------------------*
* 向EXCEL中填入数据,并显示出来
*----------------------------------------------------------------------*
START-OF-SELECTION.
ABAP_Excel_处理总结 2
1 调用原理1.1 创建对象首先定义OLE对象,创建OLE对象为excel类型。
定义格式如下:CREATE OBJECT application 'excel.APPLICATION'.然后通过excel对象application,创建与excel对应的workbook,sheet,range,cell等对象。
再次通过调用各个对象的方法及属性等,实现对excel的操作。
1.2 Excel宏利用宏可以查看各个excel对象的方法和属性。
宏的录制和利用参见文档:(略)2 方法用例*&---------------------------------------------------------------------**& Report ZDEALEXCEL*& Author ALEX*& Date 2009.10*&*&---------------------------------------------------------------------**& 本程序总结了常用的ABAP对EXCEL的操作,包括如下子例程*& 方法名方法说明*& (1) create_excel 创建excel*& (2) insert_row 插入行*& (3) fill_cell 填充单元格*& (4) columnwidth 调整列宽*& (5) create_sheet 创建sheet*& (6) copy_sheet_all sheet复制*& (7) copy_sheet_area sheet区域复制*& (8) copy_cells 复制单元格*& (9) range_borders 边框粗细*& (10) row_color 行颜色*& (11) preview_excel 预览*& (12) free_object 释放对象*& (13) close_excel 关闭excel*& (14) download_excel_fromserver 从服务器下载excel模板(进入模板事务代码SMW0)*& (15) open_excel_hide 隐蔽打开excel*& (16) open_excel 打开excel*& (17) open_excel_sheet 打开指定的excel的sheet*& (18) open_excel_by_sheet 打开sheet*& (19) select_range 选择范围*& (20) clear_data 清除数据*& (21) merge_cells 合并单元格*& (22) process_indcator 进度指示*& (23) delete_row 删除行*& (24) add_comment 添加注释*& (25) hide_columns 隐藏列*& (26) delete_columns 删除列*& (27) hide_row 隐藏行* 保护工作区 lock_unlock_excel :未完成* 保护整个工作表 lock_sheet :未完成*& 本示例程序作为ABAP对EXCEL操作的参考,可以根据需要做适当的调整*&---------------------------------------------------------------------*REPORT zdealexcel.TYPE-POOLS ole2.TABLES sscrfields.DEFINE m_message.case sy-subrc.when '0'.when others.message e000(zmeg)with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.endcase.END-OF-DEFINITION.DATA: application TYPE ole2_object, "excel objectworkbook TYPE ole2_object, "excel workbook objcetsheet TYPE ole2_object, "workbook sheet objectcolumns TYPE ole2_object, "sheet col objcetrows TYPE ole2_object, "sheet row objcetrange TYPE ole2_object, "rangerange1 TYPE ole2_object, "range1font TYPE ole2_object, "fontcell TYPE ole2_object, "cellcell1 TYPE ole2_object, "cell1sheet1 TYPE ole2_object, "workbook sheet objectborders TYPE ole2_object. "bordersDATA: index TYPE i VALUE 0.*定义模板下载到本地保存的全名称DATA: p_down_file LIKE sapb-sappfad. "定义模板下载到本地保存的全名称*演示excel操作SELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN: PUSHBUTTON 01(12) but0 USER-COMMAND cli0,PUSHBUTTON 20(15) but2 USER-COMMAND cli2. SELECTION-SCREEN END OF LINE.*导入数据块SELECTION-SCREEN BEGIN OF BLOCK status2 WITH FRAME TITLE text-f02. SELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN COMMENT 1(20) text-002.PARAMETERS: p_file LIKE rlgrap-filename.SELECTION-SCREEN END OF LINE.SELECTION-SCREEN END OF BLOCK status2.*导出数据块SELECTION-SCREEN BEGIN OF BLOCK status3 WITH FRAME TITLE text-f03 . SELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN COMMENT 1(20) text-003.PARAMETERS: p_down LIKE rlgrap-filename.SELECTION-SCREEN:PUSHBUTTON 75(20) but1 USER-COMMAND cli1.SELECTION-SCREEN END OF LINE.SELECTION-SCREEN END OF BLOCK status3.INITIALIZATION.CREATE OBJECT application 'excel.APPLICATION'.but0 = '演示excel操作'. "初始化导出button显示名称but1 = '导出'. "初始化导出button显示名称but2 = '从模板导出excel'. "从模板导出excel*-------------------------设置文件上载-------------------------AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.CALL FUNCTION 'WS_FILENAME_GET'EXPORTINGdef_path = p_filemask = ',*.*.'mode = 'O'title = text-f01IMPORTINGfilename = p_fileEXCEPTIONSinv_winsys = 1no_batch = 2selection_cancel = 3selection_error = 4OTHERS = 5.CHECK sy-subrc = 0 AND NOT p_file IS INITIAL.*-------------------------设置文件下载-------------------------AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_down.CALL FUNCTION 'WS_FILENAME_GET'EXPORTINGdef_path = p_downmask = ',*.xls,*.xls.'mode = 'S'title = text-f02IMPORTINGfilename = p_downEXCEPTIONSinv_winsys = 1no_batch = 2selection_cancel = 3selection_error = 4OTHERS = 5.CHECK sy-subrc = 0 AND NOT p_down IS INITIAL.*--------------------------------------------------------------AT SELECTION-SCREEN.IF sscrfields-ucomm EQ 'CLI1'.IF p_down IS INITIAL.MESSAGE e804(zdev).ELSE.p_down_file = p_down.PERFORM download_excel_fromserver USING 'ZHRI0004' p_down_file . "下载模板到本地MESSAGE i600(zdev) WITH '模板导出成功'.ENDIF.ELSEIF sscrfields-ucomm EQ 'ONLI'.IF p_file IS INITIAL.MESSAGE e803(zdev).ENDIF.ELSEIF sscrfields-ucomm EQ 'CLI0'.PERFORM demo_forms.ELSEIF sscrfields-ucomm EQ 'CLI2'.PERFORM demo_forms_server USING p_file.ENDIF.START-OF-SELECTION.PERFORM open_excel_hide USING p_file.PERFORM close_excel.PERFORM open_excel USING p_file.PERFORM close_excel.PERFORM open_excel_sheet USING 'sheet2' p_file.DO 10 TIMES.PERFORM insert_row USING 1.PERFORM fill_cell USING 1 1 0 'e'.PERFORM fill_cell USING 1 2 0 'f'.PERFORM fill_cell USING 1 3 0 'g'.index = index + 1.ENDDO.PERFORM close_excel.PERFORM free_object.*&---------------------------------------------------------------------**& Form demo_forms_server*&---------------------------------------------------------------------** 从SMW0下载excel模板*----------------------------------------------------------------------** -->P_FILE excel本地路径*----------------------------------------------------------------------* FORM demo_forms_server USING p_file.p_down_file = p_file.PERFORM process_indcator USING '程序正在下载模板' 0 .PERFORM download_excel_fromserver USING 'ZHRI0004' p_down_file . PERFORM process_indcator USING '程序正在初始化OLE控件' 0 .PERFORM process_indcator USING '程序正在打开excel文档' 0 .PERFORM open_excel_sheet USING '处分处罚信息批导入' p_file.DO 10 TIMES.PERFORM insert_row USING 5.PERFORM fill_cell USING 5 1 0 'e'.PERFORM fill_cell USING 5 2 0 'f'.PERFORM fill_cell USING 5 3 0 'g'.index = index + 1.ENDDO.PERFORM clear_data USING 1 6 2 7.PERFORM merge_cells USING 1 6 2 7.PERFORM add_comment USING 8 1 'OK'.PERFORM close_excel.PERFORM free_object.ENDFORM. "demo_forms_server*&---------------------------------------------------------------------* *& Form demo_forms*&---------------------------------------------------------------------* * 演示用form*----------------------------------------------------------------------* FORM demo_forms.PERFORM create_excel.DO 10 TIMES.PERFORM insert_row USING 1.PERFORM fill_cell USING 1 1 0 'a'.PERFORM fill_cell USING 1 2 0 'b'.PERFORM fill_cell USING 1 3 0 'c'.index = index + 1.ENDDO.PERFORM columnwidth USING 'A:C' '' .PERFORM create_sheet.PERFORM create_sheet.PERFORM copy_sheet_all.PERFORM copy_sheet_area.PERFORM copy_cells.PERFORM range_borders.PERFORM row_color USING 1.PERFORM preview_excel.PERFORM del_sheet USING 'sheet2'.PERFORM free_object.ENDFORM. "demo_forms*&---------------------------------------------------------------------* *& (1) Form create_excel*&---------------------------------------------------------------------* * 创建excel*----------------------------------------------------------------------* FORM create_excel.IF sy-subrc <> 0.MESSAGE 'EXCEL ERROR' TYPE 'S' DISPLAY LIKE 'E'.STOP.ENDIF.CALL METHOD OF application 'WORKBOOKS' = workbook.SET PROPERTY OF application 'VISIBLE' = 1.SET PROPERTY OF application 'SHEETSINNEWWORKBOOK' = 1. CALL METHOD OF workbook 'ADD' = sheet.CALL METHOD OF sheet 'ACTIVE'.ENDFORM. "create_excel*&---------------------------------------------------------------------* *& (2) Form insert_row*&---------------------------------------------------------------------* * 根据行号插入一行*----------------------------------------------------------------------* * -->I_ROW 行号*----------------------------------------------------------------------* FORM insert_row USING i_row.CALL METHOD OF application 'ROWS' = rowsEXPORTING#1 = i_row.CALL METHOD OF rows 'INSERT'.ENDFORM. "insert_row*&---------------------------------------------------------------------* *& (3) Form fill_cell*&---------------------------------------------------------------------* * 填充单元格*----------------------------------------------------------------------* * -->I_ROW 行号* -->I_COL 列号* -->BOLD 是否加粗* -->P_VALUE 值*----------------------------------------------------------------------* FORM fill_cell USING i_row i_col bold p_value.CALL METHOD OF application 'CELLS' = cellEXPORTING#1 = i_row#2 = i_col.SET PROPERTY OF cell 'VALUE' = p_value.SET PROPERTY OF cell 'HORIZONTALALIGNMENT' = 2.GET PROPERTY OF cell 'FONT' = font.SET PROPERTY OF font 'BOLD' = bold.SET PROPERTY OF font 'COLORINDEX' = 41.CALL METHOD OF cell 'BORDERS' = bordersEXPORTING#1 = '2'.SET PROPERTY OF borders 'LINESTYLE' = '0'.SET PROPERTY OF borders 'WEIGHT' = 0.ENDFORM. "fill_cell*&---------------------------------------------------------------------* *& (4) Form columnwidth*&---------------------------------------------------------------------* * 设置列宽为自动或是固定值*----------------------------------------------------------------------* * -->COLNUM 列号,可以为单个列或是多个列* -->WIDTH 列宽值*----------------------------------------------------------------------* FORM columnwidth USING colnum width.CALL METHOD OF application 'COLUMNS' = columnsEXPORTING#1 = colnum.IF width = ''.CALL METHOD OF columns 'AutoFit'.ELSE.SET PROPERTY OF columns 'COLUMNWIDTH' = width.ENDIF.ENDFORM. "columnwidth*&---------------------------------------------------------------------* *& (5) Form create_sheet*&---------------------------------------------------------------------* * 创建sheet*----------------------------------------------------------------------* FORM create_sheet.CALL METHOD OF application 'sheets' = sheet.CALL METHOD OF sheet 'ADD'.ENDFORM. "create_sheet*&---------------------------------------------------------------------* *& (6) Form copy_sheet_all*&---------------------------------------------------------------------* * 复制sheet全部内容到另一个sheet*----------------------------------------------------------------------* FORM copy_sheet_all.CALL METHOD OF application 'Sheets' = sheetEXPORTING#1 = 'Sheet1'.CALL METHOD OF sheet 'Cells' = cell.CALL METHOD OF cell 'copy'.CALL METHOD OF application 'Sheets' = sheetEXPORTING#1 = 'Sheet3'.CALL METHOD OF application 'Range' = rangeEXPORTING#1 = 'A1'.CALL METHOD OF range 'PasteSpecial'.ENDFORM. "copy_sheet_all*&---------------------------------------------------------------------* *& (7) Form copy_sheet_area*&---------------------------------------------------------------------* * 复制sheet部分内容到另一个sheet*----------------------------------------------------------------------* FORM copy_sheet_area.CALL METHOD OF application 'Sheets' = sheetEXPORTING#1 = 'Sheet1'.CALL METHOD OF sheet 'Range' = rangeEXPORTING#1 = 'A1'#2 = 'B10'.CALL METHOD OF range 'copy'.CALL METHOD OF application 'Sheets' = sheet1EXPORTING#1 = 'Sheet2'.CALL METHOD OF sheet1 'Range' = range1EXPORTING#1 = 'A1'#2 = 'B10'.CALL METHOD OF range1 'PasteSpecial'.ENDFORM. "copy_sheet_area*&---------------------------------------------------------------------* *& (8) Form copy_cells*&---------------------------------------------------------------------* * 在一个sheet内复制单元格*----------------------------------------------------------------------* FORM copy_cells.CALL METHOD OF application 'Sheets' = sheetEXPORTING#1 = 'Sheet1'.CALL METHOD OF sheet 'Range' = rangeEXPORTING#1 = 'A1'#2 = 'B10'.CALL METHOD OF range 'copy'.CALL METHOD OF sheet 'Range' = range1EXPORTING#1 = 'A12'#2 = 'B22'.CALL METHOD OF range1 'PasteSpecial'.ENDFORM. "copy_cells*&---------------------------------------------------------------------* *& (9) Form range_borders*&---------------------------------------------------------------------* * 指定range加边框*----------------------------------------------------------------------* FORM range_borders.CALL METHOD OF application 'Range' = rangeEXPORTING#1 = 'A1'#2 = 'B10'.CALL METHOD OF range 'BORDERS' = borders.SET PROPERTY OF borders 'LINESTYLE' = 1.SET PROPERTY OF borders 'WEIGHT' = 10.ENDFORM. "range_borders*&---------------------------------------------------------------------* *& (10) Form row_color*&---------------------------------------------------------------------* * 设定行背景色*----------------------------------------------------------------------* * -->I_ROW 行号*----------------------------------------------------------------------* FORM row_color USING i_row.CALL METHOD OF application 'ROWS' = rowsEXPORTING#1 = i_row.GET PROPERTY OF rows 'Interior' = font.SET PROPERTY OF font 'ColorIndex' = '10'.ENDFORM. "row_color*&---------------------------------------------------------------------* *& (11) Form preview_excel*&---------------------------------------------------------------------* * 预览excel*----------------------------------------------------------------------* FORM preview_excel.SET PROPERTY OF application 'VISIBLE' = 1.CALL METHOD OF sheet 'PRINTPREVIEW'.* m_message.ENDFORM. "preview_excel*&---------------------------------------------------------------------* *& (12) Form free_object*&---------------------------------------------------------------------* * 释放object*----------------------------------------------------------------------* FORM free_object.FREE OBJECT font.FREE OBJECT range.FREE OBJECT range1.FREE OBJECT columns.FREE OBJECT rows.FREE OBJECT cell.FREE OBJECT cell1.FREE OBJECT sheet1.FREE OBJECT sheet.FREE OBJECT workbook.FREE OBJECT application.ENDFORM. "free_object*&---------------------------------------------------------------------* *& (13) Form close_excel*&---------------------------------------------------------------------* * 关闭excel并保存*----------------------------------------------------------------------* FORM close_excel.CALL METHOD OF workbook 'Save'.CALL METHOD OF workbook 'Close'EXPORTING#1 = 0.CALL METHOD OF application 'Quit'.ENDFORM. "close_excel*&---------------------------------------------------------------------* *& (14) Form download_excel_fromserver*&---------------------------------------------------------------------* * 从服务器下载excel模板*----------------------------------------------------------------------* * -->P_OBJID 模板名称* -->P_DEST 存档路径*----------------------------------------------------------------------* FORM download_excel_fromserverUSING p_objid LIKE wwwdatatab-objidp_dest LIKE sapb-sappfad.DATA: lo_objdata LIKE wwwdatatab,lo_mime LIKE w3mime,ls_destination LIKE rlgrap-filename,ls_objnam TYPE string,li_rc LIKE sy-subrc,ls_errtxt TYPE string.CONCATENATE p_objid '.XLS' INTO ls_objnam.CONDENSE ls_objnam NO-GAPS.SELECT SINGLE relid objid FROM wwwdataINTO CORRESPONDING FIELDS OF lo_objdataWHERE srtf2 = 0AND relid = 'MI'AND objid = p_objid.IF sy-subrc NE 0 OR lo_objdata-objid EQ space.CONCATENATE '模板文件:' ls_objnam '不存在,请用TCODE:SMW0进行加载' INTO ls_errtxt.MESSAGE e600(zdev) WITH ls_errtxt.ENDIF.ls_destination = p_dest.CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'EXPORTINGkey = lo_objdatadestination = ls_destinationIMPORTINGrc = li_rc.IF li_rc NE 0.CONCATENATE '模板文件:' ls_objnam '下载失败' INTO ls_errtxt.MESSAGE e600(zdev) WITH ls_errtxt.ENDIF.ENDFORM. "download_excel_fromserver*&---------------------------------------------------------------------**& (15) Form open_excel_hide*&---------------------------------------------------------------------** 后台打开指定的excel*----------------------------------------------------------------------** -->P_FILE excel本地路径*----------------------------------------------------------------------*FORM open_excel_hide USING p_file.SET PROPERTY OF application 'Visible' = 0.CALL METHOD OF application 'Workbooks' = workbook.CALL METHOD OF workbook 'Open' = workbookEXPORTING#1 = p_file.CALL METHOD OF workbook 'Worksheets' = sheetEXPORTING#1 = 'Sheet1'.CALL METHOD OF sheet 'Activate'.ENDFORM. "open_excel_hide*&---------------------------------------------------------------------* *& (16) Form open_excel*&---------------------------------------------------------------------* * 打开指定的excel*----------------------------------------------------------------------* * -->P_FILE excel本地路径*----------------------------------------------------------------------* FORM open_excel USING p_file.SET PROPERTY OF application 'Visible' = 1.CALL METHOD OF application 'Workbooks' = workbook.CALL METHOD OF workbook 'Open' = workbookEXPORTING#1 = p_file.CALL METHOD OF workbook 'Worksheets' = sheetEXPORTING#1 = 'Sheet1'.CALL METHOD OF sheet 'Activate'.ENDFORM. "open_excel*&---------------------------------------------------------------------* *& (17) Form open_excel_sheet*&---------------------------------------------------------------------* * 打开指定的excel的sheet*----------------------------------------------------------------------* * -->P_SHEET 打开的sheet名* -->P_FILE excel本地路径*----------------------------------------------------------------------* FORM open_excel_sheet USING p_sheet p_file.SET PROPERTY OF application 'Visible' = 1.CALL METHOD OF application 'Workbooks' = workbook.CALL METHOD OF workbook 'Open' = workbookEXPORTING#1 = p_file.CALL METHOD OF workbook 'Worksheets' = sheetEXPORTING#1 = p_sheet.CALL METHOD OF sheet 'Activate'.ENDFORM. "open_excel_sheet*&---------------------------------------------------------------------**& (18) Form open_excel_by_sheet*&---------------------------------------------------------------------* * 打开sheet*----------------------------------------------------------------------* * -->P_SHEET 打开的sheet名*----------------------------------------------------------------------* FORM open_excel_by_sheet USING p_sheet.CALL METHOD OF workbook 'WORKSHEETS' = sheetEXPORTING#1 = p_sheet.CALL METHOD OF sheet 'ACTIVATE'.ENDFORM. "open_excel_by_sheet*&---------------------------------------------------------------------* *& (19) Form select_range*&---------------------------------------------------------------------* * 选择范围*----------------------------------------------------------------------* * -->P_LEFT 左* -->P_TOP 上* -->P_RIGHT 右* -->P_BUTTOM 下*----------------------------------------------------------------------* FORM select_range USINGp_left TYPE ip_top TYPE ip_right TYPE ip_buttom TYPE i.CALL METHOD OF application 'CELLS' = cellEXPORTING#1 = p_top#2 = p_left.CALL METHOD OF application 'CELLS' = cell1EXPORTING#1 = p_buttom#2 = p_right.CALL METHOD OF application 'RANGE' = rangeEXPORTING#1 = cell#2 = cell1.CALL METHOD OF range 'SELECT'.ENDFORM. "select_range*&---------------------------------------------------------------------* *& (20) Form clear_data*&---------------------------------------------------------------------* * 清除数据*----------------------------------------------------------------------* * -->P_LEFT 左* -->P_TOP 上* -->P_RIGHT 右* -->P_BUTTOM 下*----------------------------------------------------------------------* FORM clear_data USING p_left TYPE ip_top TYPE ip_right TYPE ip_buttom TYPE i.PERFORM select_range USING p_leftp_topp_rightp_buttom.CALL METHOD OF range 'ClearContents'.ENDFORM. "clear_data*&---------------------------------------------------------------------* *& (21) Form merge_cells*&---------------------------------------------------------------------* * 合并单元格*----------------------------------------------------------------------* * -->P_LEFT text* -->P_TOP text* -->P_RIGHT text* -->P_BUTTOM text*----------------------------------------------------------------------* FORM merge_cells USING p_left TYPE ip_top TYPE ip_right TYPE ip_buttom TYPE i.PERFORM select_range USING p_leftp_topp_rightp_buttom.CALL METHOD OF range 'Merge'.ENDFORM. "merge_cells*&---------------------------------------------------------------------* *& (22) Form process_indcator*&---------------------------------------------------------------------* * 进度指示*----------------------------------------------------------------------* * -->TEXT 提示文字* -->PERCENTAGE 进度百分比*----------------------------------------------------------------------* FORM process_indcator USING text percentage.CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'EXPORTINGpercentage = percentagetext = text.ENDFORM. "process_indcator*&---------------------------------------------------------------------* *& (23) Form delete_row*&---------------------------------------------------------------------* * 删除行*----------------------------------------------------------------------* * -->I_ROW 行号*----------------------------------------------------------------------* FORM delete_row USING i_row.CALL METHOD OF application 'ROWS' = rowsEXPORTING#1 = i_row.CALL METHOD OF rows 'DELETE'.ENDFORM. "delete_row*&---------------------------------------------------------------------* *& (24) Form add_comment*&---------------------------------------------------------------------* * 添加注释*----------------------------------------------------------------------* * -->I_ROW 行号* -->I_COL 列号* -->P_TEXT 注释内容*----------------------------------------------------------------------* FORM add_comment USING i_row i_col p_text.CALL METHOD OF application 'Cells' = cellEXPORTING#1 = i_row#2 = i_col.CALL METHOD OF cell 'AddComment' = cell1.CALL METHOD OF cell1 'Text'EXPORTING#1 = p_text.CALL METHOD OF cell1 'Shape' = font.SET PROPERTY OF font 'Width' = '20'.SET PROPERTY OF font 'Height' = '20'.ENDFORM. "add_comment*&---------------------------------------------------------------------* *& (25) Form hide_columns*&---------------------------------------------------------------------* * 隐藏列*----------------------------------------------------------------------* * -->COLNUM 输入列号*----------------------------------------------------------------------* FORM hide_columns USING colnum.CALL METHOD OF application 'COLUMNS' = columnsEXPORTING#1 = colnum.SET PROPERTY OF columns 'Hidden' = 'true'.ENDFORM. "hide_columns*&---------------------------------------------------------------------* *& (26) Form delete_columns*&---------------------------------------------------------------------* * 删除列*----------------------------------------------------------------------* * -->COLNUM 输入列号*----------------------------------------------------------------------* FORM delete_columns USING colnum.CALL METHOD OF application 'COLUMNS' = columnsEXPORTING#1 = colnum.CALL METHOD OF columns 'DELETE'.ENDFORM. "delete_columns*&---------------------------------------------------------------------**& (27) Form hide_row*&---------------------------------------------------------------------** 隐藏行*----------------------------------------------------------------------** -->I_ROW 输入行号*----------------------------------------------------------------------*FORM hide_row USING i_row.CALL METHOD OF application 'ROWS' = rowsEXPORTING#1 = i_row.SET PROPERTY OF rows 'Hidden' = 'true'.ENDFORM. "hide_row3 注意事项3.1 Sheet名称Sheet名为中文时,在调用sheet对象时往往会调用不到,用英文字母不存在该问题。
SAPABAP如何将内表与EXCEL导入、导出
SAPABAP如何将内表与EXCEL导入、导出将内表的数据导出到EXCELREPORT ZTEST08.DATA: BEGIN OF MY_DATA,MANDT LIKE USR02-MANDT,BNAME LIKE USR02-BNAME,ERDAT LIKE USR02-ERDAT,LTIME LIKE USR02-LTIME,END OF MY_DATA.DATA: IT_DATA LIKE MY_DATA OCCURS 0.SELECT USR02~MANDT USR02~BNAME USR02~ERDAT USR02~LTIMEAPPENDING TABLE IT_DATAFROM USR02UP to 100 rows.*排序SORT IT_DATA BY BNAME.INCLUDE OLE2INCL. "定义OLE变量DATA: EXCEL TYPE OLE2_OBJECT,SHEET TYPE OLE2_OBJECT,CELL TYPE OLE2_OBJECT,WORKBOOK TYPE OLE2_OBJECT,XLSNAME TYPE STRING VALUE 'C:\sap_usr02.xls',LINE TYPE I VALUE 0. "行号CREATE OBJECT EXCEL 'EXCEL.APPLICATION'."启动ExcelIF SY-SUBRC NE 0.WRITE: / '启动Excel失败。
'.STOP.ENDIF.CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK.SET PROPERTY OF EXCEL 'VISIBLE' = 1. "使excel 可视SET PROPERTY OF EXCEL 'SHEETSINNEWWORKBOOK' = 1. "如果是读取excel文件中的内容则是直接打开工作簿第一页CALL METHOD OF WORKBOOK 'ADD'. "例如:CALL METHOD OF EXCEL 'WORKSHEETS' = SHEET EXPORTING #1 = 1.LOOP AT IT_DATA INTO MY_DATA.LINE = LINE + 1. "Excel 中行号从1开始CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 1. "指定单元格,SET PROPERTY OF CELL 'VALUE' = MY_DATA-MANDT. "写入值CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 2. "指定单元格,SET PROPERTY OF CELL 'VALUE' = MY_DATA-BNAME. "写入值CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 =LINE #2 = 3. "指定单元格,SET PROPERTY OF CELL 'VALUE' = MY_DATA-ERDAT. "写入值CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 4. "指定单元格,SET PROPERTY OF CELL 'VALUE' = MY_DATA-LTIME. "写入值ENDLOOP.GET PROPERTY OF EXCEL 'ACTIVESHEET' = SHEET. "激活工作簿GET PROPERTY OF EXCEL 'ACTIVEWORKBOOK' = WORKBOOK. "激活工作区CALL METHOD OF WORKBOOK 'SAVEAS' EXPORTING #1 = XLSNAME #2 = 1. "将excel文件保存CALL METHOD OF WORKBOOK 'CLOSE'. "关闭工作区CALL METHOD OF EXCEL 'QUIT'. "退出excel*WRITE:/ XLSNAME,'DONE'. "退出成功,输出doneFREE OBJECT SHEET. "释放操作FREE OBJECT WORKBOOK.FREE OBJECT EXCEL.上传的EXCEL给内表赋值这个EXCEL上载直接读取行和列,并把行和列对应的值赋给内表。
SAP ABAP-DOI技术输出EXCEL文档(1)
DOI技术是ABAP程序编程中常用的一种技术,这种技术在使用中有许多不常用的技巧,灵活使用这种技巧可以让你在编程过程中如虎添翼,简化你的程序逻辑与代码复杂度。
本文告诉你如何全屏幕显示DOI输出的EXCEL文档。
创建一个空屏幕上,只在屏幕上激活PAI、PBO事件下的FUNCTIONMOUDLE。
干嘛非要在屏幕上放一个CONTAINER组件呢,固定后那么难看。
完全可以动态创建嘛!动态创建CL_GUI_CONTAINER这个类。
动态创建的好处在于,CONTAINER的大小不是固定的,完全随着屏幕的大小而创建。
注意检查屏幕是否支持对象的使用。
OAOR上传模版。
其实如果输出的EXCEL比较简单,就可以不使用模版。
本文先输出一个简单的EXCEL文件,不使用上传的EXCEL模版。
*&---------------------------------------------------------------------**& REPORT ZTRAINING26 ABAP DOI技术输出EXCEL文档*&*&---------------------------------------------------------------------**& CREATED BY XAVERY HSUEH(薛现军) ON 2015-07-26.*& **&---------------------------------------------------------------------*REPORT ZTRAINING26 NO STANDARD PAGE HEADING.************************************************************************** 声明数据库表DECLARATION OF DATABASE************************************************************************* TABLES:MARA. "物料主数据*调用相关对象使用的变量TYPE-POOLS:VRM, SBDST, SOI.************************************************************************** 定义结构类型DEFINE THE STRUCTURE'S TYPE *************************************************************************** 结果内表结构TYPES:BEGIN OF TY_RESULT,MATNR TYPE MARA-MATNR, "物料编号ERSDA TYPE MARA-ERSDA,ERNAM TYPE MARA-ERNAM,MTART TYPE MARA-MTART,MATKL TYPE MARA-MATKL,MEINS TYPE MARA-MEINS,PRDHA TYPE MARA-PRDHA,MAKTX TYPE MAKT-MAKTX,END OF TY_RESULT.TYPES: BEGIN OF TY_DOCU_DESCR,DOCUMENT_NAME(40),DOCUMENT_ID(64),END OF TY_DOCU_DESCR.* EXCEL的数据块TYPES:BEGIN OF TY_EXCEL,AREANO(2) TYPE N,BORDER TYPE C,TOP TYPE I,LEFT TYPE I,ROWS TYPE I,COLS TYPE I,CONTENT TYPE SOI_GENERIC_TABLE,END OF TY_EXCEL.************************************************************************* * 定义变量与内表DEFINE THE VARIANTS AND INTERNAL TABLES ************************************************************************** DATA:GT_RESULT TYPE TABLE OF TY_RESULT WITH HEADER LINE. "结果内表*@---------------------------------------------------------------------** SAP DESKTOP OFFICE INTEGRATION INTERFACESDATA: CL_CONTAINER TYPE REF TO CL_GUI_CONTAINER,CL_CONTROL TYPE REF TO I_OI_CONTAINER_CONTROL,CL_DOCU_PROXY TYPE REF TO I_OI_DOCUMENT_PROXY,CL_SPREADSHEET TYPE REF TO I_OI_SPREADSHEET,CL_ERROR TYPE REF TO I_OI_ERROR,CL_ERRORS TYPE REF TO I_OI_ERROR OCCURS 0 WITH HEADER LINE.* SPREADSHEET INTERFACE STRUCTURES FOR EXCEL DATA INPUTDATA:GS_CELLITEM TYPE SOI_GENERIC_ITEM,GS_RANGEITEM TYPE SOI_RANGE_ITEM,GT_RANGES TYPE SOI_RANGE_LIST,GV_INITIALIZED TYPE C,GV_RETCODE TYPE SOI_RET_STRING,GT_EXCEL_FORMAT TYPE SOI_FORMAT_TABLE,GS_FORMAT LIKE LINE OF GT_EXCEL_FORMAT.DATA:GT_IMT_TAB TYPE TABLE OF TY_EXCEL,GS_IMT_TAB TYPE TY_EXCEL,GT_CONTENT TYPE SOI_GENERIC_TABLE,GS_CONTENT TYPE SOI_GENERIC_ITEM,GV_MACRO TYPE TEXT100,GV_SHEET TYPE CHAR20,GV_CELL_FIT TYPE C VALUE 'X'.DATA:GV_APP TYPE VRM_ID,GT_APPLIST TYPE VRM_VALUES,GV_EXCEL TYPE TEXT80 VALUE 'EXCEL.SHEET', "EXCEL的表单GV_DOCU_TYPE TYPE TEXT80,GV_URL(256) TYPE C,GV_HAS_ACTIVEX TYPE C,GV_HAS TYPE I,GV_LINE TYPE I,GV_COL TYPE I,"字段所在的列数GV_ROW TYPE I.************************************************************************** 宏定义DEFINE THE MACRO ************************************************************************** DEFINE M_VALUE.CLEAR &1.&1-KEY = &2.&1-TEXT = &3.APPEND &1.END-OF-DEFINITION.* 将数据对应到EXCEL的单元格,并添加到内表中DEFINE M_EXCEL_CELL.CLEAR GS_CONTENT.GV_COL = GV_COL + 1.GS_CONTENT-ROW = GV_ROW.GS_CONTENT-COLUMN = GV_COL.GS_CONTENT-VALUE = &1.SHIFT GS_CONTENT-VALUE LEFT DELETING LEADING SPACE.APPEND GS_CONTENT TO GT_CONTENT.END-OF-DEFINITION.************************************************************************* * 选择屏幕CUSTOMIZE THE SELECTION-SCREEN ************************************************************************* *SELECTION-SCREEN BEGIN OF BLOCK XAVERY WITH FRAME TITLE TEXT-001.SELECT-OPTIONS:S_MATNR FOR MARA-MATNR MEMORY ID MAT. "物料编号SELECTION-SCREEN END OF BLOCK XAVERY.************************************************************************* * 执行程序事件EXECUTING THE PROGRAM'S EVENTS************************************************************************** INITIALIZATION.START-OF-SELECTION.PERFORM SUB_QUERY_MARA.END-OF-SELECTION.PERFORM SUB_PROCESS_EXCEL.CALL SCREEN 0100.*&---------------------------------------------------------------------**& FORM SUB_QUERY_MARA*&---------------------------------------------------------------------** 从物料主数据表MARA中取数*----------------------------------------------------------------------*FORM SUB_QUERY_MARA .* 取物料主数据SELECT MARA~MATNRMARA~ERSDAMARA~ERNAMMARA~MTARTMARA~MATKLMARA~MEINSMARA~PRDHAMAKT~MAKTXFROM MARA INNER JOIN MAKT ON MARA~MATNR = MAKT~MATNRINTO CORRESPONDING FIELDS OF TABLE GT_RESULTWHERE MARA~MATNR IN S_MATNR ANDMAKT~SPRAS = SY-LANGU.ENDFORM.************************************************************************* * DIALOG MODULES PBO AND PAI************************************************************************ MODULE STATUS_0100 OUTPUT.SET PF-STATUS 'XAVERY_HSUEH'.SET TITLEBAR 'TIT_100' WITH '活动界面示例'.PERFORM SUB_CREATE_BASIC_OBJECT.PERFORM SUB_SET_CELL_VALUE.PERFORM SUB_SET_SHEET.ENDMODULE. "STATUS_0100 OUTPUT*&---------------------------------------------------------------------**& DIALOG MODULES PAI*&---------------------------------------------------------------------*MODULE USER_COMMAND_0100 INPUT.LEAVE TO SCREEN 0.ENDMODULE. "USER_COMMAND_0100 INPUT*&---------------------------------------------------------------------**& FORM SUB_CREATE_BASIC_OBJECT*&---------------------------------------------------------------------** 创建相关的对象*----------------------------------------------------------------------*FORM SUB_CREATE_BASIC_OBJECT .CALL FUNCTION 'GUI_HAS_ACTIVEX'IMPORTINGRETURN = GV_HAS_ACTIVEX.IF GV_HAS_ACTIVEX IS INITIAL.MESSAGE E007(DEMOOFFICEINTEGRATIO).ENDIF.CHECK GV_INITIALIZED IS INITIAL.* FIRST GET THE SAP DOI I_OI_CONTAINER_CONTROL INTERFACECALL METHOD C_OI_CONTAINER_CONTROL_CREATOR=>GET_CONTAINER_CONTROL IMPORTINGCONTROL = CL_CONTROLERROR = CL_ERROR.* CHECK NO ERRORS OCCUREDCALL METHOD CL_ERROR->RAISE_MESSAGEEXPORTING TYPE = 'E'.* CONTIANER初始化CL_CONTAINER = CL_GUI_CONTAINER=>SCREEN0.* INITIALIZE THE SAP DOI CONTAINER, TELL IT TO RUN IN THE CONTAINER* SPECIFIED ABOVE AND TELL IT TO RUN EXCEL IN-PLACECALL METHOD CL_CONTROL->INIT_CONTROLEXPORTING R3_APPLICATION_NAME = 'RBASIS' "#EC NOTEXT INPLACE_ENABLED = 'X'INPLACE_SCROLL_DOCUMENTS = 'X'PARENT = CL_CONTAINERREGISTER_ON_CLOSE_EVENT = 'X'REGISTER_ON_CUSTOM_EVENT = 'X'NO_FLUSH = 'X'IMPORTINGERROR = CL_ERRORS.* SAVE ERROR OBJECT IN COLLECTIONAPPEND CL_ERRORS.CALL METHOD CL_GUI_CFW=>DISPATCH.* ASK THE SAP DOI CONTAINER FOR A I_OI_DOCUMENT_PROXY FOR EXCEL CALL METHOD CL_CONTROL->GET_DOCUMENT_PROXYEXPORTINGDOCUMENT_TYPE = 'EXCEL.SHEET'NO_FLUSH = 'X'*REGISTER_CONTAINER = 'X'IMPORTINGDOCUMENT_PROXY = CL_DOCU_PROXYERROR = CL_ERRORS.APPEND CL_ERRORS.* 创建EXCEL文档CALL METHOD CL_DOCU_PROXY->CREATE_DOCUMENTEXPORTINGOPEN_INPLACE = 'X'NO_FLUSH = 'X'DOCUMENT_TITLE = '明细表'IMPORTINGERROR = CL_ERRORS.* 检查标识: WHETHER INTERFACE IS SUPPORTED FOR THIS DOC. TYPECALL METHOD CL_DOCU_PROXY->HAS_SPREADSHEET_INTERFACE EXPORTINGNO_FLUSH = 'X'IMPORTINGIS_AVAILABLE = GV_HASERROR = CL_ERRORS.APPEND CL_ERRORS.CALL METHOD CL_DOCU_PROXY->GET_SPREADSHEET_INTERFACE EXPORTINGNO_FLUSH = 'X'IMPORTINGSHEET_INTERFACE = CL_SPREADSHEETERROR = CL_ERRORS.APPEND CL_ERRORS.FREE CL_ERRORS.GV_INITIALIZED = 'X'.ENDFORM.*&---------------------------------------------------------------------**& FORM SUB_PROCESS_EXCEL*&---------------------------------------------------------------------** 处理结果内表*----------------------------------------------------------------------*FORM SUB_PROCESS_EXCEL .* 将表格抬头数据添加到内表CLEAR GS_IMT_TAB.REFRESH GT_CONTENT.GV_COL = 0.GV_ROW = 1.M_EXCEL_CELL '物料编码'.M_EXCEL_CELL '物料类型'.M_EXCEL_CELL '物料组'.M_EXCEL_CELL '物料描述'.GS_IMT_TAB-AREANO = 1.GS_IMT_TAB-BORDER = 'X'.GS_IMT_TAB-TOP = 1. "输出起始单元格行GS_IMT_TAB-LEFT = 1. "输出起始单元格列GS_IMT_TAB-ROWS = 1.GS_IMT_TAB-COLS = 4.GS_IMT_TAB-CONTENT = GT_CONTENT.APPEND GS_IMT_TAB TO GT_IMT_TAB.* 将表格数据添加到内表REFRESH GT_CONTENT.GV_ROW = 0.LOOP AT GT_RESULT.GV_COL = 0.GV_ROW = GV_ROW + 1.M_EXCEL_CELL GT_RESULT-MATNR.M_EXCEL_CELL GT_RESULT-MTART.M_EXCEL_CELL GT_RESULT-MATKL.M_EXCEL_CELL GT_RESULT-MEINS.M_EXCEL_CELL GT_RESULT-MAKTX.ENDLOOP.GV_LINE = GV_ROW.GS_IMT_TAB-AREANO = 2.GS_IMT_TAB-BORDER = 'X'.GS_IMT_TAB-TOP = 2. "数据从第二行开始GS_IMT_TAB-LEFT = 1. "数据从第一列开始GS_IMT_TAB-ROWS = GV_ROW.GS_IMT_TAB-COLS = GV_COL.GS_IMT_TAB-CONTENT = GT_CONTENT.APPEND GS_IMT_TAB TO GT_IMT_TAB.CLEAR GS_IMT_TAB.REFRESH GT_CONTENT.GV_COL = 0.GV_ROW = 1.M_EXCEL_CELL '执行日期'.M_EXCEL_CELL SY-DATUM.M_EXCEL_CELL '登录用户'.M_EXCEL_CELL SY-UNAME.GS_IMT_TAB-AREANO = 3.GS_IMT_TAB-TOP = GV_LINE + 2. "输出起始单元格行GS_IMT_TAB-LEFT = 1. "输出起始单元格列GS_IMT_TAB-ROWS = 1. "共N行N = 1GS_IMT_TAB-COLS = 4. "共N列N = 4GS_IMT_TAB-CONTENT = GT_CONTENT.APPEND GS_IMT_TAB TO GT_IMT_TAB.ENDFORM. " SUB_PROCESS_EXCEL*&---------------------------------------------------------------------**& FORM SUB_SET_CELL_VALUE*&---------------------------------------------------------------------** 设置EXCEL文档的单元格的内容*----------------------------------------------------------------------* FORM SUB_SET_CELL_VALUE .LOOP AT GT_IMT_TAB INTO GS_IMT_TAB.* 给EXCEL添加数据块CALL METHOD CL_SPREADSHEET->INSERT_RANGE_DIMEXPORTINGNAME = 'CELL'NO_FLUSH = 'X'TOP = GS_IMT_TAB-TOPLEFT = GS_IMT_TAB-LEFTROWS = GS_IMT_TAB-ROWSCOLUMNS = GS_IMT_TAB-COLSIMPORTINGERROR = CL_ERRORS.APPEND CL_ERRORS.* 设定输出数据的内容REFRESH GT_RANGES.GS_RANGEITEM-NAME = 'CELL'.GS_RANGEITEM-COLUMNS = GS_IMT_TAB-COLS.GS_RANGEITEM-ROWS = GS_IMT_TAB-ROWS.GS_RANGEITEM-CODE = 4.APPEND GS_RANGEITEM TO GT_RANGES.* SET DATACALL METHOD CL_SPREADSHEET->SET_RANGES_DATA EXPORTINGRANGES = GT_RANGESCONTENTS = GS_IMT_TAB-CONTENTNO_FLUSH = 'X'IMPORTINGERROR = CL_ERRORS.APPEND CL_ERRORS.* 设置FRAME.IF GS_IMT_TAB-BORDER = 'X'.CALL METHOD CL_SPREADSHEET->SET_FRAMEEXPORTINGRANGENAME = 'CELL'TYP = '127'COLOR = '1'NO_FLUSH = 'X'IMPORTINGERROR = CL_ERRORS.ENDIF.ENDLOOP.ENDFORM.*&---------------------------------------------------------------------* *& FORM SUB_SET_SHEET*&---------------------------------------------------------------------* * 设置EXCEL文档的SHEET页的格式*----------------------------------------------------------------------* FORM SUB_SET_SHEET .* 自动调整单元格宽度IF GV_CELL_FIT = 'X'.CALL METHOD CL_SPREADSHEET->FIT_WIDESTEXPORTINGNAME = SPACENO_FLUSH = 'X'.ENDIF.* 修改WORK SHEET 的名字GV_SHEET = '物料主数据'.CALL METHOD CL_SPREADSHEET->SET_SHEET_NAME EXPORTINGNEWNAME = GV_SHEETOLDNAME = 'SHEET1'IMPORTINGERROR = CL_ERRORS.ENDFORM. " SUB_SET_SHEET。
Excel中的数据导入与导出方法详解
Excel中的数据导入与导出方法详解Excel是一款广泛使用的电子表格软件,它在数据处理和分析方面具有强大的功能。
在Excel中,数据的导入和导出是常见的操作。
本文将详细介绍Excel中的数据导入和导出方法,帮助读者更好地利用Excel进行数据处理。
一、数据导入方法1. 手动输入数据最简单的数据导入方式是手动输入数据。
在Excel中,只需选中目标单元格,然后直接输入数据即可。
这种方法适用于数据量较小的情况,但对于大量数据的导入来说,效率较低。
2. 复制粘贴数据复制粘贴是一种常用的数据导入方法。
首先,将源数据复制到剪贴板中,然后在Excel中选中目标单元格,使用Ctrl+V快捷键或右键点击目标单元格并选择粘贴选项进行数据粘贴。
这种方法适用于从其他文件或网页中复制数据到Excel中。
3. 导入文本文件Excel还提供了导入文本文件的功能。
在Excel中,选择“数据”选项卡,然后点击“从文本”按钮,在弹出的对话框中选择目标文本文件并按照向导进行导入。
这种方法适用于从外部文本文件中导入数据。
4. 导入数据库数据如果需要导入数据库中的数据,可以使用Excel的“数据连接”功能。
在Excel 中,选择“数据”选项卡,然后点击“从其他源”按钮,选择“从SQL Server”或其他数据库类型,并按照向导进行导入。
这种方法适用于从数据库中导入大量数据。
二、数据导出方法1. 导出为文本文件将Excel中的数据导出为文本文件是一种常见的操作。
在Excel中,选择要导出的数据范围,然后选择“文件”选项卡,点击“另存为”按钮,在弹出的对话框中选择目标文件格式为文本文件,并按照向导进行导出。
这种方法适用于将Excel中的数据导出到其他软件或系统中使用。
2. 导出为CSV文件CSV(逗号分隔值)文件是一种常用的数据交换格式。
在Excel中,选择要导出的数据范围,然后选择“文件”选项卡,点击“另存为”按钮,在弹出的对话框中选择目标文件格式为CSV文件,并按照向导进行导出。
SAP日记-ABAP导出EXCEL的两种方式(OLE,DOI)
SAP日记——ABAP导出EXCEL的两种方法一、OLEABAP通过OLE的方式导出EXCEL,由于数据时一条一条的生成,速度较慢,不适合内表数据较多的场景使用,但是其实现起来比较简单。
REPORT ZTEST1.TABLES: ZWYM. “自定义的一张表TYPE-POOLS:SLIS,ole2. “需要用到OLE2DATA: excel TYPE ole2_object,workbook TYPE ole2_object,sheet TYPE ole2_object,cell TYPE ole2_object.DATA: application TYPE ole2_object,book TYPE ole2_object,books TYPE ole2_object.DATA: ole_book TYPE ole2_object.DATA: FILENAME(100) TYPE C VALUE 'D:\data\test.xls'. “定义文件导出的路径"DATA: GT_ITAB LIKE TABLE OF ZWYM WITH HEADER LINE.DATA:BEGIN OF GT_ITAB OCCURS 0, “定义内表INCLUDE STRUCTURE ZWYM.END OF GT_ITAB.SELECTION-SCREEN:BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. “查询条件SELECT-OPTIONS:S_MATNR FOR ZWYM-MATNR.SELECTION-SCREEN:END OF BLOCK B1.START-OF-SELECTION.PERFORM GET_DATA.END-OF-SELECTION.PERFORM gen_excel.FORM GET_DATA.SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_ITAB FROM ZWYM W HERE MATNR IN S_MATNR.ENDFORM. “GET_DATA.FORM gen_excel.CREATE OBJECT excel 'EXCEL.APPLICATION'.IF sy-subrc NE 0 .MESSAGE '不能生成EXCEL对象,检查系统是否安装 MS EXCEL。
ABAP程序完成EXCEL文件的导入导出
ABAP程序完成EXCEL文件的导入导出今天主要学习了使用微软的OLE技术,用ABAP程序完成excel 文件的导入导出处理。
(一)常用的OLE函数:CALL METHOD , CALL OBJECT , FREE OBJECT , GET PROPERTY , SET PROPERTY.设属性:SET PROPERTY OF <对象><属性> = <value>取属性:GET PROPERTY OF <对象><属性> = 变量调用方法:CALL METHOD OF <对象><METHOD><value> EXPORTING #<PARA> = <VALUE>.输出可以是多个参数。
释放对象是:FREE OBJECT<对象>.2.在程序中下载模板*下载EXCEL模板FORMFORM temp_excel_get.DATA: lo_objdata LIKE wwwdatatab,lo_mime LIKE w3mime,lc_filename TYPE string VALUE'test01' ,lc_fullpath TYPE string VALUE'D:\test\' ,lc_path TYPE string VALUE'D:\test\' ,ls_destination LIKE rlgrap-filename,ls_objnam TYPE string,li_rc LIKE sy-subrc,ls_errtxt TYPE string.DATA:p_objid TYPE wwwdatatab-objid,p_dest LIKE sapb-sappfad.p_objid = 'ZTEST.XLS'. “此处为EXCEL模板名称CALL METHOD cl_gui_frontend_services=>file_save_dialog ”调用保存对话框EXPORTINGdefault_extension = 'XLS'default_file_name = lc_filenameCHANGINGfilename = lc_filenamepath = lc_pathfullpath = lc_fullpathEXCEPTIONScntl_error = 1error_no_gui = 2not_supported_by_gui = 3OTHERS = 4.IF lc_fullpath = ''.MESSAGE '不能打开excel' TYPE 'E'.ENDIF.IF sy-subrc = 0.p_dest = lc_fullpath.* concatenate p_objid '.XLS' into ls_objnam.CONDENSE ls_objnam NO-GAPS.SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdataWHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid.*检查表wwwdata中是否存在所指定的模板文件IF sy-subrc NE 0 OR lo_objdata-objid EQ space.“如果不存在,则给出错误提示CONCATENATE '模板文件' ls_objnam '不存在' INTO ls_errtxt.MESSAGE ls_errtxt TYPE 'I'.ENDIF.ls_destination = p_dest. ”保存路径*如果存在,调用DOWNLOAD_WEB_OBJECT 函数下载模板到路径下CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'EXPORTINGkey = lo_objdatadestination = ls_destinationIMPORTINGrc = li_rc.IF li_rc NE 0.CONCATENATE '模板文件:' ls_objnam '下载失败' INTO ls_errtxt.MESSAGE ls_errtxt TYPE 'E'.ENDIF.fname = ls_destination.ENDIF.ENDFORM. "fm_excel3.下载完模板后,打开模板文件,填入数据CREATE OBJECT excel 'EXCEL.APPLICATION'. "Create EXCEL OBJECTIF sy-subrc NE 0.EXIT.ENDIF.SET PROPERTY OF excel 'Visible' = 0. "1/0 是否显示EXCELCALL METHOD OF excel 'Workbooks' = workbook.CALL METHOD OF workbook 'Open'EXPORTING#1 = fname. ."打开上面下载路径下的excel文件CALL METHOD OF excel 'Sheets' = sheetEXPORTINGCALL METHOD OF sheet 'Select' .CALL METHOD OF sheet 'ACTIVATE'. “sheet 激活SET PROPERTY OF sheet 'NAME' = sheetname. “设定sheet 名称*此处假设内表itab 中已经存在需要写入excel中的数据*并且假如从模板的第7行开始插入数据LOOP AT itab INTO wa.tab = wa-tabix + 6.*在excel中插入一行PERFORM excel_row_insert USING sheet tab 1.*填充所插入行的每个单元格的数据PERFORM fill_range USING tab 1 wa-t01.PERFORM fill_range USING tab 2 wa-t02.PERFORM fill_range USING tab 3 wa-t03.PERFORM fill_range USING tab 4 wa-t04.PERFORM fill_range USING tab 5 wa-t05.PERFORM fill_range USING tab 6 wa-t06.ENDLOOP.*设置EXCEL中所插入的数据行边框线格式为黑色有边框bod = tab.CONDENSE bod NO-GAPS.CONCATENATE 'A6:F' bod INTO bod.PERFORM borderrange USING excel bod.PERFORM sub_excel_save."保存excel数据*&---------------------------------------------------------------------**& Form sub_excel_save*&---------------------------------------------------------------------**----------------------------------------------------------------------*FORM sub_excel_save.GET PROPERTY OF excel 'ActiveSheet' = sheet. “获取活动SHEETFREE OBJECT sheet.FREE OBJECT workbook.GET PROPERTY OF excel 'ActiveWorkbook' = workbook.CALL METHOD OF workbook 'SAVE'.SET PROPERTY OF excel 'Visible' = 1. "是否显示EXCEL 此处显示不退出* SET PROPERTY OF excel 'Visible' = 1.* CALL METHOD OF workbook 'CLOSE'.* CALL METHOD OF excel 'QUIT'. 注释部分为不显示直接退出FREE OBJECT sheet.FREE OBJECT workbook.FREE OBJECT excel.ENDFORM. "save_book*&---------------------------------------------------------------------**& 向excel中的指定行插入N行*&---------------------------------------------------------------------*FORM excel_row_insert USING lcobj_sheetlc_rowlc_count.DATA lc_range TYPE ole2_object.DATA h_borders TYPE ole2_object.DO lc_count TIMES.CALL METHOD OF lcobj_sheet 'Rows' = lc_rangeEXPORTING #1 = 6.CALL METHOD OF lc_range 'Copy'. “COPY第6行插入一个新行CALL METHOD OF lcobj_sheet 'Rows' = lc_rangeEXPORTING #1 = lc_row.CALL METHOD OF lc_range 'Insert'.CALL METHOD OF lc_range 'ClearContents'. "是否需要清空CellENDDO.ENDFORM. "excel_row_insert*&---------------------------------------------------------------------**& Form fill_range*&---------------------------------------------------------------------** text 填充EXCEL 单元格*----------------------------------------------------------------------** -->VALUE(F_ROW) text* -->VALUE(F_COL) text* -->VALUE(F_VALUE) text*----------------------------------------------------------------------*FORM fill_range USING value(f_row)value(f_col)value(f_value).DATA:row TYPE i,col TYPE i.row = f_row.col = f_col.CALL METHOD OF excel 'CELLS' = cellEXPORTING#1 = row#2 = col.SET PROPERTY OF cell 'VALUE' = f_value.ENDFORM. "fill_range*&---------------------------------------------------------------------**& Form borderrange*&---------------------------------------------------------------------** text:设置EXCEL中所插入的数据行边框线格式*----------------------------------------------------------------------** -->LCOBJ_EXCEL text* -->RANGE text*----------------------------------------------------------------------*FORM borderrange USING lcobj_excelrange .DATA: lc_cell TYPE ole2_object ,lc_borders TYPE ole2_object .CALL METHOD OF lcobj_excel 'RANGE' = lc_cellEXPORTING#1 = range.DO 4 TIMES .CALL METHOD OF lc_cell 'BORDERS' = lc_bordersEXPORTING #1 = sy-index.SET PROPERTY OF lc_borders 'LineStyle' = '1'.SET PROPERTY OF lc_borders 'WEIGHT' = 2. "4=maxSET PROPERTY OF lc_borders 'ColorIndex' = '1'.ENDDO.FREE OBJECT lc_borders.FREE OBJECT lc_cell.ENDFORM. "borderrange。
sap abap以自定义excel的方式输出alv文件
*&---------------------------------------------------------------------**& Report YT_TEST008*&*&---------------------------------------------------------------------**&sap 以excel的方式输出文件*&*&---------------------------------------------------------------------*REPORT YT_TEST008.TYPE-POOLS ole2 .DATA: v_excel TYPE ole2_object,v_book TYPE ole2_object,v_cell TYPE ole2_object,v_range TYPE ole2_object,v_font TYPE ole2_object,v_color TYPE ole2_object,v_column TYPE ole2_object.DATA: BEGIN OF it_stock OCCURS 0,werks LIKE mard-werks,name1 LIKE t001w-name1, "地点名称lgort LIKE mard-lgort, "库位matnr LIKE mara-matnr, "商品编码maktx LIKE makt-maktx, "商品名称labst LIKE mard-labst, "当前库存END OF it_stock.PARAMETER: p_werks LIKE mard-werks DEFAULT '3010'. "'2021'.* 通过宏来设置指定单元格的内容和字体DEFINE fill_cell.call method of v_excel 'CELLS' = v_cell "单元格位置exporting#1 = &1 "行#2 = &2 . "列set property of v_cell 'value' = &3. "单元格内容call method of v_cell 'FONT' = v_font. "字体方法set property of v_font 'Bold' = &4. "粗体set property of v_font 'size' = &5. "字体大小END-OF-DEFINITION.START-OF-SELECTION.*将数据抓到内表中SELECT a~werksb~name1a~lgorta~matnrc~maktxa~labstINTO TABLE it_stockFROM mard AS aINNER JOIN t001w AS b ON a~werks = b~werksINNER JOIN makt AS c ON a~matnr = c~matnr AND c~spras = sy-la nguWHERE a~werks = p_werks. " AND a~lgort = '11R1' .*创建Excel对象CREATE OBJECT v_excel 'EXCEL.APPLICATION'. "EXCEL 应用CALL METHOD OF v_excel 'WORKBOOKS' = v_book. "new workbooksCALL METHOD OF v_book 'ADD'. "增加*使excel 可视SET PROPERTY OF v_excel 'VISIBLE' = 1.fill_cell 1 1 '当前商品可用库存状态' 1 18."水平对齐方式居中-4108 左对齐-4131 右对齐-4152SET PROPERTY OF v_cell 'HorizontalAlignment' = -4108. "水平对齐方式居中SET PROPERTY OF v_font 'UNDERLINE' = 2.*设置表头fill_cell 2 1 '地点' 1 10.fill_cell 2 2 '地点名称' 1 10.fill_cell 2 3 '库位' 1 10.fill_cell 2 4 '物料' 1 10.fill_cell 2 5 '物料名称' 1 10.fill_cell 2 6 '当前库存' 1 10.*选中标题所在的单元格并合并CALL METHOD OF v_excel 'Range' = v_rangeEXPORTING#1 = 'A1'#2 = 'F1' . "如果这个列是动态的,如何用字母表示?CALL METHOD OF v_range 'select'.SET PROPERTY OF v_range 'MergeCells' = 1. "合并单元格FIELD-SYMBOLS:<wa> TYPE ANY,<field> TYPE ANY.DATA: lv_txt(50) TYPE c.DATA: row_i TYPE i,col_i TYPE i.*从内表循环数据,按顺序填到单元格之中LOOP AT it_stock ASSIGNING <wa>.row_i = sy-tabix + 2. "从第三行开始DO 6 TIMES. "共6列col_i = sy-index.ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <field>. lv_txt = <field>.fill_cell row_i col_i lv_txt 0 10.IF col_i = 6 AND lv_txt <= 0.*可用程序为0时,在Excel中将该单元格标识为黄色CALL METHOD OF v_cell 'INTERIOR' = v_color.SET PROPERTY OF v_color 'ColorIndex' = 6.ENDIF.ENDDO.ENDLOOP.*将Excel单元格宽度按实际文本长度来设置CALL METHOD OF v_excel 'Columns' = v_column.CALL METHOD OF v_column 'autofit'.*设置Excel为可见SET PROPERTY OF v_excel 'visible' = 1.。
SAP日记-ABAP导出EXCEL的两种方式(OLE,DOI)
SAP日记——ABAP导出EXCEL的两种方法一、OLEABAP通过OLE的方式导出EXCEL,由于数据时一条一条的生成,速度较慢,不适合内表数据较多的场景使用,但是其实现起来比较简单。
REPORT ZTEST1.TABLES: ZWYM. “自定义的一张表TYPE-POOLS:SLIS,ole2. “需要用到OLE2DATA: excel TYPE ole2_object,workbook TYPE ole2_object,sheet TYPE ole2_object,cell TYPE ole2_object.DATA: application TYPE ole2_object,book TYPE ole2_object,books TYPE ole2_object.DATA: ole_book TYPE ole2_object.DATA: FILENAME(100) TYPE C VALUE 'D:\data\test.xls'. “定义文件导出的路径"DATA: GT_ITAB LIKE TABLE OF ZWYM WITH HEADER LINE.DATA:BEGIN OF GT_ITAB OCCURS 0, “定义内表INCLUDE STRUCTURE ZWYM.END OF GT_ITAB.SELECTION-SCREEN:BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. “查询条件SELECT-OPTIONS:S_MATNR FOR ZWYM-MATNR.SELECTION-SCREEN:END OF BLOCK B1.START-OF-SELECTION.PERFORM GET_DATA.END-OF-SELECTION.PERFORM gen_excel.FORM GET_DATA.SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_ITAB FROM ZWYM W HERE MATNR IN S_MATNR.ENDFORM. “GET_DATA.FORM gen_excel.CREATE OBJECT excel 'EXCEL.APPLICATION'.IF sy-subrc NE 0 .MESSAGE '不能生成EXCEL对象,检查系统是否安装 MS EXCEL。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
使用EXCEL导出数据要完成使用EXCEL导出数据,基本分为两布:1 建立excel模版2 在程序中整理要输出的数据,并完成最终的输出1 建立excel模版1.1 在本地建立一个excel设置模版的格式,在本例中,模版格式如下:1.2 使用Tcode:OROA,将模版导入SAP系统1.2.1 输入Tcode:OROA注:其他字段可以不填1.2.2 导入模版1.2.2.1 导入模版Create——>standard.doc.types ——> table template1.2.2.2 选择要导入的模版1.2.2.3 输入模版信息——描述和语言1.2.2.5 导入模版之后,在屏幕左上角会添加模版2. 程序中整理数据,并调用模版输出2.1 整理数据,并调用屏幕SELECT * FROM zlhjtestINTO CORRESPONDING FIELDS OF TABLE l_itab_stu. CALL SCREEN0100.2.2 输出屏幕2.2.1 输出屏幕之前,调用模版SET PF-STATUS 'STAN1'.PERFORM create_basic_objects USING'''''''' document_name. PERFORM output_to_excel.2.2.2 输出屏幕之后,根据用户选择做出反应实现这些功能的的代码,如下:REPORT zlhjtest_abavn.TYPE-POOLS:slis,vrm,soi.TYPES:BEGIN OF l_hh,BSART LIKE T161-BSART,batxt TYPE t161t-batxt,END OF l_hh.DATA: ok_code TYPE sy-ucomm,save_ok TYPE sy-ucomm.DATA: flag TYPE c,initialized TYPE c,item_url(256) TYPE c,app TYPE vrm_id,excel(80) VALUE'Excel.Sheet',v_title1(60) TYPE c VALUE'制作人:李红娟'.DATA: rangeitem TYPE soi_range_item,ranges TYPE soi_range_list.DATA: excel_input TYPE soi_generic_table,excel_input_wa TYPE soi_generic_item.DATA: document_type(80) TYPE c,container TYPE REF TO cl_gui_custom_container,control TYPE REF TO i_oi_container_control,document TYPE REF TO i_oi_document_proxy,spreadshee TYPE REF TO i_oi_spreadsheet,error TYPE REF TO i_oi_error,errors TYPE REF TO i_oi_errorOCCURS0WITH HEADER LINE. DATA: l_itab_stu TYPE TABLE OF t161t,l_wa_stu TYPE t161t.CONSTANTS:document_name(30) VALUE'学生信息表', "inplace VALUE'X'.1. 选择数据SELECT * FROM zlhjtestINTO CORRESPONDING FIELDS OF TABLE l_itab_stu.2 调用输出屏幕CALL SCREEN0100.*******************下面是子程序******************************&---------------------------------------------------------------------**& Module STATUS_0100 OUTPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE status_0100 OUTPUT.SET PF-STATUS 'STAN1'.PERFORM create_basic_objects USING'''''''' document_name. PERFORM output_to_excel.ENDMODULE. " STATUS_0100 OUTPUT*&---------------------------------------------------------------------**& Form create_basic_objects*&---------------------------------------------------------------------** text*----------------------------------------------------------------------** -->P_0130 text* -->P_0131 text* -->P_0132 text* -->P_0133 text* -->P_DOCUMENT_NAME text*----------------------------------------------------------------------*FORM create_basic_objects USING p_app_namep_classnamep_classtypep_obj_keyp_docname.DATA: l_app_name(200).CHECK initialized IS INITIAL.CALL METHODc_oi_container_control_creator=>get_container_controlIMPORTINGcontrol = controlerror = error.CALL METHOD error->raise_messageEXPORTINGtype = 'E'.CREATE OBJECT containerEXPORTINGcontainer_name = 'CONTAINER_NAME'. "输入屏幕0100的"容器“元素名IF p_app_name IS INITIAL.l_app_name = 'TEST1'.ELSE.l_app_name = p_app_name.ENDIF.CALL METHOD control->init_controlEXPORTINGr3_application_name = l_app_nameinplace_enabled = inplaceinplace_scroll_documents = 'X'"parent = containerregister_on_close_event = 'X'register_on_custom_event = 'X'no_flush = 'X'IMPORTINGerror = errors.APPEND errors.* save error object in collectionAPPEND errors.CLEAR item_url.DATA: bds_instance TYPE REF TO cl_bds_document_set. DATA: doc_signature TYPE sbdst_signature,wa_doc_signature LIKE LINE OF doc_signature,doc_components TYPE sbdst_components,doc_uris TYPE sbdst_uri,wa_doc_uris LIKE LINE OF doc_uris.*以下三个值为Tcode:OAOR里面新建模板文件的参数********************************DATA:doc_classname TYPE sbdst_classname VALUE'PICTURES', doc_classtype TYPE sbdst_classtype VALUE'OT',doc_object_key TYPE sbdst_object_keyVALUE'ZLHJEXCELTEMPLATE'."定义对象的key wa_doc_signature-prop_name = 'DESCRIPTION'.app = 'excel'.IF app = 'excel'.document_type = excel.wa_doc_signature-prop_value = p_docname.ELSE.ENDIF.APPEND wa_doc_signature TO doc_signature.CREATE OBJECT bds_instance.CALL METHOD bds_instance->get_infoEXPORTINGclassname = doc_classnameclasstype = doc_classtypeobject_key = doc_object_keyCHANGINGcomponents = doc_componentssignature = doc_signature.CALL METHOD bds_instance->get_with_urlEXPORTINGclassname = doc_classnameclasstype = doc_classtypeobject_key = doc_object_keyCHANGINGuris = doc_urissignature = doc_signature.FREE bds_instance.READ TABLE doc_uris INTO wa_doc_uris INDEX1.item_url = wa_doc_uris-uri.* ask the SAP DOI container for a i_oi_document_proxy for Excel CALL METHOD control->get_document_proxyEXPORTINGdocument_type = document_typeno_flush = 'X'IMPORTINGdocument_proxy = documenterror = errors.APPEND errors.* open a document saved in business document service. CALL METHOD document->open_documentEXPORTINGopen_inplace = inplacedocument_url = item_url.DATA: has TYPE i.CALL METHOD document->has_spreadsheet_interface EXPORTINGno_flush = ''IMPORTINGis_available = haserror = errors.APPEND errors.CALL METHOD document->get_spreadsheet_interface EXPORTINGno_flush = ' 'IMPORTINGsheet_interface = spreadsheeterror = errors.APPEND errors.* Activate sheet 1CALL METHOD spreadsheet->select_sheetEXPORTINGname = '表整理'* NO_FLUSH = ' 'IMPORTINGerror = errors.APPEND errors.LOOP AT errors.CALL METHOD errors->raise_messageEXPORTINGtype = 'E'.ENDLOOP.FREE errors.initialized = 'X'.ENDFORM. " create_basic_objects*&---------------------------------------------------------------------* *& Form output_to_excel*&---------------------------------------------------------------------* * text*----------------------------------------------------------------------* * --> p1 text* <-- p2 text*----------------------------------------------------------------------* FORM output_to_excel .perform fill_cell using21 v_title1.DATA: num TYPE i.num = 5.LOOP AT l_itab_stu INTO l_wa_stu .PERFORM fill_cell USING num 1 num .PERFORM fill_cell USING num 2 l_wa_stu-SPRAS .PERFORM fill_cell USING num 3 l_wa_stu-BSART.PERFORM fill_cell USING num 4 l_wa_stu-batxt.* PERFORM fill_cell USING num 2 l_wa_stu-sno .* PERFORM fill_cell USING num 3 l_wa_stu-sname. * PERFORM fill_cell USING num 4 l_wa_stu-ssex.num = num + 1.ENDLOOP.ENDFORM. " output_to_excel*&---------------------------------------------------------------------* *& Form fill_cell*&---------------------------------------------------------------------* * text*----------------------------------------------------------------------* * -->P_2 text* -->P_1 text* -->P_V_TITLE1 text*----------------------------------------------------------------------* FORM fill_cell USING i j value.DATA: columns_number TYPE i,rows_number TYPE i.columns_number = 1.rows_number = 1.CALL METHOD spreadsheet->insert_range_dimEXPORTINGname = 'cell'no_flush = 'X'top = ileft = jrows = rows_numbercolumns = columns_numberIMPORTINGerror = errors.APPEND errors.REFRESH: ranges, excel_input.rangeitem-name = 'cell'.rangeitem-columns = 1.rangeitem-rows = 1.APPEND rangeitem TO ranges.excel_input_wa-column = 1.excel_input_wa-row = 1.excel_input_wa-value = value.APPEND excel_input_wa TO excel_input.* set dataCALL METHOD spreadsheet->set_ranges_dataEXPORTINGranges = rangescontents = excel_inputno_flush = 'X'IMPORTINGerror = errors.APPEND errors.CALL METHOD spreadsheet->fit_widestEXPORTINGname = spaceno_flush = 'X'.REFRESH: ranges, excel_input.ENDFORM. " fill_cell*&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT*&---------------------------------------------------------------------* * text*----------------------------------------------------------------------*MODULE user_command_0100 INPUT.flag = 'X'.save_ok = ok_code.CLEAR ok_code.CASE save_ok.WHEN'&F12'OR'&F15'. "'STOP' .IF NOT document IS INITIAL.CALL METHOD document->close_document.FREE document.ENDIF.IF NOT control IS INITIAL.CALL METHOD control->destroy_control.FREE control.ENDIF.LEAVE PROGRAM.WHEN'&F03'. "'BACK' .IF NOT document IS INITIAL.CALL METHOD document->close_document.FREE document.ENDIF.IF NOT control IS INITIAL.CALL METHOD control->destroy_control.FREE control.ENDIF.SET SCREEN0. " quit the program"set screen 1000.ENDCASE.ENDMODULE. " USER_COMMAND_0100 INPUT。