Download an Excel document built with class CL_XLSX_DOCUMENT - abap

My program below creates an Excel document built with the class CL_XLSX_DOCUMENT, which contains two sheets.
How can I download the Excel document on my laptop ?
lo_worksheetpart ?= lo_workbookpart->get_worksheetparts( )->get_part( lv_line_no_loop ).
lo_worksheetpart_1 ?= lo_workbookpart->add_worksheetpart( ).
lv_sheetxml = lo_worksheetpart->get_data( ).
lv_sheetxml_2 = lo_worksheetpart_1->get_data( ).
lo_sharedstringspart = lo_workbookpart->get_sharedstringspart( ).
lv_sharedxml = lo_sharedstringspart->get_data( ).
"Parse and replace
if lt_nameval is not initial.
lv_resultxml = update_shared_string_1(
EXPORTING
iv_xml = lv_sharedxml
iv_node = co_shared_string
it_nameval = lt_nameval ).
"write new excel
lo_sharedstringspart->feed_data( lv_resultxml ).
lv_resultxml = update_sheet_data_1(
EXPORTING
iv_xml = lv_sheetxml
iv_node = co_sheet_data
it_days = lt_days
it_nameval = lt_nameval ).
"write new excel
lo_worksheetpart->feed_data( lv_resultxml ).
endif.

I use the following:
DATA:
lv_fsize TYPE i,
lt_data TYPE tsfixml,
lv_resultxml TYPE xstring.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_resultxml
IMPORTING
output_length = lv_fsize
TABLES
binary_tab = lt_data.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_fsize
filetype = 'BIN'
filename = 'C:\Temp\filename.xlsx
CHANGING
data_tab = lt_data

Related

Attach XSTRING file as GOS?

I have created a SICF Service for GOS.
Basically it takes a pdf on frontend, converts it to base64 or binary and send it to the backend.
Now on the backend I have that PDF as xstring and want to create a business document "attachment".
Manually it's quite easy with the FM ARCHIV_CREATE_FILE / ARCHIVOBJECT_CREATE_FILE, but they need the file path as input param.
How can I use my xstring as "filepath" to attach those data?
Try ARCHIV_CREATE_TABLE module:
DATA: ls_toadt TYPE toadt,
lv_flength TYPE sapb-length,
lv_number TYPE i.
DATA: lt_binary TYPE STANDARD TABLE OF tbl1024.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = iv_pdf " your PDF xstring!
TABLES
binary_tab = lt_binary.
DESCRIBE TABLE lt_binary LINES lv_number.
lv_flength = lv_number * 1024.
CALL FUNCTION 'ARCHIV_CREATE_TABLE'
EXPORTING
ar_object = ar_object
object_id = object_id
sap_object = sap_object
flength = lv_flength
IMPORTING
outdoc = ls_toadt
TABLES
binarchivobject = lt_binary
EXCEPTIONS
error_archive = 01
error_communicationtable = 02
error_connectiontable = 03
error_kernel = 04
error_parameter = 05
OTHERS = 06.

How to create a txt-file on the application server filled with an internal table?

I'm learning ABAP at the moment and got the task to build a function that creates a .txt file or a .csv file from an internal table and save it on the application server.
I know you can use, for example, GUI_DOWNLOAD for that, but the task is to build my own function to do that.
So I got an internal table filled and I want that to be saved as .txt file on the AS. I'm coding in Eclipse, BTW.
Anybody has an idea how to do that, using a function?
EDIT:
Here's what I tried already:
I created a function in the function builder. On the import parameters I put a parameter with the name "lv_mytab" with type table.
The source code of my function looks like this:
*"-------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*" IMPORTING
*" REFERENCE(LV_MYTAB) TYPE TABLE
*"-------------------------------------------------------------------
DATA(myfile) = '/usr/sap/S42/data/textfile.txt'.
OPEN DATASET myfile FOR OUTPUT IN TEXT MODE ENCODING DEFAULT WITH SMART LINEFEED.
LOOP AT lv_mytab into lt_table.
TRANSFER lt_table to myfile.
CLOSE DATASET myfile.
In my program I tried calling the function like this:
CALL FUNCTION 'EXPORT_TXT_CSV_MR'
EXPORTING
lv_mytab = lt_summe.
lt_summe is the internal table I want to be exported as txt or csv.
Something like this:
OPEN DATASET lv_p_app FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT gt_error INTO gs_final.
TRANSFER gs_final TO lv_p_app.
ENDLOOP.
CLOSE DATASET lv_p_app.
ENDIF.
More samples here https://answers.sap.com/questions/3704234/download-internal-table-onto-the-application-serve.html
Okay with the help of a fellow student I got it working.
Heres the solution I found:
Code source of the function:
FUNCTION EXPORT_TXT_CSV_MR.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*" IMPORTING
*" REFERENCE(S_FILENAME) TYPE STRING
*" CHANGING
*" REFERENCE(GT_MYTAB) TYPE STANDARD TABLE
*" REFERENCE(GD_VERARBEITUNG) TYPE INT4
*"----------------------------------------------------------------------
DATA:
ld_filename TYPE string,
ld_datei type standard table of sbook,
ld_Zeile like line of ld_datei,
gd_useraction TYPE i,
lt_Ausgabe TYPE STANDARD TABLE OF sbook.
CONCATENATE '/usr/sap/S42/data/' s_filename into ld_filename.
IF gd_Verarbeitung = 1.
ld_datei = gt_mytab.
OPEN DATASET ld_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT ld_datei into ld_Zeile.
Data(ld_string) = | Partnernummer: { ld_Zeile-customid } Betrag: { ld_Zeile-forcuram } Waehrung: { ld_Zeile-forcurkey } Name: { ld_Zeile-passname }|.
Transfer ld_string to ld_filename.
ENDLOOP.
Close dataset ld_filename.
ElseIF gd_Verarbeitung = 2.
ld_datei = gt_mytab.
*ld_filename = '/usr/sap/S42/data/CSVFile_MR.csv'.
Open Dataset ld_filename for output in text mode encoding default with SMART LINEFEED.
LOOP AT ld_datei into ld_Zeile.
ld_string = | Partnernummer: { ld_Zeile-customid } Betrag: { ld_Zeile-forcuram } Waehrung: { ld_Zeile-forcurkey } Name: { ld_Zeile-passname }|.
Transfer ld_string to ld_filename.
ENDLOOP.
Close dataset ld_filename.
ENDIF.
ENDFUNCTION.
And in my program I call it like this:
if p_txt = 'X'.
ld_txtcsv = 1.
CALL FUNCTION 'EXPORT_TXT_CSV_MR'
EXPORTING
s_filename = 'TXTFile_MR.txt'
CHANGING
gt_mytab = lt_summe
gd_verarbeitung = ld_txtcsv.
clear ld_txtcsv.
endif.
if p_csv = 'X'.
ld_txtcsv = 2.
CALL FUNCTION 'EXPORT_TXT_CSV_MR'
EXPORTING
s_filename = 'CSVFile_MR.csv'
CHANGING
gt_mytab = lt_summe
gd_verarbeitung = ld_txtcsv.
clear ld_txtcsv.
ENDIF.
Thanks for all of your help! I got it now! :)

Open Document returns error (SOFFICEINTEGRATION)

I'm trying to open a Excel file with SOFFICEINTEGRATION to modify the file inplace.
But I always get the CALL_NOT_FLUSHED error, I tried several code snippets (with no_flush = X and no_flush = ' ')
The error "CALL_NOT_FLUSHED" is appearing after calling proxy->open_document
My current code:
DATA proxy TYPE REF TO i_oi_document_proxy.
PARAMETERS dummy.
AT SELECTION-SCREEN OUTPUT.
IF proxy IS NOT BOUND.
CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING
control = DATA(lo_container_control)
retcode = DATA(l_rc).
CALL METHOD lo_container_control->init_control
EXPORTING
r3_application_name = 'EXCEL' "#EC NOTEXT
inplace_enabled = 'X'
inplace_scroll_documents = 'X'
parent = cl_gui_container=>screen0
IMPORTING
retcode = l_rc.
CALL METHOD lo_container_control->get_document_proxy
EXPORTING
document_type = 'EXCEL.SHEET'
IMPORTING
document_proxy = proxy.
CALL METHOD proxy->open_document
EXPORTING
document_url = 'file://C:\Users\yourusername\Documents\test.xlsx'
open_inplace = 'X'
IMPORTING
error = DATA(openerr).
ENDIF.

Add new emails Customer at XD02 from CALL METHOD cmd_ei_api=>maintain_bapi

I need to add a new email address on customers in XD02 from an xls.
That's all ok, but, when I CALL METHOD cmd_ei_api=>maintain_bapi
this really change the email but delete all the emails on XD02, and it's not what I want, I want to add a new email put this one default, but I want to keep the old ones.
My code :
FORM data_to_bapi.
DATA: gs_correct TYPE cmds_ei_main,
gt_customers TYPE cmds_ei_main,
gs_address TYPE bapiad1vl,
gs_addressx TYPE bapiad1vlx,
gs_company_code_st TYPE cmds_ei_company,
gs_company_code TYPE cmds_ei_cmd_company,
gt_smtp TYPE cvis_ei_smtp_t,
gs_smtp LIKE LINE OF gt_smtp,
gs_comm TYPE cvis_ei_cvi_communication,
gs_customers TYPE cmds_ei_extern,
gs_defective TYPE cmds_ei_main,
gs_msg_correct TYPE cvis_message,
gs_msg_error TYPE cvis_message,
iv_test_run TYPE c.
LOOP AT lt_data INTO wa_data.
"Controlo
gs_customers-header-object_instance-kunnr = wa_data-kunnr. "kunnr
gs_customers-header-object_task = 'U'. "Update this kunnr
gs_smtp-contact-task = 'I'. " Insert New Email
gs_smtp-contact-data-e_mail = wa_data-email. " New email
gs_smtp-contact-datax-e_mail = 'X'.
APPEND gs_smtp TO gt_smtp.
gs_comm-smtp-smtp = gt_smtp[].
gs_customers-central_data-address-communication = gs_comm.
gs_customers-central_data-address-task = 'I'. " Insert new communication
APPEND gs_customers TO gt_customers-customers.
**********************************************************************
* CALL BAPI *
**********************************************************************
CHECK gt_customers-customers IS NOT INITIAL.
gv_collect_messages = abap_true.
cmd_ei_api=>initialize( ).
iv_test_run = ' '.
CALL METHOD cmd_ei_api=>maintain_bapi
EXPORTING
iv_test_run = iv_test_run
iv_collect_messages = gv_collect_messages
is_master_data = gt_customer
" Master Data
IMPORTING
es_master_data_correct = gs_correct
es_message_correct = gs_msg_correct
es_master_data_defective = gs_defective
es_message_defective = gs_msg_error.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
CLEAR wa_data.
ENDLOOP.
ENDFORM. "data_to_bapi
Thanks !

Call AFIP MTXCA web-service properly from SAP ECC

I am trying to call the AFIP WebService MTXCA directly from SAP ECC.
There is no problem with that. The thing is when I try to get the TOKEN and SIGN from the first WS
https://wsaahomo.afip.gov.ar/ws/services/LoginCms?WSDL
I can not sign the XML to build the right CMS to get the proper answer from AFIP.
cl_secxml_helper=>upload_file( EXPORTING filename = p_xml
IMPORTING bindata = DATA(lv_xml) ).
DATA(lo_object) = cl_sec_sxml_dsignature=>create_reader_instance( if_input = lv_xml ).
lo_object->m_ssf_hash_algorithm = 'SHA1'.
lo_object->m_dsig_hash_algorithm = 'SHA1'.
lo_object->m_dsig_method = 'RSA'.
lo_object->m_signature_ns_prefix = ''.
lo_object->sign_xml( EXPORTING if_ssf_app = 'OAUTH'
if_add_keyinfo = abap_true
if_add_keyinfo_ex = abap_true
IMPORTING ef_signature_xml = DATA(lf_result)
es_signer = DATA(ls_signer) ).
lo_object->embed_signature( EXPORTING if_xml = lv_xml
if_signature = lf_result
if_embed_as_child = abap_true
if_embed_at_end = abap_true
is_signer = ls_signer
IMPORTING ef_result = lf_result ).
cl_soap_xml_helper=>xml_show( xdoc = lf_result ).
ls_request-in0 = lf_result.