Create XLSX - MHTML File From Internal Table in Background
Create XLSX - MHTML File From Internal Table in Background
Tweets by @abapblog
Łukasz Pęgiel
Retweeted
source=http%3A%2F%2Fabapblog.com&text=:%20http%3A%2F%2Fabapblog.com&via=abapblog)
YouTube @YouTube (https://plus.google.com/share?
mini=true&url=http%3A%2F%2Fabapblog.com&title=&summary=&source=http%3A%2F%2Fabapblog.com) (https://pinboard.in/popup_login/?
url=http%3A%2F%2Fabapblog.com&title=&description=)
30 Mar 2018
Home (/) / Articles (/articles) / Tricks (/articles/tricks)
/Łukasz
Create
PęgielXLSX/MHTML file from internal table in background (http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-
Retweeted
internal-table-in-background)
Vitaliy Rudnytskiy
@Sygyzmundovych
List of #sitWRO sessions is
Attachments:
ZAB_MHTML_XLS.nugg (http://abapblog.com/files/nugg/ZAB_MHTML_XLS.nugg)
(http://abapblog.com/files/nugg/ZAB_MHTML_XLS.nugg)
ZCL_ABAPBLOG_COM.nugg (http://abapblog.com/files/nugg/ZCL_ABAPBLOG_COM.nugg)
(http://abapblog.com/files/nugg/ZCL_ABAPBLOG_COM.nugg)
searchword=SCMS_XSTRING_TO_BINARY&ordering=&searchphrase=all)
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 1/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
109 Comments
I think that any of us had meet the situation when we needed to create an Excel output from internal table in background. There is a really nice project called
ABAP2XLSX which gives you to possibility to do all you need but in some case you won't be allowed to install ABAP2XLSX at you SAP instance. Don't worry there is a
way to do it, but in older SAP version we will be allowed only to save the file as MHTML excel (like in ALV->Export to Spreadsheet). In newest version we're able save
the file directly to XLSX also. In a method shown bellow you can see that to create Excel file you need to only pass internal table, the rest of the parameters are option
which gives you opurtinity to pass the settings you've made in your program to the output file.
So what am I doing here.... firstly I check if the fieldcatalog was passed. If not then I create it on a base of internal table with usage of cl_salv_table and
cl_salv_controller_metadata=>get_lvc_fieldcatalog. After I finally have the fieldcatalog I create result table with sort and filter criteria if they were passed. To
do that I use class cl_salv_ex_util. At the end result table is transformed to xstring with method cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform. So
you're receiving at the end of the method an xstring file so you can do what you want with it, for example send via mail to the user, or save it on local PC (like in the
example found at the end). The most important is that you can use this method in a background so you can prepare some jobs for your programs and send
results of them to the users!
Importing:
it_fieldcat type lvc_t_fcat optional -> field catalog for list viewer control
it_sort type lvc_t_sort optional -> alv control: table of sort criteria
it_filt type lvc_t_filt optional -> alv control: table of filter conditions
is_layout type lvc_s_layo optional -> alv control: layout structure
i_xlsx type flag optional -> create xlsx file?
Changing:
Exporting:
Implementation:
method create_xls_from_itab.
endtry.
"get colums & aggregation infor to create fieldcat
mo_columns = mo_salv_table->get_columns( ).
mo_aggreg = mo_salv_table->get_aggregations( ).
mt_fcat = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
r_columns = mo_columns
r_aggregations = mo_aggreg ).
else.
*else we take the one we passed
mt_fcat[] = it_fieldcat[].
endif.
if cl_salv_bs_a_xml_base=>get_version( ) eq if_salv_bs_xml=>version_25 or
cl_salv_bs_a_xml_base=>get_version( ) eq if_salv_bs_xml=>version_26.
mo_result_data = cl_salv_ex_util=>factory_result_data_table(
r_data = mt_data
s_layout = is_layout
t_fieldcatalog = mt_fcat
t_sort = it_sort
t_filter = it_filt
).
case cl_salv_bs_a_xml_base=>get_version( ).
when if_salv_bs_xml=>version_25.
m_version = if_salv_bs_xml=>version_25.
when if_salv_bs_xml=>version_26.
m_version = if_salv_bs_xml=>version_26.
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 2/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
endcase.
"if we flag i_XLSX then we'll create XLSX if not then MHTML excel file
if i_xlsx is not initial.
m_file_type = if_salv_bs_xml=>c_type_xlsx.
else.
m_file_type = if_salv_bs_xml=>c_type_mhtml.
endif.
m_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export.
"transformation of data to excel
call method cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
exporting
xml_type = m_file_type
xml_version = m_version
r_result_data = mo_result_data
xml_flavour = m_flavour
gui_type = if_salv_bs_xml=>c_gui_type_gui
importing
xml = e_xstring.
endif.
endmethod.
Example of use:
report zab_mhtml_xls.
cl_gui_frontend_services=>file_save_dialog(
exporting
* window_title = window_title
default_extension = 'XLS'
* default_file_name = default_file_name
* with_encoding = with_encoding
* file_filter = file_filter
* initial_directory = initial_directory
* prompt_on_overwrite = 'X'
changing
filename = g_filename
path = g_path
fullpath = p_path
* user_action = user_action
* file_encoding = file_encoding
exceptions
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
others = 4
).
if sy-subrc <> 0.
* Implement suitable error handling here
endif.
start-of-selection.
"fill table with data
select * from spfli into corresponding fields of table gt_spfli.
cl_gui_frontend_services=>gui_download(
exporting
bin_filesize = g_size
filename = p_path
filetype = 'BIN'
changing
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 3/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
data_tab = gt_bintab
exceptions
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
others = 24
).
if sy-subrc <> 0.
* Implement suitable error handling here
endif.
endif.
Enjoy!
Sort by Newest
Recommend 9 ⤤ Share
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
I have one qustion, do you think the excel file can be created as "read only" file?
△ ▽ • Reply • Share ›
Cheers
Łukasz
△ ▽ • Reply • Share ›
Fantastic Blog, is this solution suitable for reading XLS/XLSX files from Application server ?
That would be great if its possible?any workarounds?, because I have requirement to read password protected workbooks from AL11 directly,
which is not possible even through ABAP2XLSX library too.
Cheers,
Suresh Kumar
△ ▽ • Reply • Share ›
Excel hat die Überprüfung und Reparatur auf Dateiebene abgeschlossen. Einige Teile dieser Arbeitsmappe wurden repariert oder verworfen.
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 4/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
ce at d e Übe p ü u g u d epa atu au ate ebe e abgesc osse ge e e d ese be ts appe u de epa e t ode e o e
woran liegt das? Version? ich benutze Excel Professional 2013 und 2016. Vielen Dank
△ ▽ • Reply • Share ›
The get_Version method result is 2.6. I chanded it to 2.7. after then Excel could not open the file.
EXPORTING
buffer = i_file
TABLES
binary_tab = lt_binary_file.
△ ▽ • Reply • Share ›
Warm Regards
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 5/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
Warm Regards,
Manoj Sahu.
△ ▽ • Reply • Share ›
Cheers
Łukasz
△ ▽ • Reply • Share ›
Manoj.
△ ▽ • Reply • Share ›
I should create Excel with more sheet . I have three internal Tables and for everyone i have create xls File.
Now Before downloading, i would create one file excel with 3 Sheets (because i have three internal tables) .
How can i concatenate the different xstring in my different Sheet ??
Can u help me???
Thanks man
ABAP Junior
△ ▽ • Reply • Share ›
It's was really a very good article. Thanks for sharing this to everyone.
I had a question on email attachment, Prasad helped me on that part. I am facing a issue now. I ran for two sets of data
1) which has 10K records - result was successful(able to open attachment).
2) which has 30K records - can see the attachment but unable to open it. I get the error as 'Database error for <get data="" from="" kpro=""> <>'. I
am assuming this might be due to large file size. I tried to implement the file ZIP logic, but something went wrong. Can you please suggest me
with the logic or any logic I can implement.
Thanks,
Dheeru.
△ ▽ • Reply • Share ›
Cheers
Łukasz
△ ▽ • Reply • Share ›
Yeah, the size was the reason why I was unable to open the file. As I cant send 1 large file, I am splitting up the file into multiple
files and sent them. Thank you very much once again for the useful article.
△ ▽ • Reply • Share ›
Can you please share me the code, how to send the xlsx as attachement.
△ ▽ • Reply • Share ›
You can refer Łukasz code upto FM SCMS_XSTRING_TO_BINARY which will give you a binary table. After that use the following
code :
△ ▽ • Reply • Share ›
Cheers
△ ▽ • Reply • Share ›
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 7/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
Le 15 juil. 2017 10:02, "Disqus" <notifications@disqus.net> a écrit :
△ ▽ • Reply • Share ›
⛺
Hi Łukasz,
And yes this solution is applicable if you can't install abap2xlsx. Anyway you can always create mhtml version in older
releases and excel will read it correctly.
△ ▽ • Reply • Share ›
kann ich eine gespeicherte Layout Variante vom Typ SLIS_VARI übergeben?
Danke
------------
can i pass diyplay variant type SLIS_VARI ?
If it is possible, how can I do it?
Thank you
△ ▽ • Reply • Share ›
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 8/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
Now I dont want to use the display method, BUT the method
To generate Excel.
This works well BUT I can not pass the variant p_vari from type SLIS_VARI (disvariant-variant) to the
method create_xls_from_itab.
△ ▽ • Reply • Share ›
lt_fcat = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
r_columns = lo_table->get_columns( )
r_aggregations = lo_table->>get_aggregations( )
).
and now call of create_xls_from_itab.
△ ▽ • Reply • Share ›
ALSO ON ABAPBLOG.COM
ABAP Favorites Eclipse plugin Speed up your coding with ABAP in Eclipse (SITWRO 2017
14 comments • 9 months ago session)
Łukasz Pęgiel — Hello Timo,Technically I think it should not be a 2 comments • 10 months ago
Avatarproblem. I'll check if I can add it easily just as line number or as full adt Łukasz Pęgiel — Probably yes, additionally SAP has based lot's of it's
link.Cheers Łukasz Avatartools on Eclipse, like for example Design studio. The most important it's
better than SAP GUI :-)
back to top
Like 2 Tweet Share
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 10/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
USER (http://abapblog.com/search/?
searchword=USER&ordering=&searchphrase=exact&Itemid=102&option=com_s
SETTINGS (http://abapblog.com/search/?
searchword=SETTINGS&ordering=&searchphrase=exact&Itemid=102&option=com_search) FALV
(http://abapblog.com/search/?
searchword=FALV&ordering=&searchphrase=exact&Itemid=102&option=com_sear
INFO (http://abapblog.com/search/?searchword=INFO&ordering=&searchphrase=exact&Itemid=102&option=com_search) ESRUO (http://abapblog.com/search/?
searchword=ESRUO&ordering=&searchphrase=exact&Itemid=102&option=com_search) COC1 (http://abapblog.com/search/?
searchword=COC1&ordering=&searchphrase=exact&Itemid=102&option=com_search) FEATURE (http://abapblog.com/search/?
searchword=FEATURE&ordering=&searchphrase=exact&Itemid=102&option=com_search) CHECK (http://abapblog.com/search/?
searchword=CHECK&ordering=&searchphrase=exact&Itemid=102&option=com_search) ABAP HTML (http://abapblog.com/search/?searchword=ABAP
HTML&ordering=&searchphrase=exact&Itemid=102&option=com_search) DESCRIBE (http://abapblog.com/search/?
searchword=DESCRIBE&ordering=&searchphrase=exact&Itemid=102&option=com_search) NAME (http://abapblog.com/search/?
ZDEMO (http://abapblog.com/search/?
searchword=NAME&ordering=&searchphrase=exact&Itemid=102&option=com_search)
searchword=ZDEMO&ordering=&searchphrase=exact&Itemid=102&option=com_search) FILL
(http://abapblog.com/search/?searchword=FILL&ordering=&searchphrase=exact&Itemid=102&option=com_search) MDPA (http://abapblog.com/search/?
FROM (http://abapblog.com/search/?
searchword=MDPA&ordering=&searchphrase=exact&Itemid=102&option=com_search)
Follow @abapblog
abapblog
Follow
abapblog.com
91 polubienia
(http://marketplace.eclipse.org/marketplace-client-intro?mpc_install=3522403)
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 11/12
4/4/2018 ABAPblog.com - Create XLSX/MHTML file from internal table in background
-38%
ABAP code and articles provided on http://abapblog.com (http://abapblog.com), if it is not statet otherwise, were created by Łukasz Pęgiel. You can use the code in your SAP instance for commercial and non-commercial use without any warranty from side of the
author. You cannot sell the code as a full program or a part of it.
Replicating of the articles and code is prohibited unless the agreement of the author is given to you.
http://abapblog.com/articles/tricks/33-create-xlsx-mhtml-file-from-internal-table-in-background 12/12