首页 > 代码库 > OLE填充EXCEL 多SHEET

OLE填充EXCEL 多SHEET

"1 设置行高 "参数说明:行/列号、行高/列宽、R-行 C-列

FORM row_column  USING   p_r p_width p_type.  CASE p_type.    WHEN R.        "行高      CALL METHOD OF gs_excel CELLS = gs_cells EXPORTING #1 = p_r #2 = 1.      GET PROPERTY OF gs_cells rows = gs_rows.      SET PROPERTY OF gs_rows rowheight = p_width.    WHEN C.        "列宽      CALL METHOD OF gs_excel CELLS = gs_cells EXPORTING #1 = 1 #2 = p_r.      GET PROPERTY OF gs_cells columns = gs_columns .      SET PROPERTY OF gs_columns columnwidth = p_width.  ENDCASE.  FREE OBJECT gs_rows.  FREE OBJECT gs_columns.  FREE OBJECT gs_cells.ENDFORM.                    " PRM_EXPORT_EXCEL1

"1 设置列宽

FORM row_column  USING   p_r p_width p_type.  CASE p_type.    WHEN R.        "行高      CALL METHOD OF gs_excel CELLS = gs_cells EXPORTING #1 = p_r #2 = 1.      GET PROPERTY OF gs_cells rows = gs_rows.      SET PROPERTY OF gs_rows rowheight = p_width.    WHEN C.        "列宽      CALL METHOD OF gs_excel CELLS = gs_cells EXPORTING #1 = 1 #2 = p_r.      GET PROPERTY OF gs_cells columns = gs_columns .      SET PROPERTY OF gs_columns columnwidth = p_width.  ENDCASE.  FREE OBJECT gs_rows.  FREE OBJECT gs_columns.  FREE OBJECT gs_cells.ENDFORM.                    " PRM_EXPORT_EXCEL1

"设置边框 "第一个单元格X、Y 第二个单元格X、Y、边框(1带边框 0不带)

FORM merged  USING   x1 y1 x2 y2 p_linestyle.*--Selecting cell area to be merged.  CALL METHOD OF gs_excel Cells = gs_cell1    EXPORTING    #1 = x1    #2 = y1.  CALL METHOD OF gs_excel Cells = gs_cell2    EXPORTING    #1 = x2    #2 = y2.  CALL METHOD OF gs_excel Range = gs_range    EXPORTING    #1 = gs_cell1    #2 = gs_cell2.  CALL METHOD OF gs_range Select.*--Merging  CALL METHOD OF gs_range Merge .  GET PROPERTY OF gs_range borders = gs_borders .  SET PROPERTY OF gs_borders weight = 2.  SET PROPERTY OF gs_borders linestyle = p_linestyle.  FREE OBJECT gs_range.  FREE OBJECT gs_borders.ENDFORM.                    " MERGED
FORM fill_cell_gs  USING p_r1                         p_r2                         p_bold                         p_size                         p_linestyle                         p_value.  CALL METHOD OF gs_excel CELLS = gs_cells EXPORTING #1 = p_r1 #2 = p_r2.* 设置被选中单元格的对齐方式  " && 水平方向 2左对齐,3居中,4右对齐  SET PROPERTY OF gs_cells horizontalAlignment = 3.  "&& 垂直方向 1靠上 ,2居中,3靠下  SET PROPERTY OF gs_cells VerticalAlignment = 2.  "自动换行  SET PROPERTY OF gs_cells WrapText = 1.  "设置边框属性  GET PROPERTY OF gs_cells borders = gs_borders .  SET PROPERTY OF gs_borders weight = 2.  SET PROPERTY OF gs_borders linestyle = p_linestyle.  "设置字体属性  CALL METHOD OF gs_cells FONT = gs_font.  SET PROPERTY OF gs_font BOLD = p_bold.     "1:粗体  0:普通字体  SET PROPERTY OF gs_font SIZE = p_size.     "字体大小  "设置单元格的值  SET PROPERTY OF  gs_cells VALUE = p_value.  FREE OBJECT gs_font.  FREE OBJECT gs_borders.  FREE OBJECT gs_cells.  g_c1 = g_c1 + 1.ENDFORM.
FORM prm_export_excel1 .  DATA:lv_char TYPE string.  DATA:lv_line TYPE i.  DATA:lv_date TYPE string.  DATA:lv_name(10) TYPE c.  DATA:lv1 TYPE c,       lv2 TYPE c,       lv3 TYPE c,       lv4 TYPE c.  DATA:lv_lines TYPE i VALUE 1.  CREATE OBJECT gs_excel EXCEL.APPLICATION.*设置EXCEL可见  SET PROPERTY OF gs_excel Visible = 0.*创建工作表  CALL METHOD OF gs_excel Workbooks = gs_workbook.  DESCRIBE TABLE gt_zsqm00501 LINES lv_line.* 创建sheet并添加到工作表  SET PROPERTY OF gs_excel SHEETSINNEWWORKBOOK = lv_line.   "如需多个sheets,将1改成相应的值  CALL METHOD OF gs_workbook ADD.*如果是已有EXCEL模板则直接打开即可*  CALL METHOD OF gs_workbook ‘OPEN‘*    EXPORTING*      #1 = ‘C:\test.xls‘.   "EXCEL文件路径*选中相应sheet设置名称  LOOP AT gt_zsqm00501 INTO gs_zsqm00501.*    SHIFT GS_ZSQM00501-PRUEFLOS LEFT DELETING LEADING ‘0‘.    CALL METHOD OF gs_excel WORKSHEETS = gs_sheet       EXPORTING         #1 = lv_lines.    lv_lines = lv_lines + 1.    CALL METHOD OF gs_sheet ACTIVATE.    SET PROPERTY OF gs_sheet NAME  = gs_zsqm00501-prueflos.    FREE OBJECT gs_sheet .    "参数说明:行/列号、行高/列宽、R-行 C-列    PERFORM  row_column USING: 1  23 R, "1 设置行高                               2  23 R,                               3  23 R,                               4  23 R,                               5  20 R,                               6  20 R,                               7  20 R,                               8  20 R,                               9  20 R,                              10  20 R,                              11  20 R.    PERFORM  row_column USING: 1  18 C, "1 设置列宽                               2  18 C,                               3  18 C,                               4  18 C,                               5  14 C.    "第一个单元格X、Y 第二个单元格X、Y、边框(1带边框 0不带)    PERFORM merged USING:1 1 1 5 0 ,"行1                         2 1 2 5 0 ,"行2                         3 1 3 5 0 ,                         4 1 4 5 0 ,                         5 1 5 1 1 ,                         5 2 5 2 1 ,                         5 3 5 3 1 ,                         5 4 5 5 1 ,                         6 1 6 1 1 ,                         6 2 6 2 1 ,                         6 3 6 3 1 ,                         6 4 6 5 1 ,                         7 1 7 1 1 ,                         7 2 7 2 1 ,                         7 3 7 3 1 ,                         7 4 7 5 1 ,                         8 1 8 1 1 ,                         8 2 8 2 1 ,                         8 3 8 3 1 ,                         8 4 8 5 1 ,                         9 1 9 1 1 ,                         9 2 9 2 1 ,                         9 3 9 3 1 ,                         9 4 9 5 1 ,                       10 1 10 1 1 ,                       10 2 10 2 1 ,                       10 3 10 3 1 ,                       10 4 10 4 1 ,                       10 5 10 5 1 .    SELECT SINGLE butxt INTO g_butxt    FROM t001    WHERE bukrs = gs_zsqm00501-werk.    "参数说明:单元格X、单元格Y、字体(1粗体 0普通字体)、字体大小、边框(1带边框 0不带)、内容    IF gs_zsqm00501-werk = 2001.      PERFORM fill_cell_gs USING: 1 1 1 18 0 广州东凌粮油股份有限公司 ,                                  2 1 1  9 0 Donlinks grain & oil CO.,LTD..      lv_name = 黄新哲.    ELSE.      PERFORM fill_cell_gs USING: 1 1 1 18 0 广州植之元油脂实业有限公司,                                  2 1 1  9 0 Guangzhou Green Oil Industrial Co.,Ltd..      lv_name = 文国才.    ENDIF.    CLEAR:lv_char.    CONCATENATE ‘‘‘‘ gs_zsqm00501-pastrterm+0(4) - gs_zsqm00501-pastrterm+4(2) - gs_zsqm00501-pastrterm+6(2) INTO lv_date.    CONCATENATE 质检批号: gs_zsqm00501-zinslot      检验批: gs_zsqm00501-prueflos          制单日期: sy-datum+0(4) - sy-datum+4(2) - sy-datum+6(2) INTO lv_char.    CONCATENATE ‘‘‘‘ gs_zsqm00501-sghth INTO gs_zsqm00501-sghth.    PERFORM fill_cell_gs USING: "1 1 1 18 0 G_BUTXT ,                                3 1 1 18 0 检  验  单,                                4 1 1 12 0 lv_char,                                5 1 1 12 1 物 料 名 称,                                5 2 1 12 1 gs_zsqm00501-ktextmat,                                5 3 1 12 1 采 购 订 单,                                5 4 1 12 1 gs_zsqm00501-ebeln,                                6 1 1 12 1 检 验 日 期,                                6 2 1 12 1 lv_date,*                                6 2 1 12 1 GS_ZSQM00501-PASTRTERM,                                6 3 1 12 1 供  应  商,                                6 4 1 12 1 gs_zsqm00501-name1,                                7 1 1 12 1 抽 样 人,                                7 2 1 12 1 gs_zsqm00501-uname,                                7 3 1 12 1 车/船 号,                                7 4 1 12 1 gs_zsqm00501-zcarid,                                8 1 1 12 1 抽 样 基 数,                                8 2 1 12 1 gs_zsqm00501-zlosmenge,                                8 3 1 12 1 抽样数量,                                8 4 1 12 1 gs_zsqm00501-zgesstichpr,                                9 1 1 12 1 执 行 标 准,                                9 2 1 12 1 gs_zsqm00501-zstand,                                9 3 1 12 1 纸质合同号,                                9 4 1 12 1 gs_zsqm00501-sghth,                               10 1 1 12 1 检 验 项 目,                               10 2 1 12 1 检 验 结 果,                               10 3 1 12 1 检验依据,                               10 4 1 12 1 企 业 标 准,                               10 5 1 12 1 单项评价.    g_r1 = 11.    LOOP AT gt_zsqm00502 INTO gs_zsqm00502 WHERE prueflos = gs_zsqm00501-prueflos.      PERFORM  row_column USING: g_r1  20 R. "1 设置行高      PERFORM fill_cell_gs USING:g_r1 1 1 12 1 gs_zsqm00502-kurztext,                                 g_r1 2 1 12 1 gs_zsqm00502-dxjg,                                 g_r1 3 1 12 1 gs_zsqm00502-pmethode,                                 g_r1 4 1 12 1 gs_zsqm00502-ktx01,                                 g_r1 5 1 12 1 gs_zsqm00502-jywb.      g_r1 = g_r1 + 1.    ENDLOOP.    PERFORM merged USING:g_r1 1 g_r1 1 1 ,                         g_r1 2 g_r1 5 1 .    CLEAR:lv_char.    IF gs_zsqm00501-code = A.      lv1 = .      lv_char = √ 接收       接收,扣款         让步接收        退货.*      CONCATENATE LV1 ‘√ 接收        ‘ LV2 ‘接收,扣款         ‘LV3 ‘让步接收         ‘ LV4 ‘退货‘ INTO LV_CHAR.    ELSEIF gs_zsqm00501-code = A0.      lv2 = .      lv_char = 接收      √ 接收,扣款         让步接收        退货.    ELSEIF gs_zsqm00501-code = A1.      lv3 = .      lv_char = 接收       接收,扣款        √ 让步接收        退货.    ELSEIF gs_zsqm00501-code = R.      lv4 = .      lv_char = 接收       接收,扣款         让步接收       √ 退货.    ENDIF.    PERFORM fill_cell_gs USING: g_r1 1 1 12 1 处 理 方 式,                                g_r1 2 1 12 1 lv_char.    g_r1 = g_r1 + 1.    PERFORM  row_column USING: g_r1  30 R. "1 设置行高    PERFORM fill_cell_gs USING:g_r1 1 1 12 1 备注.    PERFORM merged USING:g_r1 1 g_r1 1 1 ,                         g_r1 2 g_r1 5 1 .    PERFORM fill_cell_gs USING:g_r1 2 1 12 1 gs_zsqm00501-sgtxt.    g_r1 = g_r1 + 2.    PERFORM  row_column USING: g_r1  23 R. "1 设置行高    PERFORM merged USING:g_r1 1 g_r1 5 0.    CLEAR:lv_char.    CONDENSE gs_zsqm00501-usnam NO-GAPS.    CONDENSE lv_name NO-GAPS.    CONDENSE gs_zsqm00501-name_qave NO-GAPS.    CONCATENATE 制单人: gs_zsqm00501-usnam            化验员: gs_zsqm00501-pruefer"LV_NAME                  审核人: gs_zsqm00501-name_qave INTO lv_char.    PERFORM fill_cell_gs USING:g_r1 1 1 12 0 lv_char.    GET PROPERTY OF gs_excel ACTIVESHEET = gs_sheet.    GET PROPERTY OF gs_excel ActiveWorkbook = gs_workbook.  ENDLOOP.  CALL METHOD OF gs_workbook SAVEAS    EXPORTING      #1 = gv_file    "保存路径      #2 = 1.  IF sy-subrc = 0.    MESSAGE s000 WITH 数据已导出.  ELSE.    MESSAGE s000 WITH 数据导出失败 DISPLAY LIKE E.  ENDIF.*  CALL METHOD OF gs_workbook CLOSE.  CALL METHOD OF gs_excel QUIT.  FREE OBJECT gs_sheet.  FREE OBJECT gs_workbook.  FREE OBJECT gs_excel.ENDFORM.                    " FRM_EXCEL_BZYJH

 

OLE填充EXCEL 多SHEET