Step-by-step implementation of ABAP background import Excel to database "1"

Source: Internet
Author: User
Tags app service

In SAP applications, importing and exporting Excel files is a common requirement, and sometimes users may have a large amount of data that needs to be imported periodically into SAP's database. In this case, using the importer in the foreground import may take a while, if you can schedule the import program as a background job, you can save the user's time and effectively utilize the idle server resources. The following is an introduction to the relevant knowledge and practical approaches.

This article link: http://www.cnblogs.com/hhelibeb/p/5912330.html

Original content, reproduced please specify

1, defining the structure

First, prepare an Excel file. Suppose a relatively simple scenario, such as a database table to be uploaded, has three fields. Then we also create a three-column content Excel file, test.xlsx:

  

From this, you can define the corresponding inner table Itab in your code:

DATA BEGIN  of WA,        col1 (type  C,        col2 (type  C,        col3 (TYPE  C, END      of  wa. DATA  like TABLE  of WA.
2, upload and read the file

We need to have a selection screen that specifies the path to the file:

Selection-screen BEGIN  of BLOCK  with FRAME TITLE text-001. PARAMETERS TYPE  ' C:\Users\liyue\Desktop\test.xlsx ' ID ty1. Selection-screen END  of Block block

Add File Selection Help:

 at Selection-screen  on value-request  forP_source. PERFORMGet_filename.
FORMGet_filename. TRY. Pager FUNCTION 'Ws_filename_get' ExportingMask='*. XLSX.'Mode='O' Importingfilename=P_sourceEXCEPTIONSInv_winsys= onNo_batch= GenevaSelection_cancel=GenevaSelection_error=Geneva. Endtry. IFP_sourceEQ "'. MESSAGEs000 (ZFI01) with 'No files selected!'DISPLAY like 'E'. ENDIF.EndForm.

After running the program, use the Alsm_excel_to_internal_table function to read the file:

start-of-selection. PERFORMRead_data. PERFORMOutput_data.*&---------------------------------------------------------------------**& Form Frm_read_data*&---------------------------------------------------------------------**read the uploaded Excel file*----------------------------------------------------------------------**- P1 text*<--P2 text*----------------------------------------------------------------------*FORMRead_data. TRY. Pager FUNCTION 'alsm_excel_to_internal_table'        Exportingfilename=P_source I_begin_col=1I_begin_row=1I_end_col=255I_end_row=3        TABLESIntern=gt_excel[]EXCEPTIONSinconsistent_parameters=1S_file_ole=2OTHERS=3. IFSY-SUBRC <>0. MESSAGEs000 (ZFI01) with 'File open failed!'DISPLAY like 'E'. LEAVE list-processing. ENDIF.*CATCH icx_obl_parameter_error into.  Endtry. Field-symbols<fs_value>. Field-symbols<fs_excel> likeGt_excel. SORTGt_excel byRow Col. LOOP  atGt_excel Assigning <fs_excel>. ASSIGN COMPONENT<fs_excel>-col of STRUCTUREWa to<fs_value>. <fs_value> = <fs_excel>-value.  at END  ofrow. APPENDWa toItab. CLEARWA. ENDAT. Endloop.EndForm.*&---------------------------------------------------------------------**& Form Output_data*&---------------------------------------------------------------------**writing data to Applacation Server*----------------------------------------------------------------------**- P1 text*<--P2 text*----------------------------------------------------------------------*FORMOutput_data. DATAS_fileTYPErlgrap-filename. PERFORMGenerate_filename_in_serverUSINGS_file. OPEN DATASETS_file forINPUTinch TEXTMODE ENCODING DEFAULT. IFSY-SUBRC =0 . LOOP  atItab intoWA. Write:\WA. Endloop.EndForm.

There is no INSERT statement inserted into the database because we have not established the corresponding database table:) Therefore, you first use write to verify the data read condition.

The next step is to schedule the program to run as a background job. On the Select screen, click F9, Schedule the program to execute as a background job immediately, (if successful, you can view the file contents of the write output in the spool log):

  

  

After saving to view the results of the run, open the toolbar--system--own job

  

Well.... What, has it been canceled? It seems that the job has failed, so we have to analyze the reason. Double-click the task name and click on the "Task Log" button to view:

  

Originally, in a background job, the Alsm_excel_to_internal_table function did not read the contents of the file correctly.

On the SCN to inquire about this problem, you can find that many people have stepped on the pit. It is also explained that the corresponding file content cannot be read because background jobs actually run on the ABAP application server level (application Servers). Instead of the presentation layer (Presentation), it is not possible to read the file in the given path.

Tips: SAP system Architecture

1. At the bottom is the database tier. SAP does not provide the underlying database, but uses the other vendor's database management system (which supports all major databases) . . Only the programs and metadata required to manage and run SAP itself are not kept in the database, and almost all the data that the application runs is stored in the database.

2.ABAP program runs at app service layer . The ABAP program includes standard programs provided by SAP and our own developed programs. ABAP programs read data from a database, process data, and possibly store data.

3. The third layer is the presentation layer. This layer is the user interface, through which users can access programs, enter data, and receive the results of work process processing. SAP also calls the presentation layer a server, because it works like a Web browser, and is responsible for translating the interface layout data from the application server into a user-friendly interface, which can also become a ' service '.

&NBSP;

in the SAP system, The technical distribution of the software is completely independent of the physical location of the hardware it installs.

It seems that we need to find a compromise way to implement background import data to the database. The next step is to implement the ABAP background import Excel to the database "2".

  

  

Step-by-step implementation of ABAP background import Excel to database "1"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.