{"id":1219,"date":"2014-12-09T10:18:24","date_gmt":"2014-12-09T09:18:24","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1219"},"modified":"2014-12-09T11:30:33","modified_gmt":"2014-12-09T10:30:33","slug":"webdynpro-4-abap-open-excel-sheet-from-dms","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1219","title":{"rendered":"WebDynpro 4 ABAP &#8211; Open Excel sheet from DMS"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>In this article I&#8217;ll show you how to retrieve an Excel sheet from SAP DMS (Document management System) where it is stored as &#8220;original&#8221; in a Document container.<!--more--><\/p>\n<p>Let&#8217;s presume that:<\/p>\n<ul>\n<li>we already have our document stored in DMS and we know the document&#8217;s number, type, version and part (these are key fields we have to know to be able to retrieve the document)<\/li>\n<li>We know how to create a webdynpro component\/application<\/li>\n<li>We have already created new WDY component\/application with one Window containing one View where we placed the <em>OfficeControl<\/em> of type MS_EXCEL from the <em>Integration <\/em>layout controls.<\/li>\n<\/ul>\n<h2>1. Read document information (metadata)<\/h2>\n<pre lang=\"abap\">DATA:\r\n  ls_document_data  TYPE bapi_doc_draw2,\r\n  ls_return         TYPE bapiret2,\r\n  lt_document_files TYPE TABLE OF bapi_doc_files2_keys.\r\n\r\nFIELD-SYMBOLS:\r\n  &lt;ls_document_file&gt; LIKE LINE OF lt_document_files.\r\n\r\n* Specify our document key\r\nls_document_data-documenttype    = 'ZTP'. \r\nCALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'\r\n  EXPORTING\r\n    input         = '3000000000003'\r\n  IMPORTING\r\n    OUTPUT        = ls_document_data-documentnumber.\r\nls_document_data-documentversion = '00'.\r\nls_document_data-documentpart    = '000'.\r\n\r\n* Read document info\r\nCALL FUNCTION 'BAPI_DOCUMENT_GETLIST2'\r\n      EXPORTING\r\n        select_documentdata = ls_document_data\r\n        getdocfiles         = 'X'\r\n      IMPORTING\r\n        return              = ls_return\r\n      TABLES\r\n        documentfiles       = lt_document_files.\r\n\r\n    IF ls_return-type CA 'EA'\r\n      OR lines( lt_document_files ) = 0.\r\n*      RAISE EXCEPTION document_does_not_exist.\r\n    ENDIF.\r\n\r\n* To simplify our example we read just the first file from \r\n* the document container\r\n    READ TABLE lt_document_files ASSIGNING &lt;ls_document_file&gt; INDEX 1.<\/pre>\n<h2>2. Read document contents (binary data)<\/h2>\n<pre lang=\"abap\">DATA:\r\n  lt_cvapi_files   TYPE TABLE OF cvapi_doc_file,\r\n  lt_bapi_files    TYPE t_bapi_doc_files2,\r\n  lt_drao          TYPE dms_tbl_drao,\r\n  ls_message       TYPE messages,\r\n  lt_orblk         TYPE TABLE OF orblk,\r\n  lv_excel_xstring TYPE xstring\r\n  lv_data_size     TYPE i.\r\n\r\nFIELD-SYMBOLS:\r\n  &lt;ls_drao&gt;  TYPE drao,\r\n  &lt;ls_orblk&gt; TYPE orblk.\r\n\r\n* Convert BAPI file structure to CVAPI file structure\r\nAPPEND &lt;ls_document_file&gt; TO lt_bapi_files.\r\nCALL FUNCTION 'MAP2I_BAPI_FILE2_TO_API_FILE'\r\n  TABLES\r\n    bapi_doc_file = lt_bapi_files\r\n    api_doc_file  = lt_cvapi_files.\r\n\r\n* Get binary data from system\r\n* Content provider is set to 'TBL' \r\n* -&gt; tabular data filled in lt_drao\r\nCALL FUNCTION 'CVAPI_DOC_CHECKOUTVIEW'\r\n  EXPORTING\r\n    pf_dokar           = &lt;ls_document_file&gt;-documenttype\r\n    pf_doknr           = &lt;ls_document_file&gt;-documentnumber\r\n    pf_dokvr           = &lt;ls_document_file&gt;-documentversion\r\n    pf_doktl           = &lt;ls_document_file&gt;-documentpart\r\n    pf_content_provide = 'TBL'\r\n  IMPORTING\r\n    psx_message        = ls_message\r\n  TABLES\r\n    pt_files           = lt_cvapi_files\r\n    ptx_content        = lt_drao\r\n  EXCEPTIONS\r\n    error_message      = 1\r\n    OTHERS             = 2.\r\n\r\n* Get the binary data and its length\r\nLOOP AT lt_drao ASSIGNING &lt;ls_drao&gt;.\r\n  APPEND INITIAL LINE TO lt_orblk ASSIGNING &lt;ls_orblk&gt;.\r\n  &lt;ls_orblk&gt; = &lt;ls_drao&gt;-orblk.\r\n\r\n  IF lv_data_size IS INITIAL.\r\n    lv_data_size = &lt;ls_drao&gt;-orln.\r\n  ENDIF.\r\nENDLOOP.\r\n\r\n* Convert binary data from itab to xstring\r\nCALL FUNCTION 'SCMS_BINARY_TO_XSTRING'\r\n  EXPORTING\r\n    input_length       = lv_data_size\r\n  IMPORTING\r\n    BUFFER             = lv_excel_xstring\r\n  tables\r\n    binary_tab         = lt_orblk.<\/pre>\n<h2>3. Initialize the WDY Office Control<\/h2>\n<p>As we initially presumed &#8211; we have a WDY component with one View which contains the <em>OfficeControl<\/em> of type MS_EXCEL element in the Layout.<\/p>\n<p>Now we have to create a context Attribute where we will store the Excel binary data.<\/p>\n<ol>\n<li>In the View&#8217;s Context tab create new Attribute of type <em><strong>XSTRING<\/strong> <\/em>and name it e.g. <em><strong>XLS_DATA<\/strong><\/em><\/li>\n<li>In the View&#8217;s Layout select the OfficeControl and bind its <strong><em>dataSource<\/em> <\/strong>to the context attribute <em><strong>XLS_DATA<\/strong><\/em><\/li>\n<\/ol>\n<p>If you reached this point, you already know how to read the Excel data, you know how to integrate OfficeControl to the WDY view and bind it to the view&#8217;s Context attribute. Now we just have to fill the Context attribute with data we&#8217;ve already prepared before &#8211; you can put the following piece of code e.g. in the View&#8217;s WDDOMODIFYVIEW method in case you want to initialize the Excel control in the WDY right from the start<\/p>\n<pre lang=\"abap\">wd_context-&gt;set_attribute(\r\n  EXPORTING\r\n    value = lv_excel_xstring\r\n    name  = 'XLS_DATA'\r\n).<\/pre>\n<p>Now if you try to run your WDY application in browser, you should see the Excel being displayed within the browser window and loaded with data of the XLS sheet from your DMS document.<\/p>\n<h2>4. Download XLS as attachment<\/h2>\n<p>Once we have our Excel data loaded into browser, we might also want to download the updated data to local file. This can be done e.g. by adding a button on the View&#8217;s layout and to the Action assigned to the new button you&#8217;ll put the following code<\/p>\n<pre lang=\"abap\">DATA:\r\n  lv_content  TYPE xstring,\r\n  lv_filename TYPE string,\r\n  lv_ctype    TYPE string.\r\n\r\nlv_filename = 'My_XLS_Sheet.xlsx'.\r\nlv_ctype = 'xlsx'.\r\n\r\n* Access data stored in View's context \r\nwd_context-&gt;get_attribute(\r\n  EXPORTING\r\n    name  = 'DATA'\r\n  IMPORTING\r\n    value = lv_content\r\n).\r\n\r\ncl_wd_runtime_services=&gt;attach_file_to_response(\r\n  EXPORTING\r\n    i_filename      = lv_filename\r\n    i_content       = lv_content\r\n    i_mime_type     = lv_ctype\r\n    i_in_new_window = abap_true\r\n).<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;ll show you how to retrieve an Excel sheet from SAP DMS (Document management System) where it is stored as &#8220;original&#8221; in a Document container.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"WebDynpro 4 ABAP - Open Excel sheet from DMS","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,9,359],"tags":[369,370,368,304,367,371,366,309],"class_list":["post-1219","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","category-webdynpro-abap","tag-bapi_document_getlist2","tag-cvapi_doc_checkoutview","tag-dms","tag-excel","tag-officecontrol","tag-scms_binary_to_xstring","tag-wdy","tag-webdynpro"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-jF","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1219"}],"version-history":[{"count":6,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1219\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1219\/revisions\/1226"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}