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
时间: 2024-10-13 19:46:49

OLE填充EXCEL 多SHEET的相关文章

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. W

OLE操作Excel编译错误处理

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

java的poi技术写Excel的Sheet

在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-2007,2010] java的poi技术读取Excel数据到MySQL java的jxl技术导入Excel java的poi技术读取和导入Excel 然而,这篇blog主要内容是关于Excel里面怎样去写Sheet数据. 那么在Excel里面什么叫做Sheet呢?如下图红色框里面的内容就是Excel的

C#实现多Excel多Sheet合并

using Excel = Microsoft.Office.Interop.Excel;using System.Reflection; int modelSheetHeadRows = 1; Excel.Application excel = null; try { excel = new Excel.Application(); string newModel = tbModel.Text.Replace(".", "合并."); excel.Visible

Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印

1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) throws IOException { String[][] testArray = new String[sheet.getPhysicalNumberOfRows()][]; for(int rowId =0;rowId<sheet.getPhysicalNumberOfRows();rowId++)

POI删除Excel的sheet页

POI删除Excel的sheet页 poi有删除一个sheet页的方法,我希望除了一个sheet页之外其他的都删除,但是使用的时候发现,删除之后经常会出现文件破坏的提示,研究发现,是由于excel有一个默认的当前页功能. 比如表格中有3个sheet页,保存的时候默认显示的是第三页,那么删除两页之后,表格只剩一页,打开的时候表格会显示第三页失败,就会提示文件损坏.. 遇到这种情况,有两种方法: 将表格默认显示的页改为第一页再处理 使用代码修改默认页. 此方法删除表格中除了传入的页之外所有的页 /*

利用VBA遍历Excel所有sheet的例子

下面是利用VBA遍历Excel sheet的例子,可以用于Excel的批量处理. Sub Insert_CodeString() Dim fs, ft As Object Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim Sql As String Sql = "truncate table code_string;" & Chr(10)

关于C#操作Excel,复制Sheet的记录

1.先用了NPOI,去做,HSSFWorkbook 里面有一个Copy方法,但这个只支持office2003. 对应的XSSFWorkbook没有些方法. 而且这个这个方法对devexpress导出的2003的excel文件读取不了,出现异常,需要用excel打开后,另存一下才行. var fs = new FileStream("c://pivotGrid.xls", FileMode.Open, FileAccess.Read); HSSFWorkbook workbook = n

JAVA读取Excel中sheet页内容

package com.bestpay.bpbp.bill.init; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import java.io.File; import java.io.IOException; public class ReadExcelDataForTestNG {    private static  String parementF