Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Need to send xlsx email attachment in zip format, currently using CL_ABAP_ZIP

anreddy
Explorer
0 Kudos

Hi All

     My initial requirement is to send an .xls file email attachment in zip format. for that i have used the class CL_ABAP_ZIP methods and able to create an .xls file in zip format. when i try to open the created excel file it is giving the below warning.

I am not sure why this is coming .. can any one help me to surpass this warning dialog.??

Also now the user is asking for the .XLSX file attachment, can we do that using CL_ABAP_ZIP class ??

My sample code :

step1: First i am concatenating my internal table headers and data into a string.( i need column headers also in excel file ).

CONCATENATE l_string l_text INTO l_string.

Step 2: Converting the above string to xstring.

      CALL METHOD cl_bcs_convert=>string_to_xstring

        EXPORTING

          iv_string  = p_dlrdata

        RECEIVING

          ev_xstring = l_xstring.

Step 3:

*Prepare Mail Object

  CLASS cl_bcs DEFINITION LOAD.

  TRY.

      CLEAR:lo_bcs_exception.

      lo_send_request = cl_bcs=>create_persistent( ).

    CATCH cx_send_req_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

    CATCH cx_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

Step 4:

TRY.

      CLEAR:lo_bcs_exception.

*Create Email document

      lo_document = cl_document_bcs=>create_document(

      i_type = 'RAW'

      i_text =  li_message

      i_subject = l_subject " Email subject

      i_language   = sy-langu ).

    CATCH cx_document_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

  TRY.

      CLEAR:lo_bcs_exception.

* Pass the document to send request

      lo_send_request->set_document( lo_document ).

    CATCH cx_send_req_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

    CATCH cx_document_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

Step 5:

  CREATE OBJECT lo_zipper.

  CONCATENATE l_subject '.XLS' INTO l_file.

*  move l_subject to l_file.

  CONDENSE l_file.

  CALL METHOD lo_zipper->add

    EXPORTING

      name    = l_file

      content = l_xstring.

  CALL METHOD lo_zipper->save

    RECEIVING

      zip = l_xstring.

Step 6:

TRY.

      CLEAR:lo_bcs_exception.

* Convert Xstring into Binary

      CALL METHOD cl_bcs_convert=>xstring_to_solix

        EXPORTING

          iv_xstring = l_xstring

        RECEIVING

          et_solix   = li_binary_content.

    CATCH cx_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

* Create Attachment

  TRY.

      CLEAR:lo_bcs_exception.

      lo_document->add_attachment( EXPORTING

                                      i_attachment_type = 'ZIP'

                                      i_attachment_subject = l_zip_name

                                      i_attachment_size = l_len

                                      i_att_content_hex = li_binary_content  ).

    CATCH cx_document_bcs  INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

It would be great if any one help me on this issue, how can i create .XLSX file ..

at this point of time i want to make changes to the existing code rather than trying complete new code

19 REPLIES 19

AmarUbhe
Advisor
Advisor
0 Kudos

Hi Nageswara,

first of all you have to check XLSX coversion done properly or not.

see every binary file send we need to follow following steps.

1.convert string to xstring                     ---you did for XLSX

2.convert xstring to to binary.                --no code for this for XLSX

0 Kudos

Hi Amar,

     I am doing those necessary conversions.

is there any way to check it?

rosenberg_eitan
Active Contributor
0 Kudos

Hi,

The file might be a plain CSV or TXT with an extension of .xlsx

This is a security check done by Excel .

Rename the file to CSV or TXT .

Regards.

0 Kudos

Hi Eitan,

     I am creating the .xls file in my program itself.

0 Kudos

Hi,

I suggest you look here:

https://en.wikipedia.org/wiki/Comma-separated_values

Look at the "Basic rules and examples"

The file needs to have CSV extension.

Excel recognize this format so no warning will be issue.

Try to open Excel file using some text editor and look at it. It is a binary file (actually a zip file) .

Regards .

a sample from

Comma-separated values - Wikipedia, the free encyclopedia

0 Kudos

Hi,

     Thanks for your information.

     In my program i am converting my internal table date into string with horizantal tabs, so please suggest me in my case how can i skip that warning.

     I have tried with CSV format, it is creating successfully and no warning is coming but the data is not coming in the expected excel format it is all coming in rows.

Please help me on this.

Thanks

Nageswar

0 Kudos

Hi,

The attachment is saved.

      

Here we have a tab separator file the sufix is txt.

If we open it in excel we do not receive any message and the file looks ok .

You might need to go through the import dialog.

We do not have to lie to excel

Regards.

Juwin
Active Contributor
0 Kudos

Hi Nageswara,

XLS is a proprietary format of Microsoft. You cannot create a XLS file just by using ABAP. Earlier, most people used to create text files from ABAP and rename it as XLS files. Such files will show warning message like the one you are getting, when you try to open using Excel.

XLSX is the most advanced version of Excel file and it uses open source. This allows proper creation of XLSX files from ABAP. For Eg. To convert a table to Excel file, you can take help of CL_SALV_TABLE class (Office 2007 XLSX Support | SCN). Similarly, to download a hierarchical display to Excel file, you can take help of this blog (SALV Tree to Excel (xlsx) | SCN).

XLSX files are already in ZIP format, so you do not have to zip them again. But, surely you can use CL_ABAP_ZIP to zip the file - it is just not necessary.

Proper XLSX files when opened in Excel, will not show the warning message.

Thanks,

Juwin

0 Kudos

Hi Juwin,

     I am using the below code and creating .xls file with string data input

  CREATE OBJECT lo_zipper.

  CONCATENATE l_subject '.XLS' INTO l_file.

*  move l_subject to l_file.

  CONDENSE l_file.

  CALL METHOD lo_zipper->add

    EXPORTING

      name    = l_file

      content = l_xstring.

You can check my above complete code. I am able to generate .xls file with in the program.

Now i have to points,

1. can we skip that warning message?

2.  how can we create .xlsx file with in the program using CL_ABAP_ZIP.

Thanks

Nageswar

Juwin
Active Contributor
0 Kudos

Hi, Looking at your statement above, you are creating a tab separated text file with XLS extension. That doesn't mean the file is really an XLS file. You are just creating a text file with wrong extension code. This is the warning you are getting in Excel when you try to open the file. You have to use the classes I mentioned above to create a proper XLSX file to avoid the warning.

Thanks

0 Kudos

Hi Juwin,

     If possible can you provide me any sample code to create .xls file, it would be very help full for me.

Thanks

Juwin
Active Contributor
0 Kudos

As I have told you already, you cannot create XLS file just by using ABAP. The sample to code for creating XLSX file is in the links which I provided earlier.

Thanks,

Juwin

0 Kudos

Hi Juwin,

     I checked the links provided by you those were good, but one point here i want to mention is i need to ZIP the XLSX file and send it as attachment.

Any help on that would be greatly helpful for me.

Thanks

Nageswar

Juwin
Active Contributor
0 Kudos

Hi Nageswara,

The method you had used for ZIP was correct in your code. Please follow the same.

Thanks,

Juwin

0 Kudos

Hi Juwin,

          But i am not getting the desired format XLSX, through my code i am getting only XLS file in ZIP format, what should i change in my code to get XLSX file in ZIP format.

Thanks

Nageswar

Juwin
Active Contributor
0 Kudos

Hi, Can you please paste your updated code here?

Thanks

0 Kudos

HI ,

     Please check the starting post i have pasted my code there.

Thanks

Nageswar

0 Kudos

Here is the code:

step1: First i am concatenating my internal table headers and data into a string.( i need column headers also in excel file ).

CONCATENATE l_string l_text INTO l_string.

Step 2: Converting the above string to xstring.

      CALL METHOD cl_bcs_convert=>string_to_xstring

        EXPORTING

          iv_string  = p_dlrdata

        RECEIVING

          ev_xstring = l_xstring.

Step 3:

*Prepare Mail Object

  CLASS cl_bcs DEFINITION LOAD.

  TRY.

      CLEAR:lo_bcs_exception.

      lo_send_request = cl_bcs=>create_persistent( ).

    CATCH cx_send_req_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

    CATCH cx_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

Step 4:

TRY.

      CLEAR:lo_bcs_exception.

*Create Email document

      lo_document = cl_document_bcs=>create_document(

      i_type = 'RAW'

      i_text =  li_message

      i_subject = l_subject " Email subject

      i_language   = sy-langu ).

    CATCH cx_document_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

  TRY.

      CLEAR:lo_bcs_exception.

* Pass the document to send request

      lo_send_request->set_document( lo_document ).

    CATCH cx_send_req_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

    CATCH cx_document_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

Step 5:

  CREATE OBJECT lo_zipper.

  CONCATENATE l_subject '.XLS' INTO l_file.

*  move l_subject to l_file.

  CONDENSE l_file.

  CALL METHOD lo_zipper->add

    EXPORTING

      name    = l_file

      content = l_xstring.

  CALL METHOD lo_zipper->save

    RECEIVING

      zip = l_xstring.

Step 6:

TRY.

      CLEAR:lo_bcs_exception.

* Convert Xstring into Binary

      CALL METHOD cl_bcs_convert=>xstring_to_solix

        EXPORTING

          iv_xstring = l_xstring

        RECEIVING

          et_solix   = li_binary_content.

    CATCH cx_bcs INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

* Create Attachment

  TRY.

      CLEAR:lo_bcs_exception.

      lo_document->add_attachment( EXPORTING

                                      i_attachment_type = 'ZIP'

                                      i_attachment_subject = l_zip_name

                                      i_attachment_size = l_len

                                      i_att_content_hex = li_binary_content  ).

    CATCH cx_document_bcs  INTO lo_bcs_exception.

      IF lo_bcs_exception IS NOT INITIAL.

        CLEAR:g_email_success.

      ENDIF.

  ENDTRY.

Tomas_Buryanek
Active Contributor
0 Kudos

It the same problem all over again.

Warning you are getting in Excel is OK. Because your file with .xls extension is not in any XLS format.

You are creating text file. In CSV format but it is tabulator separated intead of comma separated.

Solutions were already offered to you by other users...

ZIP is not a problem. It has nothing to do with it.

-- Tomas --