SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Upload from DBF format



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Thu Nov 08, 2007 11:03 am    Post subject: Upload from DBF format Reply with quote

Code:
REPORT z_load_dbf_file.
INCLUDE ole2incl.
*&---------------------------------------------------------------------*
*&      Form  load_dbf_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM load_dbf_file TABLES input_table
                   USING value(t_filename) LIKE rlgrap-filename.
  DATA: application TYPE ole2_object,
        workbooks TYPE ole2_object,
        fworkbook TYPE ole2_object,
        fsheets TYPE ole2_object,
        fsheet TYPE ole2_object,
        dsheet TYPE ole2_object,
        frange TYPE ole2_object,
        frow TYPE ole2_object.

  DATA: return,
      filename(128)   TYPE c,
      l_filename TYPE string.

* Filename conversion

  MOVE t_filename TO filename.

  TRANSLATE t_filename TO UPPER CASE.

  REPLACE '.DBF' WITH '.DAT' INTO t_filename.
  IF sy-subrc NE 0.
* error - filename extension is not DBF "TODO
    MESSAGE w899(f5) WITH 'Error - filename extension is not DBF'.
    EXIT.
  ENDIF.

* check file existence
  CALL FUNCTION 'WS_QUERY'
    EXPORTING
      filename       = filename
      query          = 'FE'
    IMPORTING
      return         = return
    EXCEPTIONS
      inv_query      = 1
      no_batch       = 2
      frontend_error = 3
      OTHERS         = 4.
  IF ( sy-subrc NE 0 ) OR ( return EQ 0 ).
* error - file does not exist "TODO
    MESSAGE w899(f5) WITH 'Error - file does not exist'.
    EXIT.
  ENDIF.

***** Working through OLE (MS Excel) *****
* OLE initialization
  CREATE OBJECT dsheet 'Excel.Sheet'.
  IF sy-subrc NE 0.
* error - OLE initialization of MS Excel "TODO
    MESSAGE e899(f5) WITH 'Error - OLE initialization of MS Excel'.
    EXIT.
  ENDIF.
* supress messages from MS Excel
  CALL METHOD OF dsheet 'Application' = application.
  SET PROPERTY OF application 'DisplayAlerts' = 0.
* opening DBF-file
  CALL METHOD OF application 'Workbooks' = workbooks.
  CALL METHOD OF workbooks 'Open' EXPORTING #1 = filename
    #2 = 0
    #3 = 1.
* remove header line from DBF data
  GET PROPERTY OF application 'ActiveWorkbook' = fworkbook.
  CALL METHOD OF fworkbook 'Sheets' = fsheets.
  CALL METHOD OF fsheets 'Item' = fsheet EXPORTING #1 = 1.
  GET PROPERTY OF fsheet 'Rows' = frange.
  CALL METHOD OF frange 'Item' = frow EXPORTING #1 = 1.
  CALL METHOD OF frow 'Delete'.
* save as DAT file
  CALL METHOD OF fworkbook 'SaveAs' EXPORTING #1 = t_filename
    #2 = 20.
* close the book without saving
  CALL METHOD OF fworkbook 'Close' EXPORTING #1 = 0.
  FREE OBJECT dsheet.

***** Filling internal table with data *****
  REFRESH input_table.
  l_filename = t_filename.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = l_filename
      has_field_separator     = 'X'
    TABLES
      data_tab                = input_table
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc NE 0.
* error - loading DAT file "TODO
    MESSAGE w899(f5) WITH 'Error - loading DAT file'.
    EXIT.
  ELSE.
* the loading was successfull "TODO
  ENDIF.
***** Delete temporary DAT-file *****
  CALL FUNCTION 'GUI_DELETE_FILE'
    EXPORTING
      file_name = t_filename
    EXCEPTIONS
      OTHERS    = 1.
  IF sy-subrc <> 0.
    CALL FUNCTION 'WS_QUERY'
      EXPORTING
        filename       = t_filename
        query          = 'FE'
      IMPORTING
        return         = return
      EXCEPTIONS
        inv_query      = 1
        no_batch       = 2
        frontend_error = 3
        OTHERS         = 4.
    IF ( sy-subrc NE 0 ) OR ( return EQ 0 ).
      EXIT.
    ELSE.
* error - deleting temporary file "TODO
    ENDIF.
  ENDIF.
ENDFORM.                    " load_dbf_file
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.