Chinaunix首页 | 论坛 | 博客
  • 博客访问: 197082
  • 博文数量: 98
  • 博客积分: 1435
  • 博客等级: 上尉
  • 技术积分: 1955
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 15:01
文章分类

全部博文(98)

文章存档

2012年(1)

2011年(1)

2009年(95)

2008年(1)

我的朋友

分类: IT职场

2009-03-12 17:00:29

EXCEL数据上载到内表又一例

大家经常用的恐怕是ALSM_EXCEL_TO_INTERNAL_TABLE,还有个KCD_EXCEL_OLE_TO_INT_CONVERT
跟它类似,不过相比起来后一个读取的单元格的值的位数要小一些,如果单元格长度大于50的话,不妨自己把函数改改.

TYPES:   BEGIN OF t_datatab,
         col1(25)  TYPE c,
         col2(30)  TYPE c,
         col3(30)  TYPE c,
         col4(30)  TYPE c,
         col5(30)  TYPE c,
         col6(30)  TYPE c,
       END OF t_datatab.
DATA: it_datatab TYPE STANDARD TABLE OF t_datatab INITIAL SIZE 0,
      wa_datatab TYPE t_datatab.
Data : p_table type t_datatab occurs 0 with header line.
DATA : gd_scol   TYPE i VALUE '1',
       gd_srow   TYPE i VALUE '1',
       gd_ecol   TYPE i VALUE '6',
       gd_erow   TYPE i VALUE '2000'.

DATA: it_tab TYPE filetable,
      gd_subrc TYPE i.
field-symbols : .

*********************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS:  p_file LIKE rlgrap-filename
               DEFAULT 'c:\test.xls' OBLIGATORY.   " File Name
SELECTION-SCREEN END OF BLOCK b1.

***********************************************************************
START-OF-SELECTION.
  PERFORM upload_excel_file TABLES   it_datatab
                             USING   p_file
                                     gd_scol
                                     gd_srow
                                     gd_ecol
                                     gd_erow.
  LOOP AT it_datatab INTO wa_datatab.
    WRITE:/ wa_datatab-col1,
            wa_datatab-col2,
            wa_datatab-col3,
            wa_datatab-col4,
            wa_datatab-col5,
            wa_datatab-col6.
  ENDLOOP.


*&--------------------------------------------------------------------*
*&      Form  UPLOAD_EXCEL_FILE
*&--------------------------------------------------------------------*
*       upload excel spreadsheet into internal table
*---------------------------------------------------------------------*
*      -->P_TABLE    Table to return excel data into
*      -->P_FILE     file name and path
*      -->P_SCOL     start column
*      -->P_SROW     start row
*      -->P_ECOL     end column
*      -->P_EROW     end row
*---------------------------------------------------------------------*
FORM upload_excel_file TABLES   p_table
                       USING    p_file
                                p_scol
                                p_srow
                                p_ecol
                                p_erow.

  DATA : lt_intern TYPE  kcde_cells OCCURS 0 WITH HEADER LINE.
  DATA : ld_index TYPE i.

  CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'
    EXPORTING
      filename                = p_file
      i_begin_col             = p_scol
      i_begin_row             = p_srow
      i_end_col               = p_ecol
      i_end_row               = p_erow
    TABLES
      intern                  = LT_INTERN
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
    FORMAT COLOR COL_BACKGROUND INTENSIFIED.
    WRITE:/ 'Error Uploading file'.
    EXIT.
  ENDIF.

  IF lt_intern[] IS INITIAL.
    FORMAT COLOR COL_BACKGROUND INTENSIFIED.
    WRITE:/ 'No Data Uploaded'.
    EXIT.
  ELSE.
    SORT lt_intern BY row col.
    LOOP AT lt_intern.
     MOVE lt_intern-col TO ld_index.
     assign component ld_index of structure  p_table to .
     move : lt_intern-value to .
      AT END OF row.
        APPEND p_table.
        CLEAR p_table.
      ENDAT.
    ENDLOOP.
  ENDIF.
ENDFORM.                    "UPLOAD_EXCEL_FILE

阅读(739) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~