OLE填充EXCEL

先把基本的FORM写好:

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

2.指定行列边框,字体

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

3.单元格填充

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.

ENDFORM.

3.2居中填充单元格

FORM fill_cell_gs2  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‘ = 2.
  "&& 垂直方向 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.

ENDFORM.

4.插入行:

*&---------------------------------------------------------------------*
*& 向excel中的指定行插入N行
*&---------------------------------------------------------------------*
FORM excel_row_insert USING lcobj_sheet
                            lc_row
                            lc_count.
  DATA lc_range TYPE ole2_object.
  DATA h_borders  TYPE ole2_object.
  DATA:p_row TYPE i.

  p_row = lc_row - 1.
  DO lc_count TIMES.
    CALL METHOD OF lcobj_sheet ‘Rows‘ = lc_range
      EXPORTING #1 = p_row.
    CALL METHOD OF lc_range ‘Copy‘.
    "copy第6行插入一个新行
    CALL METHOD OF lcobj_sheet ‘Rows‘ = lc_range
    EXPORTING #1 = p_row.
    CALL METHOD OF lc_range ‘Insert‘.
    CALL METHOD OF lc_range ‘ClearContents‘. "是否需要清空Cell
  ENDDO.
ENDFORM.                    "excel_row_insert

5.正式的数据填充。excel打开,关闭,保存

  CREATE OBJECT gs_excel ‘EXCEL.APPLICATION‘.
*设置EXCEL可见
  SET PROPERTY OF gs_excel ‘Visible‘ = 0.
*创建工作表
  CALL METHOD OF gs_excel ‘Workbooks‘ = gs_workbook.

  CALL METHOD OF gs_workbook ‘Open‘
    EXPORTING
      #1 = gv_file3. "打开上面下载路径下的excel文件
*选中相应sheet设置名称
  CALL METHOD OF gs_excel ‘WORKSHEETS‘ = gs_sheet
     EXPORTING
       #1 = 1.
  CALL METHOD OF gs_sheet ‘Select‘ .
  CALL METHOD OF gs_sheet ‘ACTIVATE‘.

  ls_head-ktext = m_kunnr."利润中心先赋值
  SELECT SINGLE name1 INTO ls_head-m_kunnr
        FROM kna1 WHERE kunnr = s_kunnr-low.

  CONCATENATE s_gjahr-low+0(4) ‘年‘ s_monat-low+0(2) ‘月对账单‘ INTO lv_str1.
  PERFORM fill_cell_gs2 USING: 3  1  1 16 0 lv_str1."抬头填充
  PERFORM fill_cell_gs2 USING: 4  2  0 11 0 ls_head-m_kunnr,"购买方
                              2  8  0 11 0 s_kunnr-low,"客户编码
*                              4  4  0 11 0 s_kunnr-low,"客户编码
                              4  6  0 11 0 m_bukrs."供货方

  CLEAR:gs_line1.
  CONCATENATE ‘一、‘ s_gjahr-low ‘年‘ s_monat-low ‘月期间,我司按约定为贵司送货明细如下:‘ INTO gs_line1.
  PERFORM fill_cell_gs2 USING: 8  1  0 11 0 gs_line1."

  lv_cur_line = 10.

  SORT gt_shows BY wadat_ist ASCENDING.
  LOOP AT gt_shows INTO gw_show.
    "电话联系人
    "ZM业务助理 供货方
    IF ls_head-g_cname = ‘‘ OR ls_head-ghf_tel = ‘‘.
      SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_vbpa
      FROM vbpa
      WHERE vbeln = gw_show-vbeln
      AND parvw = ‘ZM‘.
      IF sy-subrc = 0.
        SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_pa0002
         FROM pa0002
         WHERE pernr = gs_vbpa-pernr.
        SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_pa0105
   FROM pa0105 WHERE pernr = gs_vbpa-pernr AND subty IN (‘0005‘,‘0020‘).
        IF gs_pa0002-nachn <> ‘‘.
          ls_head-g_cname = gs_pa0002-nachn.
        ENDIF.
        SORT gt_pa0105 BY pernr subty ASCENDING .
        IF ls_head-ghf_tel IS INITIAL .
          LOOP AT gt_pa0105.
            IF gt_pa0105-subty = ‘0005‘.
              ls_head-ghf_tel = gt_pa0105-usrid.
            ELSEIF gt_pa0105-subty = ‘0020‘.
              IF gt_pa0105-usrid_long+20(20) <> ‘‘.
                gt_pa0105-usrid_long+20(1) = ‘-‘.
                CONCATENATE ls_head-ghf_tel gt_pa0105-usrid_long INTO ls_head-ghf_tel SEPARATED BY ‘/‘.
                CONDENSE ls_head-ghf_tel NO-GAPS.
              ELSE.
                CONDENSE gt_pa0105-usrid_long NO-GAPS.
                CONCATENATE ls_head-ghf_tel gt_pa0105-usrid_long INTO ls_head-ghf_tel SEPARATED BY ‘/‘.
              ENDIF.
            ENDIF.
          ENDLOOP.
        ENDIF.
      ENDIF.
    ENDIF.
    "ZP 联系人 购货方
    IF ls_head-m_name = ‘‘ OR ls_head-m_tel = ‘‘.
      SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_vbpa
      FROM vbpa
      WHERE vbeln = gw_show-vgbel
      AND parvw = ‘ZP‘.
      IF sy-subrc = 0.
        SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_knvk
         FROM knvk
         WHERE parnr = gs_vbpa-parnr
         AND   kunnr = gw_show-kunnr.

        SELECT SINGLE * INTO CORRESPONDING FIELDS OF gs_adcp
         FROM adcp
         WHERE persnumber = gs_vbpa-adrnp.
        IF gs_knvk-name1 <> ‘‘.
          ls_head-m_name = gs_knvk-name1.
        ENDIF.
        IF gs_adcp-tel_number <> ‘‘.
          ls_head-m_tel = gs_adcp-tel_number.
        ENDIF.
        IF gs_adcp-fax_number <> ‘‘.
          CONCATENATE ls_head-m_tel gs_adcp-fax_number INTO  ls_head-m_tel SEPARATED BY ‘/‘.
        ENDIF.
      ENDIF.
    ENDIF.
    "电话联系人

    PERFORM fill_cell_gs USING:lv_cur_line 1 0 11 1 gw_show-wadat_ist,
                               lv_cur_line 2 0 11 1 gw_show-bstkd,
                               lv_cur_line 3 0 11 1 gw_show-vbeln,
                               lv_cur_line 4 0 11 1 gw_show-arktx,
                               lv_cur_line 5 0 11 1 gw_show-zgg,
                               lv_cur_line 6 0 11 1 gw_show-vrkme,
                               lv_cur_line 7 0 11 1 gw_show-lfimg,
                               lv_cur_line 8 0 11 1 gw_show-zdj,
                               lv_cur_line 9 0 11 1 gw_show-kzwi1.
    lv_count = lv_count + gw_show-kzwi1.
    lv_cur_line = lv_cur_line + 1.
    AT LAST.
      gv_flag = ‘X‘.
      PERFORM fill_cell_gs USING:lv_cur_line 9 0 11 1 lv_count."汇总金额
      lv_cur_line = lv_cur_line + 1.
    ENDAT.

    IF gv_flag = ‘‘.
      PERFORM excel_row_insert USING:gs_sheet lv_cur_line 1."未结束,插入空白行 等待下一循环
    ENDIF.
  ENDLOOP.

  "set
  PERFORM fill_cell_gs2 USING: 5  6  0 11 0 ls_head-g_cname."供货方联系人信息
  PERFORM fill_cell_gs2 USING: 6  6  0 11 0 ls_head-ghf_tel."
  PERFORM fill_cell_gs2 USING: 5  2  0 11 0 ls_head-m_name."购货方联系人信息
  PERFORM fill_cell_gs2 USING: 6  2  0 11 0 ls_head-m_tel."
  "set

  "截止,货款余额
  READ TABLE gt_kxmx WITH KEY kunnr = gw_show-kunnr.
  IF sy-subrc = 0.
    gt_kxmx-dm1 = gt_kxmx-dm1 + lv_count.
    WRITE gt_kxmx-dm1 TO lv_swzf.
    WRITE gt_kxmx-wgq TO lv_wgq.
    WRITE gt_kxmx-ygq TO lv_ygq.
  ENDIF.
  CLEAR:gs_line1.
  CONCATENATE ‘二、截止‘ s_bldat-high+0(4) ‘年‘ s_bldat-high+4(2) ‘月‘ s_bldat-high+6(2) ‘日,   ‘
  ls_head-m_kunnr ‘尚未支付广东正业科技股份有限公司‘ ls_head-ktext ‘货款  ‘ lv_swzf ‘  元,‘
  ‘其中本月未过期货款  ‘ lv_wgq ‘  元,已到期货款  ‘ lv_ygq ‘  元,具体明细如下:‘
  INTO gs_line1.
*  CONDENSE gs_line1 NO-GAPS."去空格
  PERFORM fill_cell_gs2 USING:lv_cur_line 1 0 11 1 gs_line1."
  lv_cur_line = lv_cur_line + 2."未清清单开始需加2

  CLEAR:gv_flag.
  SORT ct_bsid BY gjahr ASCENDING monat ASCENDING.
  LOOP AT ct_bsid.
    PERFORM fill_cell_gs USING:lv_cur_line 1 0 12 1 ct_bsid-gjahr,"年
                               lv_cur_line 3 0 12 1 ct_bsid-monat,"月
                               lv_cur_line 5 0 12 1 ct_bsid-dmbtr."未清合计
    lv_cur_line = lv_cur_line + 1.
    AT LAST.
      gv_flag = ‘X‘.
    ENDAT.
    IF gv_flag = ‘‘.
      PERFORM excel_row_insert USING:gs_sheet lv_cur_line 1."未结束,插入空白行 等待下一循环
    ENDIF.
  ENDLOOP.

  IF ct_bsid[] IS INITIAL..
    lv_cur_line = lv_cur_line + 1."add by ly 20160216
  ENDIF.

  CLEAR gs_line1.
  CONCATENATE ‘请 ‘ ls_head-m_kunnr ‘收到本对账单之日起于5个工作日内予以签章确认,并回传至广东正业科技股份有限公司。‘
  ‘否则,广东正业科技股份有限公司视同贵司对本账单确认无误!‘ INTO gs_line1.
  PERFORM fill_cell_gs2 USING:lv_cur_line 1 0 11 1 gs_line1."
  PERFORM merged USING lv_cur_line 1 lv_cur_line 9 0."不知道什么原因,多出个边框

  lv_cur_line = lv_cur_line + 3.
  PERFORM fill_cell_gs2 USING:lv_cur_line 2 0 11 0 ls_head-g_cname."供货方制表
  PERFORM fill_cell_gs2 USING:lv_cur_line 7 0 11 0 ls_head-m_kunnr."购货方

  lv_cur_line = lv_cur_line + 1.
  PERFORM fill_cell_gs2 USING:lv_cur_line 2 0 11 0 sy-datum."制表日期

  GET PROPERTY OF gs_excel ‘ActiveWorkbook‘ = gs_workbook.
  CALL METHOD OF gs_workbook ‘SAVE‘.
  IF sy-subrc = 0.
    MESSAGE ‘数据已导出‘ TYPE ‘S‘.
  ELSE.
    MESSAGE ‘数据导出失败‘ TYPE ‘S‘ 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.

剩下的自己研究。。。

时间: 2024-10-24 17:55:23

OLE填充EXCEL的相关文章

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 '

OLE操作Excel编译错误处理

Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误.这几天研究了下OLE操作Excel.环境:VS2008 SP1+Excel 2007 加入OLE Type Library随便建立一个MFC程序,选添加类,Typelib中的MFC类,出来一个对话,可用类型库选“Microsoft Excel 12.0 Object Library”一般来说,添加下边这些类就够了,当然也可以全部添加CApplication CWorkbookCWorkbooksCWorkshee

C++使用OLE高速读写EXCEL的源码

<C++读写EXCEL文件方式比较>. 我的代码参考的地方是这儿,再次感谢原作者 http://blog.csdn.net/gyssoft/archive/2007/04/29/1592104.aspx 我根据自己的需要做了整理,干净了一点,而后根据发现的速度问题做了一些优化. 预加载的思路来自这个帖子 http://topic.csdn.net/t/20030626/21/1962211.html 其实思路很简单,不再一个CELL一个CELL的伛数据,而是一次把表格里面所有的数据读取出来处理

下载魔板,上传EXCEL

说道SAP里对EXCEL操作的大概就是上传,下载,显示了... 下载:(文档是通过SMW0上传的)注:如果下载的时候需要填充EXCEL的值,...请参考另一篇文档,OLE CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG EXPORTING DEFAULT_EXTENSION = 'xlsx' DEFAULT_FILE_NAME = LC_FILENAME CHANGING FILENAME = LC_FILENAME PATH = L

VSTO:使用C#开发Excel、Word【10】

第二部分:.NET中的Office编程本书前两章介绍了Office对象模型和Office PIA. 您还看到如何使用Visual Studio使用VSTO的功能构建文档中的控制台应用程序,加载项和代码. 本书的第二部分更深入地介绍了Office对象模型. 如果您只对Excel开发感兴趣,请阅读第3章至第5章.如果您只对Word开发感兴趣,请阅读第6章至第9章.如果您只对Outlook开发感兴趣,请阅读第7章至第11章.如果您只感兴趣 在InfoPath开发中,阅读第12章. 第3章  Excel

使用node.js生成excel报表下载(excel-export express篇)

引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种com组件贴心使用. nodejs作为一门新的语言,报表功能也不是十分完善. (1).js-xlsx : 目前 Github 上 star 数量最多的处理 Excel 的库,支持解析多种格式表格XLSX / XLSM / XLSB / XLS / CSV,解析采用纯js实现,写入需要依赖nodejs

excel 学习

最近越來越感覺到熟練運用office軟件是一門很深的學問,以前一直忽視了這個技能,因爲從來都不覺得這個有什麼技術難度.正是因爲這樣想,所以才一直沒能去好好研究使用這套工具.現在卻覺得有必要學習,在於: 1. 工具可以幫我們提高效率.更何況office是電腦上最基礎最常用的工具.可能因爲這套軟件太常見了,才一直忽視它們的強大能力. 2. 用好這套工具,不僅僅是對軟件操作的熟練,更是對表達能力和審美的鍛鍊.特別是做一個好的excel/ppt,是在訓練表達能力,如何用各種圖表和顏色搭配來幫助我們表達意

用 Perl 读写 Excel 文件

转载:http://www.ibm.com/developerworks/cn/linux/sdk/perl/culture-8/ Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel 在 2000 年,Takanori Kawai 和 John McNamara 编写出了 Spreadsheet::WriteExcel 和Spreadsheet::ParseExcel 模块并将它们张贴在 CPAN 上,这两个模块使得在任何平台上从 Excel 文件

关于使用OpenXml向Excel插入数据的一点总结

嗯,项目需要... 正好知道OpenXml这个东西,所以就拿过来研究了一下. 总体感觉果然是XML思想的产物,我原以为是像DataTable那样操作Excel,结果我想多了,不过还好,挺好用的,扩展性很好的说. 这个链接是微软MVA学院介绍OpenXml的简单教程 Open Xml SDK教程 这个是OpenXml SDK的下载链接 下载 OpenXml SDK 解压之后,安装. 然后在解决方案中添加这两个引用 以下是代码 (有一点要注意,这个总结是对于向Excel模板中插入数据的,也就是必须保