Skip to Content
0

Function module that supports XLSX file attachment to open in outlook mobiles from sap.

Dec 13, 2016 at 01:36 PM

191

avatar image
Former Member

Hi Abapers,

I need to know if there is any function module that supports XLSX file to open from outlook attachment that is sent from sap. I have a report which has a excel attachment with 5 sheets. Users are complaining that excel file which is going in their outlook mail as attachment is not opening from their mobile. They can open from their laptops or desktops but not from their mobiles. I have used ixml to create 5 sheets in a single excel file. I am sharing my present existing code to send this excel attachment from sap to outlook. Please someone help me to get this right or even if it is possible or not.

*-- Convert the XML data into Binary data
LOOP AT I_XML INTO WA_XML.
CONCATENATE V_STRING WA_XML GC_CRLF INTO V_STRING.
ENDLOOP.

TRY.
CL_BCS_CONVERT=>STRING_TO_SOLIX(
EXPORTING
IV_STRING = V_STRING
IV_CODEPAGE = '4110' "'4103' "suitable for MS Excel, leave empty
IV_ADD_BOM = 'X' "for other doc types
IMPORTING
ET_SOLIX = I_BINARY_CONTENT
EV_SIZE = V_SIZE ).
CATCH CX_BCS.
MESSAGE E445(SO).
ENDTRY.

*-- Send email
TRY.

*-- Create persistent send request
SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

*-- Create and set document with attachment
* Create document object from internal table with text
APPEND 'Dear Sir,' TO MAIN_TEXT.
APPEND 'Please find the daily bulk invoice report attachment.' TO MAIN_TEXT.
DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = 'HTM' "'RAW'
I_TEXT = MAIN_TEXT
I_SUBJECT = 'Daily Bulk Invoice Report' ).

*-- Add the spread sheet as attachment to document object
DOCUMENT->ADD_ATTACHMENT(
I_ATTACHMENT_TYPE = 'XLS'
I_ATTACHMENT_SUBJECT = 'Invoice Daily Bulk Report'
I_ATTACHMENT_SIZE = V_SIZE
I_ATT_CONTENT_HEX = I_BINARY_CONTENT ).
* i_attachment_header = lt_att_head ).

*-- Add document object to send request
SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).

*-- Add sender to send request
LV_MAIL = 'eoldirectsales@xyz.com'.
LO_SENDER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( LV_MAIL ).
SEND_REQUEST->SET_SENDER( LO_SENDER ).

*-- Add recipient (e-mail address) 'TO'
CLEAR: LW_MAIL.
LOOP AT LT_MAIL INTO LW_MAIL.
IF NOT LW_MAIL-TO_MAIL_ID IS INITIAL.
CLEAR: MAILTO.
MAILTO = LW_MAIL-TO_MAIL_ID.
RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
SEND_REQUEST->ADD_RECIPIENT(
I_RECIPIENT = RECIPIENT
I_EXPRESS = 'X' ).
ENDIF.
ENDLOOP.

*-- Add recipient (e-mail address) 'CC'
CLEAR: LW_MAIL.
LOOP AT LT_MAIL INTO LW_MAIL.
IF NOT LW_MAIL-CC_MAIL_ID IS INITIAL.
CLEAR: MAILTO.
MAILTO = LW_MAIL-CC_MAIL_ID.
RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
SEND_REQUEST->ADD_RECIPIENT(
I_RECIPIENT = RECIPIENT
I_COPY = 'X' ).
ENDIF.
ENDLOOP.

*-- Set send immediately flag
SEND_REQUEST->SET_SEND_IMMEDIATELY( 'X' ).

*-- Send document
SENT_TO_ALL = SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = 'X' ).
COMMIT WORK.
IF SENT_TO_ALL IS INITIAL.
MESSAGE I500(SBCOMS) WITH MAILTO.
ELSE.
MESSAGE S022(SO).
ENDIF.

*-- Exception handling
CATCH CX_BCS INTO BCS_EXCEPTION.
MESSAGE I865(SO) WITH BCS_EXCEPTION->ERROR_TYPE.

ENDTRY.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Jelena Perfiljeva
Dec 13, 2016 at 08:25 PM
1

ABAP2XLSX?

https://wiki.scn.sap.com/wiki/display/ABAP/abap2xlsx

Can they open other Excel attachments on their cell phones? Maybe it's not an SAP issue?

Show 1 Share
10 |10000 characters needed characters left characters exceeded

abap2xlsx is easy to use -> cf example supplied with abap2xlsx download:

" Create excel instance
CREATE OBJECT lo_excel.

" Get active sheet
lo_worksheet = lo_excel->get_active_worksheet( ).
lo_worksheet->set_title( ip_title = 'Demo01' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'Hello world' ).

" Create xlsx stream
CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_2007.
lv_file = lo_excel_writer->write_file( lo_excel ).

*******************************
* Output *
*******************************

" Convert to binary
lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_file ).
lv_bytecount = xstrlen( lv_file ).

" Save the file
cl_gui_frontend_services=>gui_download( ...
1
avatar image
Former Member Dec 14, 2016 at 04:12 AM
0

Hi Jelena,

If the same file is opened in laptop and saved with xlsx extension and then sent to their outlook, then they are able to open it from their mobiles. But directly if it triggers from my program they are not being able to open from their mobiles. The above link is very useful but can you tell me a solution to this. Even if its not possible i need to tell them as because my program is delivering what was required at the beginning because the mobile issue came later. If this is possible directly from my program please let me know.

Regards,

Arijit

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Excel installed on your laptop is a large application that is capable of opening several file formats. XLSX is latest file format of MS Excel. When users open your file on PC and save as xlsx, it creates a proper format file, which happens to be supported by user's mobile app.

What you are creating is xml file which is compatible with PC application, but not mobile application.

One solution for you would be to look into abap2xlsx project which creates proper xlsx files.

Another solution for you would be to create a file that mobile app supports. I think people who used ixml approach first saved PC Excel file has xml based file format, most probably XML spreadsheet 2003 format, then recreated same xml content from ABAP program. If the mobile app is capable of creating excel files, create a dummy file and save it as a format that is text or xml based. Analyze the file contents and generate similar file using your program.

1
avatar image
Former Member Dec 26, 2016 at 09:00 AM
0

Hi Everyone,

Still struggling with this issue. I am giving my complete code. I need some compatible code with my data declarations and my present report functionality to achieve this requirement (excel file to download in mobiles.) My report is huge as it is for 5 sheets in excel.

REPORT ZSD_DAILY_MAIL_BULK_INV_REPORT.

TYPE-POOLS: IXML.

TYPES: BEGIN OF TY_FINAL,
CGRPT TYPE CHAR50,
SOTPN TYPE KUNNR,
SOTPT TYPE CHAR100,
ASOTP TYPE CHAR100,
SHTPN TYPE KUNNR,
SHTPT TYPE CHAR100,
ASHTP TYPE CHAR100,
LOAPT TYPE WERKS_D,
HSDQT TYPE FKIMG,
HFHSD TYPE FKIMG,
OTHQT TYPE FKIMG,
TOTQT TYPE FKIMG,
ODQTY1 TYPE FKIMG,
ODQTY2 TYPE FKIMG,
ODQTY3 TYPE FKIMG,
ODQTY TYPE FKIMG,
PLNTT TYPE CHAR30,
END OF TY_FINAL.

TYPES: BEGIN OF TY_FINAL1,
CGRPT TYPE CHAR50,
SOTPN TYPE KUNNR,
SOTPT TYPE CHAR100,
ASOTP TYPE CHAR100,
SHTPN TYPE KUNNR,
SHTPT TYPE CHAR100,
ASHTP TYPE CHAR100,
LOAPT TYPE WERKS_D,
TOTQT TYPE FKIMG,
ODQTY TYPE FKIMG,
PLNTT TYPE CHAR30,
END OF TY_FINAL1.

DATA: LT_FINAL1 TYPE STANDARD TABLE OF TY_FINAL,
LT_FINAL1_TEMP TYPE STANDARD TABLE OF TY_FINAL,
LT_FINAL2 TYPE STANDARD TABLE OF TY_FINAL,
LT_FINAL2_TEMP TYPE STANDARD TABLE OF TY_FINAL,
LT_FINAL3 TYPE STANDARD TABLE OF TY_FINAL1,
LT_FINAL3_TEMP TYPE STANDARD TABLE OF TY_FINAL1,
LT_FINAL4 TYPE STANDARD TABLE OF TY_FINAL1,
LT_FINAL4_TEMP TYPE STANDARD TABLE OF TY_FINAL1,
LT_FINAL5 TYPE STANDARD TABLE OF TY_FINAL1,
LT_FINAL5_TEMP TYPE STANDARD TABLE OF TY_FINAL1,
LW_FINAL1 TYPE TY_FINAL,
LW_FINAL2 TYPE TY_FINAL,
LW_FINAL3 TYPE TY_FINAL1,
LW_FINAL4 TYPE TY_FINAL1,
LW_FINAL5 TYPE TY_FINAL1,
LW_ASOTP TYPE V_USR_NAME,
LW_ASHTP TYPE V_USR_NAME,
LT_VBRK TYPE STANDARD TABLE OF VBRK,
LW_VBRK TYPE VBRK,
LT_VBRP TYPE STANDARD TABLE OF VBRP,
LW_VBRP TYPE VBRP,
LT_VBPA TYPE STANDARD TABLE OF VBPA,
LW_VBPA TYPE VBPA,
LT_VBAP TYPE STANDARD TABLE OF VBAP,
LW_VBAP TYPE VBAP,
LT_LIKP TYPE STANDARD TABLE OF LIKP,
LW_LIKP TYPE LIKP,
LT_SHTP TYPE STANDARD TABLE OF ZSD_MAIL_BULK,
LW_SHTP TYPE ZSD_MAIL_BULK,
LT_SOTP TYPE STANDARD TABLE OF ZSMS_BULK,
LW_SOTP TYPE ZSMS_BULK,
LT_KNA1 TYPE STANDARD TABLE OF KNA1,
LW_KNA1 TYPE KNA1,
LT_KNA11 TYPE STANDARD TABLE OF KNA1,
LW_KNA11 TYPE KNA1,
LT_T001W TYPE STANDARD TABLE OF T001W,
LW_T001W TYPE T001W.

TYPES: BEGIN OF TY_CUGRP,
CGRPT TYPE CHAR50,
TOTQT TYPE FKIMG,
END OF TY_CUGRP.

DATA: LT_CUGRP TYPE STANDARD TABLE OF TY_CUGRP,
LT_CUGRP_TEMP TYPE STANDARD TABLE OF TY_CUGRP,
LW_CUGRP TYPE TY_CUGRP,
LW_CUGRP_TEMP TYPE TY_CUGRP.

TYPES: BEGIN OF TY_LOAPT,
PLNTT TYPE CHAR30,
TOTQT TYPE FKIMG,
END OF TY_LOAPT.

DATA: LT_LOAPT TYPE STANDARD TABLE OF TY_LOAPT,
LT_LOAPT_TEMP TYPE STANDARD TABLE OF TY_LOAPT,
LW_LOAPT TYPE TY_LOAPT,
LW_LOAPT_TEMP TYPE TY_LOAPT.

DATA: L_S_ADDRESS TYPE BAPIADDR3,
IT_RETURN TYPE STANDARD TABLE OF BAPIRET2,
V_NAME_PERSON TYPE CHAR100.

TYPES: BEGIN OF TY_EXCEL1,
CGRPT TYPE CHAR50,
SOTPN TYPE KUNNR,
SOTPT TYPE CHAR100,
ASOTP TYPE CHAR100,
SHTPN TYPE KUNNR,
SHTPT TYPE CHAR100,
ASHTP TYPE CHAR100,
LOAPT TYPE WERKS,
HSDQT TYPE CHAR15,
HFHSD TYPE CHAR15,
OTHQT TYPE CHAR15,
TOTQT TYPE CHAR15,
ODQTY1 TYPE CHAR15,
ODQTY2 TYPE CHAR15,
ODQTY3 TYPE CHAR15,
ODQTY TYPE CHAR15,
LV_FLAG1 TYPE CHAR1,
CGRPT1 TYPE CHAR50,
TOTQT1 TYPE CHAR15,
LV_FLAG2 TYPE CHAR1,
PLNTT TYPE CHAR30,
TOTQT2 TYPE CHAR15,
END OF TY_EXCEL1.

TYPES: BEGIN OF TY_EXCEL2,
CGRPT TYPE CHAR50,
SOTPN TYPE KUNNR,
SOTPT TYPE CHAR100,
ASOTP TYPE CHAR100,
SHTPN TYPE KUNNR,
SHTPT TYPE CHAR100,
ASHTP TYPE CHAR100,
LOAPT TYPE WERKS,
TOTQT TYPE CHAR15,
ODQTY TYPE CHAR15,
LV_FLAG1 TYPE CHAR1,
CGRPT1 TYPE CHAR50,
TOTQT1 TYPE CHAR15,
LV_FLAG2 TYPE CHAR1,
PLNTT TYPE CHAR30,
TOTQT2 TYPE CHAR15,
END OF TY_EXCEL2.

DATA: LT_EXCEL1 TYPE STANDARD TABLE OF TY_EXCEL1,
LT_EXCEL2 TYPE STANDARD TABLE OF TY_EXCEL1,
LT_EXCEL3 TYPE STANDARD TABLE OF TY_EXCEL2,
LT_EXCEL4 TYPE STANDARD TABLE OF TY_EXCEL2,
LT_EXCEL5 TYPE STANDARD TABLE OF TY_EXCEL2,
LW_EXCEL1 TYPE TY_EXCEL1,
LW_EXCEL2 TYPE TY_EXCEL1,
LW_EXCEL3 TYPE TY_EXCEL2,
LW_EXCEL4 TYPE TY_EXCEL2,
LW_EXCEL5 TYPE TY_EXCEL2.

FIELD-SYMBOLS: <FS_FINAL1> TYPE TY_FINAL,
<FS_EXCEL1> TYPE TY_EXCEL1,
<FS_CUGRP1> TYPE TY_CUGRP,
<FS_LOAPT1> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_CUGRP_TEMP1> TYPE TY_CUGRP.
FIELD-SYMBOLS: <FS_LOAPT_TEMP1> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_TOQTY1> TYPE TY_EXCEL1.

FIELD-SYMBOLS: <FS_FINAL2> TYPE TY_FINAL,
<FS_EXCEL2> TYPE TY_EXCEL1,
<FS_CUGRP2> TYPE TY_CUGRP,
<FS_LOAPT2> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_CUGRP_TEMP2> TYPE TY_CUGRP.
FIELD-SYMBOLS: <FS_LOAPT_TEMP2> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_TOQTY2> TYPE TY_EXCEL1.

FIELD-SYMBOLS: <FS_FINAL3> TYPE TY_FINAL1,
<FS_EXCEL3> TYPE TY_EXCEL2,
<FS_CUGRP3> TYPE TY_CUGRP,
<FS_LOAPT3> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_CUGRP_TEMP3> TYPE TY_CUGRP.
FIELD-SYMBOLS: <FS_LOAPT_TEMP3> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_TOQTY3> TYPE TY_EXCEL2.

FIELD-SYMBOLS: <FS_FINAL4> TYPE TY_FINAL1,
<FS_EXCEL4> TYPE TY_EXCEL2,
<FS_CUGRP4> TYPE TY_CUGRP,
<FS_LOAPT4> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_CUGRP_TEMP4> TYPE TY_CUGRP.
FIELD-SYMBOLS: <FS_LOAPT_TEMP4> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_TOQTY4> TYPE TY_EXCEL2.

FIELD-SYMBOLS: <FS_FINAL5> TYPE TY_FINAL1,
<FS_EXCEL5> TYPE TY_EXCEL2,
<FS_CUGRP5> TYPE TY_CUGRP,
<FS_LOAPT5> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_CUGRP_TEMP5> TYPE TY_CUGRP.
FIELD-SYMBOLS: <FS_LOAPT_TEMP5> TYPE TY_LOAPT.
FIELD-SYMBOLS: <FS_TOQTY5> TYPE TY_EXCEL2.

DATA : LV_HSDQT TYPE FKIMG,
LV_HFHSD TYPE FKIMG,
LV_OTHQT TYPE FKIMG,
LV_TMQTY TYPE FKIMG,
LV_ODQTY1 TYPE FKIMG,
LV_ODQTY2 TYPE FKIMG,
LV_ODQTY3 TYPE FKIMG,
LV_ODQTY TYPE FKIMG.

TYPES: BEGIN OF TY_VBFA,
VBELV LIKE VBFA-VBELV,
VBELN LIKE VBFA-VBELN,
POSNN LIKE VBFA-POSNN,
VBTYP_N TYPE VBTYP_N,
ERDAT TYPE ERDAT,
ERZET TYPE ERZET,
POSNN1 TYPE MBLPO,
END OF TY_VBFA.

TYPES: BEGIN OF TY_MSEGO2, " For Div 50 qty in KL (HSD & HFHSD)
MBLNR LIKE MSEGO2-MBLNR,
MJAHR LIKE MSEGO2-MJAHR,
ZEILE TYPE MSEGO2-ZEILE,
MSEHI LIKE MSEGO2-MSEHI,
ADQNTP LIKE MSEGO2-ADQNTP,
END OF TY_MSEGO2.

DATA: LT_VBFA TYPE TABLE OF TY_VBFA,
LW_VBFA TYPE TY_VBFA,
LT_MSEGO2 TYPE TABLE OF TY_MSEGO2,
LW_MSEGO2 TYPE TY_MSEGO2.

DATA : LV_TOTAL TYPE FKIMG.
DATA : LV_COUNT TYPE I.
DATA : LV_TABIX TYPE SY-TABIX.
DATA : S_DATE TYPE SY-DATUM, " Start date
E_DATE TYPE SY-DATUM. " End date
DATA : P_DATE1 TYPE CHAR11. " Yesterday Qty Date
DATA : P_DATE2 TYPE CHAR11. " Monthly Qty Date

************* Mail Sending Declarations ******************************************

DATA: I_XML TYPE STANDARD TABLE OF SOLISTI1,
I_BINARY_CONTENT TYPE SOLIX_TAB,
WA_XML TYPE SOLISTI1,
GC_CRLF TYPE C VALUE CL_BCS_CONVERT=>GC_CRLF,
V_STRING TYPE STRING,
V_SIZE TYPE SO_OBJ_LEN,
SEND_REQUEST TYPE REF TO CL_BCS,
DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
RECIPIENT TYPE REF TO IF_RECIPIENT_BCS,
BCS_EXCEPTION TYPE REF TO CX_BCS,
MAIN_TEXT TYPE BCSY_TEXT,
SENT_TO_ALL TYPE OS_BOOLEAN,
MAILTO TYPE AD_SMTPADR,
LO_SENDER TYPE REF TO IF_SENDER_BCS,
LV_MAIL TYPE AD_SMTPADR.

DATA: LT_MAIL TYPE STANDARD TABLE OF ZSD_BULK_TO_CC,
LW_MAIL TYPE ZSD_BULK_TO_CC.

************* XML Declarations ******************************************

CONSTANTS: C_HEADER1 TYPE CHAR63 VALUE '<?xml version="1.0" encoding="utf-8"?>',
C_HEADER01 TYPE CHAR63 VALUE '<?mso-application progid="Excel.Sheet"?>',
C_HEADER2 TYPE CHAR63 VALUE '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"',
C_HEADER3 TYPE CHAR63 VALUE 'xmlns:x="urn:schemas-microsoft-com:office:excel"',
C_HEADER4 TYPE CHAR63 VALUE 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"',
C_HEADER5 TYPE CHAR63 VALUE 'xmlns:html="http://www.w3.org/TR/REC-html40">',
C_STYLESBE TYPE CHAR63 VALUE '<Styles>',
C_STYLESED TYPE CHAR63 VALUE '</Styles>',
C_STYLEBE TYPE CHAR63 VALUE '<Style ss:ID="s1">',
C_STYLEED TYPE CHAR63 VALUE '</Style>',
C_FONT TYPE CHAR63 VALUE '<Font x:Family="Swiss" ss:Bold="1"/>',
C_WRKSHTBE TYPE CHAR20 VALUE '<Worksheet ss:Name="',
C_WRKSHTTG TYPE CHAR05 VALUE '">',
C_WRKSHTED TYPE CHAR20 VALUE '</Worksheet>',
C_TABLEBE TYPE CHAR10 VALUE '<Table>',
C_TABLEED TYPE CHAR10 VALUE '</Table>',
C_ROWBE TYPE CHAR10 VALUE '<Row>',
C_ROWED TYPE CHAR10 VALUE '</Row>',
C_CELLBE TYPE CHAR10 VALUE '<Cell>',
C_CELLED TYPE CHAR10 VALUE '</Cell>',
C_DATABE TYPE CHAR30 VALUE '<ss:Data ss:Type="String">',
C_DATAED TYPE CHAR30 VALUE '</ss:Data>',
C_WRKBKED TYPE CHAR30 VALUE '</Workbook>',
C_STYLEID TYPE CHAR30 VALUE '<Style ss:ID="s65">',
C_HDCOL TYPE CHAR50 VALUE '<Interior ss:Color="#92D050" ss:Pattern="Solid"/>', "#BFBFBF D699FF #C0C0C0 #FFFF00
C_HDALG TYPE CHAR50 VALUE '<Alignment ss:Vertical="Center" ss:WrapText="1"/>',
C_HDR TYPE CHAR50 VALUE '<Cell ss:StyleID="s65"><Data ss:Type="String">',
C_DTCELL TYPE CHAR30 VALUE '</Data></Cell>'.

*&---------------------------------------------------------------------*
*& Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS: P_DATE TYPE SY-DATUM OBLIGATORY.

*&---------------------------------------------------------------------*
*& Data Selection
*&---------------------------------------------------------------------*
P_DATE = P_DATE - 1.

CALL FUNCTION 'OIL_MONTH_GET_FIRST_LAST'
EXPORTING
I_DATE = P_DATE
IMPORTING
E_FIRST_DAY = S_DATE
E_LAST_DAY = E_DATE.

CALL FUNCTION 'CONVERSION_EXIT_SDATE_OUTPUT'
EXPORTING
INPUT = P_DATE
IMPORTING
OUTPUT = P_DATE1.

CONCATENATE P_DATE1+3(3) P_DATE1+7(4) INTO P_DATE2 SEPARATED BY SPACE. " month/year
CONCATENATE P_DATE+6(2) P_DATE1+3(3) P_DATE1+7(4) INTO P_DATE1 SEPARATED BY SPACE. " date/month/year

SELECT * FROM VBRK
INTO TABLE LT_VBRK
WHERE FKART NE 'ZSTO'
AND VTWEG IN ('10','40','50','52','54','56','80') " Distribution Channels
AND ( FKDAT GE S_DATE AND FKDAT LE P_DATE )
AND KDGRP IN ('41','42','43','44','45','46','47','48') " Customer Group
AND SPART IN ('50','70','80','85','97') " Product / Division
AND FKSTO NE 'X'.

IF NOT LT_VBRK IS INITIAL.

DELETE LT_VBRK WHERE FKART = 'ZIFT'.
DELETE LT_VBRK WHERE SFAKN NE ''.
DELETE LT_VBRK WHERE FKSTO = 'X'.

SELECT * FROM ZSMS_BULK
INTO TABLE LT_SOTP
FOR ALL ENTRIES IN LT_VBRK
WHERE KUNNR = LT_VBRK-KUNAG
AND SPART = LT_VBRK-SPART.

SELECT * FROM KNA1
INTO TABLE LT_KNA1
FOR ALL ENTRIES IN LT_VBRK
WHERE KUNNR = LT_VBRK-KUNAG.

SELECT * FROM VBRP
INTO TABLE LT_VBRP
FOR ALL ENTRIES IN LT_VBRK
WHERE VBELN = LT_VBRK-VBELN.

SELECT VBELV VBELN POSNN VBTYP_N ERDAT ERZET FROM VBFA
INTO TABLE LT_VBFA
FOR ALL ENTRIES IN LT_VBRP
WHERE VBELV = LT_VBRP-VGBEL
AND VBTYP_N = 'R'.

IF LT_VBFA IS NOT INITIAL.

SORT LT_VBFA BY VBELV ERDAT ERZET DESCENDING.
DELETE ADJACENT DUPLICATES FROM LT_VBFA COMPARING VBELV.

LOOP AT LT_VBFA INTO LW_VBFA.
LW_VBFA-POSNN1 = LW_VBFA-POSNN.
MODIFY LT_VBFA FROM LW_VBFA.
ENDLOOP.

SELECT MBLNR MJAHR ZEILE MSEHI ADQNTP FROM MSEGO2
INTO TABLE LT_MSEGO2
FOR ALL ENTRIES IN LT_VBFA
WHERE MBLNR = LT_VBFA-VBELN
AND ZEILE = LT_VBFA-POSNN1
AND MSEHI = 'KL'.
ENDIF.

SELECT * FROM T001W
INTO TABLE LT_T001W
FOR ALL ENTRIES IN LT_VBRP
WHERE WERKS = LT_VBRP-WERKS.

SELECT * FROM LIKP
INTO TABLE LT_LIKP
FOR ALL ENTRIES IN LT_VBRP
WHERE VBELN = LT_VBRP-VGBEL.

SELECT * FROM ZSD_MAIL_BULK
INTO TABLE LT_SHTP
FOR ALL ENTRIES IN LT_LIKP
WHERE SH_TO_PAR = LT_LIKP-KUNNR.

SELECT * FROM KNA1
INTO TABLE LT_KNA11
FOR ALL ENTRIES IN LT_LIKP
WHERE KUNNR = LT_LIKP-KUNNR.

ENDIF.

SELECT * FROM ZSD_BULK_TO_CC
INTO TABLE LT_MAIL
WHERE TO_MAIL_ID <> ''
OR CC_MAIL_ID <> ''.

SORT LT_VBRK BY SPART FKDAT.

CLEAR: LW_VBRK.

LOOP AT LT_VBRK INTO LW_VBRK.

IF LW_VBRK-SPART = '50'.

IF ( LW_VBRK-KDGRP = '41' OR LW_VBRK-KDGRP = '44' OR LW_VBRK-KDGRP = '45'
OR LW_VBRK-KDGRP = '46' OR LW_VBRK-KDGRP = '47' OR LW_VBRK-KDGRP = '48' ).
LW_FINAL1-CGRPT = 'Direct'.
ELSEIF LW_VBRK-KDGRP = '42'.
LW_FINAL1-CGRPT = 'Trader'.
ELSEIF LW_VBRK-KDGRP = '43'.
LW_FINAL1-CGRPT = 'Gr CO'.
ENDIF.

LW_FINAL1-SOTPN = LW_VBRK-KUNAG.

SORT LT_KNA1 BY KUNNR.
CLEAR : LW_KNA1.
READ TABLE LT_KNA1 INTO LW_KNA1 WITH KEY KUNNR = LW_VBRK-KUNAG
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL1-SOTPT = LW_KNA1-NAME1.
ENDIF.

SORT LT_SOTP BY KUNNR SPART.
CLEAR : LW_SOTP.
READ TABLE LT_SOTP INTO LW_SOTP WITH KEY KUNNR = LW_VBRK-KUNAG
SPART = LW_VBRK-SPART
BINARY SEARCH.
IF SY-SUBRC = 0.
CLEAR : LW_ASOTP.
SELECT SINGLE * FROM V_USR_NAME INTO LW_ASOTP
WHERE BNAME = LW_SOTP-AM_SOTP.
IF SY-SUBRC = 0.
LW_FINAL1-ASOTP = LW_ASOTP-NAME_TEXT.
ENDIF.
ENDIF.

SORT LT_VBRP BY VBELN.
CLEAR : LW_VBRP.
READ TABLE LT_VBRP INTO LW_VBRP WITH KEY VBELN = LW_VBRK-VBELN
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL1-LOAPT = LW_VBRP-WERKS.

SORT LT_VBFA BY VBELV VBTYP_N.
CLEAR : LW_VBFA.
READ TABLE LT_VBFA INTO LW_VBFA WITH KEY VBELV = LW_VBRP-VGBEL
VBTYP_N = 'R'
BINARY SEARCH.
IF SY-SUBRC = 0.
SORT LT_MSEGO2 BY MBLNR ZEILE MSEHI.
CLEAR LW_MSEGO2.
READ TABLE LT_MSEGO2 INTO LW_MSEGO2 WITH KEY MBLNR = LW_VBFA-VBELN
ZEILE = LW_VBFA-POSNN1
MSEHI = 'KL'
BINARY SEARCH.
IF SY-SUBRC = 0.
IF ( LW_VBRP-MATNR = '5000' OR LW_VBRP-MATNR = '5001' OR LW_VBRP-MATNR = '5002' ).
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL1-HSDQT = LW_MSEGO2-ADQNTP * -1.
ELSEIF ( LW_VBRK-FKART = 'ZDF2' OR LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR
LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZG2' OR LW_VBRK-FKART = 'ZL2' OR
LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL1-HSDQT = ''.
ELSE.
LW_FINAL1-HSDQT = LW_MSEGO2-ADQNTP.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL1-ODQTY1 = LW_MSEGO2-ADQNTP * -1.
ELSEIF ( LW_VBRK-FKART = 'ZDF2' OR LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR
LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZG2' OR LW_VBRK-FKART = 'ZL2' OR
LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL1-ODQTY1 = ''.
ELSE.
LW_FINAL1-ODQTY1 = LW_MSEGO2-ADQNTP.
ENDIF.
ENDIF.
ELSEIF LW_VBRP-MATNR = '5003'.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL1-HFHSD = LW_MSEGO2-ADQNTP * -1.
ELSEIF ( LW_VBRK-FKART = 'ZDF2' OR LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR
LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZG2' OR LW_VBRK-FKART = 'ZL2' OR
LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL1-HFHSD = ''.
ELSE.
LW_FINAL1-HFHSD = LW_MSEGO2-ADQNTP.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL1-ODQTY2 = LW_MSEGO2-ADQNTP * -1.
ELSEIF ( LW_VBRK-FKART = 'ZDF2' OR LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR
LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZG2' OR LW_VBRK-FKART = 'ZL2' OR
LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL1-ODQTY2 = ''.
ELSE.
LW_FINAL1-ODQTY2 = LW_MSEGO2-ADQNTP.
ENDIF.
ENDIF.
ELSEIF ( LW_VBRP-MATNR <> '5000' OR LW_VBRP-MATNR <> '5001' OR LW_VBRP-MATNR <> '5002' OR LW_VBRP-MATNR <> '5003' ).
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL1-OTHQT = LW_MSEGO2-ADQNTP * -1.
ELSEIF ( LW_VBRK-FKART = 'ZDF2' OR LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR
LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZG2' OR LW_VBRK-FKART = 'ZL2' OR
LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL1-OTHQT = ''.
ELSE.
LW_FINAL1-OTHQT = LW_MSEGO2-ADQNTP.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL1-ODQTY3 = LW_MSEGO2-ADQNTP * -1.
ELSEIF ( LW_VBRK-FKART = 'ZDF2' OR LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR
LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZG2' OR LW_VBRK-FKART = 'ZL2' OR
LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL1-ODQTY3 = ''.
ELSE.
LW_FINAL1-ODQTY3 = LW_MSEGO2-ADQNTP.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
LW_FINAL1-TOTQT = LW_FINAL1-HSDQT + LW_FINAL1-HFHSD + LW_FINAL1-OTHQT.
LW_FINAL1-ODQTY = LW_FINAL1-ODQTY1 + LW_FINAL1-ODQTY2 + LW_FINAL1-ODQTY3.
ENDIF.

SORT LT_T001W BY WERKS.
CLEAR LW_T001W.
READ TABLE LT_T001W INTO LW_T001W WITH KEY WERKS = LW_VBRP-WERKS
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL1-PLNTT = LW_T001W-NAME1.
ENDIF.

SORT LT_LIKP BY VBELN.
CLEAR : LW_LIKP.
READ TABLE LT_LIKP INTO LW_LIKP WITH KEY VBELN = LW_VBRP-VGBEL
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL1-SHTPN = LW_LIKP-KUNNR.
ENDIF.

SORT LT_SHTP BY SH_TO_PAR.
CLEAR : LW_SHTP.
READ TABLE LT_SHTP INTO LW_SHTP WITH KEY SH_TO_PAR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
USERNAME = LW_SHTP-AM_SHTP
IMPORTING
ADDRESS = L_S_ADDRESS
TABLES
RETURN = IT_RETURN.

CLEAR : V_NAME_PERSON.
CONCATENATE L_S_ADDRESS-FIRSTNAME
L_S_ADDRESS-LASTNAME
INTO V_NAME_PERSON
SEPARATED BY SPACE.

LW_FINAL1-ASHTP = V_NAME_PERSON.
ENDIF.

SORT LT_KNA11 BY KUNNR.
CLEAR : LW_KNA11.
READ TABLE LT_KNA11 INTO LW_KNA11 WITH KEY KUNNR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL1-SHTPT = LW_KNA11-NAME1.
ENDIF.

APPEND LW_FINAL1 TO LT_FINAL1.
CLEAR: LW_FINAL1.

ELSEIF LW_VBRK-SPART = '70'.

IF ( LW_VBRK-KDGRP = '41' OR LW_VBRK-KDGRP = '44' OR LW_VBRK-KDGRP = '45'
OR LW_VBRK-KDGRP = '46' OR LW_VBRK-KDGRP = '47' OR LW_VBRK-KDGRP = '48' ).
LW_FINAL2-CGRPT = 'Direct'.
ELSEIF LW_VBRK-KDGRP = '42'.
LW_FINAL2-CGRPT = 'Trader'.
ELSEIF LW_VBRK-KDGRP = '43'.
LW_FINAL2-CGRPT = 'Gr CO'.
ENDIF.

LW_FINAL2-SOTPN = LW_VBRK-KUNAG.

SORT LT_KNA1 BY KUNNR.
CLEAR : LW_KNA1.
READ TABLE LT_KNA1 INTO LW_KNA1 WITH KEY KUNNR = LW_VBRK-KUNAG
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL2-SOTPT = LW_KNA1-NAME1.
ENDIF.

SORT LT_SOTP BY KUNNR SPART.
CLEAR : LW_SOTP.
READ TABLE LT_SOTP INTO LW_SOTP WITH KEY KUNNR = LW_VBRK-KUNAG
SPART = LW_VBRK-SPART
BINARY SEARCH.
IF SY-SUBRC = 0.
CLEAR : LW_ASOTP.
SELECT SINGLE * FROM V_USR_NAME INTO LW_ASOTP
WHERE BNAME = LW_SOTP-AM_SOTP.
IF SY-SUBRC = 0.
LW_FINAL2-ASOTP = LW_ASOTP-NAME_TEXT.
ENDIF.
ENDIF.

SORT LT_VBRP BY VBELN.
CLEAR : LW_VBRP.
READ TABLE LT_VBRP INTO LW_VBRP WITH KEY VBELN = LW_VBRK-VBELN
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL2-LOAPT = LW_VBRP-WERKS.

IF LW_VBRP-MATNR = '6630' .
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL2-HSDQT = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL2-HSDQT = ''.
ELSE.
LW_FINAL2-HSDQT = LW_VBRP-NTGEW / 1000.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL2-ODQTY1 = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL2-ODQTY1 = ''.
ELSE.
LW_FINAL2-ODQTY1 = LW_VBRP-NTGEW / 1000.
ENDIF.
ENDIF.
ELSEIF LW_VBRP-MATNR = '6631' .
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL2-HFHSD = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL2-HFHSD = ''.
ELSE.
LW_FINAL2-HFHSD = LW_VBRP-NTGEW / 1000.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL2-ODQTY2 = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL2-ODQTY2 = ''.
ELSE.
LW_FINAL2-ODQTY2 = LW_VBRP-NTGEW / 1000.
ENDIF.
ENDIF.
ELSEIF ( LW_VBRP-MATNR <> '6630' OR LW_VBRP-MATNR <> '6631' ).
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL2-OTHQT = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL2-OTHQT = ''.
ELSE.
LW_FINAL2-OTHQT = LW_VBRP-NTGEW / 1000.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL2-ODQTY3 = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL2-ODQTY3 = ''.
ELSE.
LW_FINAL2-ODQTY3 = LW_VBRP-NTGEW / 1000.
ENDIF.
ENDIF.
ENDIF.
LW_FINAL2-TOTQT = LW_FINAL2-HSDQT + LW_FINAL2-HFHSD + LW_FINAL2-OTHQT.
LW_FINAL2-ODQTY = LW_FINAL2-ODQTY1 + LW_FINAL2-ODQTY2 + LW_FINAL2-ODQTY3.
ENDIF.

SORT LT_T001W BY WERKS.
CLEAR LW_T001W.
READ TABLE LT_T001W INTO LW_T001W WITH KEY WERKS = LW_VBRP-WERKS
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL2-PLNTT = LW_T001W-NAME1.
ENDIF.

SORT LT_LIKP BY VBELN.
CLEAR : LW_LIKP.
READ TABLE LT_LIKP INTO LW_LIKP WITH KEY VBELN = LW_VBRP-VGBEL
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL2-SHTPN = LW_LIKP-KUNNR.
ENDIF.

SORT LT_SHTP BY SH_TO_PAR.
CLEAR : LW_SHTP.
READ TABLE LT_SHTP INTO LW_SHTP WITH KEY SH_TO_PAR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
USERNAME = LW_SHTP-AM_SHTP
IMPORTING
ADDRESS = L_S_ADDRESS
TABLES
RETURN = IT_RETURN.

CLEAR : V_NAME_PERSON.
CONCATENATE L_S_ADDRESS-FIRSTNAME
L_S_ADDRESS-LASTNAME
INTO V_NAME_PERSON
SEPARATED BY SPACE.

LW_FINAL2-ASHTP = V_NAME_PERSON.
ENDIF.

SORT LT_KNA11 BY KUNNR.
CLEAR : LW_KNA11.
READ TABLE LT_KNA11 INTO LW_KNA11 WITH KEY KUNNR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL2-SHTPT = LW_KNA11-NAME1.
ENDIF.

APPEND LW_FINAL2 TO LT_FINAL2.
CLEAR: LW_FINAL2.

ELSEIF LW_VBRK-SPART = '80'.

IF ( LW_VBRK-KDGRP = '41' OR LW_VBRK-KDGRP = '44' OR LW_VBRK-KDGRP = '45'
OR LW_VBRK-KDGRP = '46' OR LW_VBRK-KDGRP = '47' OR LW_VBRK-KDGRP = '48' ).
LW_FINAL3-CGRPT = 'Direct'.
ELSEIF LW_VBRK-KDGRP = '42'.
LW_FINAL3-CGRPT = 'Trader'.
ELSEIF LW_VBRK-KDGRP = '43'.
LW_FINAL3-CGRPT = 'Gr CO'.
ENDIF.

LW_FINAL3-SOTPN = LW_VBRK-KUNAG.

SORT LT_KNA1 BY KUNNR.
CLEAR : LW_KNA1.
READ TABLE LT_KNA1 INTO LW_KNA1 WITH KEY KUNNR = LW_VBRK-KUNAG
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL3-SOTPT = LW_KNA1-NAME1.
ENDIF.

SORT LT_SOTP BY KUNNR SPART.
CLEAR : LW_SOTP.
READ TABLE LT_SOTP INTO LW_SOTP WITH KEY KUNNR = LW_VBRK-KUNAG
SPART = LW_VBRK-SPART
BINARY SEARCH.
IF SY-SUBRC = 0.
CLEAR : LW_ASOTP.
SELECT SINGLE * FROM V_USR_NAME INTO LW_ASOTP
WHERE BNAME = LW_SOTP-AM_SOTP.
IF SY-SUBRC = 0.
LW_FINAL3-ASOTP = LW_ASOTP-NAME_TEXT.
ENDIF.
ENDIF.

SORT LT_VBRP BY VBELN.
CLEAR : LW_VBRP.
READ TABLE LT_VBRP INTO LW_VBRP WITH KEY VBELN = LW_VBRK-VBELN
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL3-LOAPT = LW_VBRP-WERKS.

IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL3-TOTQT = LW_VBRP-NTGEW * -1.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL3-TOTQT = ''.
ELSE.
LW_FINAL3-TOTQT = LW_VBRP-NTGEW.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL3-ODQTY = LW_VBRP-NTGEW * -1.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL3-ODQTY = ''.
ELSE.
LW_FINAL3-ODQTY = LW_VBRP-NTGEW.
ENDIF.
ENDIF.
ENDIF.

SORT LT_T001W BY WERKS.
CLEAR LW_T001W.
READ TABLE LT_T001W INTO LW_T001W WITH KEY WERKS = LW_VBRP-WERKS
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL3-PLNTT = LW_T001W-NAME1.
ENDIF.

SORT LT_LIKP BY VBELN.
CLEAR : LW_LIKP.
READ TABLE LT_LIKP INTO LW_LIKP WITH KEY VBELN = LW_VBRP-VGBEL
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL3-SHTPN = LW_LIKP-KUNNR.
ENDIF.

SORT LT_SHTP BY SH_TO_PAR.
CLEAR : LW_SHTP.
READ TABLE LT_SHTP INTO LW_SHTP WITH KEY SH_TO_PAR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
USERNAME = LW_SHTP-AM_SHTP
IMPORTING
ADDRESS = L_S_ADDRESS
TABLES
RETURN = IT_RETURN.

CLEAR : V_NAME_PERSON.
CONCATENATE L_S_ADDRESS-FIRSTNAME
L_S_ADDRESS-LASTNAME
INTO V_NAME_PERSON
SEPARATED BY SPACE.

LW_FINAL3-ASHTP = V_NAME_PERSON.
ENDIF.

SORT LT_KNA11 BY KUNNR.
CLEAR : LW_KNA11.
READ TABLE LT_KNA11 INTO LW_KNA11 WITH KEY KUNNR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL3-SHTPT = LW_KNA11-NAME1.
ENDIF.

APPEND LW_FINAL3 TO LT_FINAL3.
CLEAR: LW_FINAL3.

ELSEIF LW_VBRK-SPART = '85'.

IF ( LW_VBRK-KDGRP = '41' OR LW_VBRK-KDGRP = '44' OR LW_VBRK-KDGRP = '45'
OR LW_VBRK-KDGRP = '46' OR LW_VBRK-KDGRP = '47' OR LW_VBRK-KDGRP = '48' ).
LW_FINAL4-CGRPT = 'Direct'.
ELSEIF LW_VBRK-KDGRP = '42'.
LW_FINAL4-CGRPT = 'Trader'.
ELSEIF LW_VBRK-KDGRP = '43'.
LW_FINAL4-CGRPT = 'Gr CO'.
ENDIF.

LW_FINAL4-SOTPN = LW_VBRK-KUNAG.

SORT LT_KNA1 BY KUNNR.
CLEAR : LW_KNA1.
READ TABLE LT_KNA1 INTO LW_KNA1 WITH KEY KUNNR = LW_VBRK-KUNAG
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL4-SOTPT = LW_KNA1-NAME1.
ENDIF.

SORT LT_SOTP BY KUNNR SPART.
CLEAR : LW_SOTP.
READ TABLE LT_SOTP INTO LW_SOTP WITH KEY KUNNR = LW_VBRK-KUNAG
SPART = LW_VBRK-SPART
BINARY SEARCH.
IF SY-SUBRC = 0.
CLEAR : LW_ASOTP.
SELECT SINGLE * FROM V_USR_NAME INTO LW_ASOTP
WHERE BNAME = LW_SOTP-AM_SOTP.
IF SY-SUBRC = 0.
LW_FINAL4-ASOTP = LW_ASOTP-NAME_TEXT.
ENDIF.
ENDIF.

SORT LT_VBRP BY VBELN.
CLEAR : LW_VBRP.
READ TABLE LT_VBRP INTO LW_VBRP WITH KEY VBELN = LW_VBRK-VBELN
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL4-LOAPT = LW_VBRP-WERKS.

IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL4-TOTQT = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL4-TOTQT = ''.
ELSE.
LW_FINAL4-TOTQT = LW_VBRP-NTGEW / 1000.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL4-ODQTY = ( LW_VBRP-NTGEW * -1 ) / 1000.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL4-ODQTY = ''.
ELSE.
LW_FINAL4-ODQTY = LW_VBRP-NTGEW / 1000.
ENDIF.
ENDIF.
ENDIF.

SORT LT_T001W BY WERKS.
CLEAR LW_T001W.
READ TABLE LT_T001W INTO LW_T001W WITH KEY WERKS = LW_VBRP-WERKS
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL4-PLNTT = LW_T001W-NAME1.
ENDIF.

SORT LT_LIKP BY VBELN.
CLEAR : LW_LIKP.
READ TABLE LT_LIKP INTO LW_LIKP WITH KEY VBELN = LW_VBRP-VGBEL
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL4-SHTPN = LW_LIKP-KUNNR.
ENDIF.

SORT LT_SHTP BY SH_TO_PAR.
CLEAR : LW_SHTP.
READ TABLE LT_SHTP INTO LW_SHTP WITH KEY SH_TO_PAR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
USERNAME = LW_SHTP-AM_SHTP
IMPORTING
ADDRESS = L_S_ADDRESS
TABLES
RETURN = IT_RETURN.

CLEAR : V_NAME_PERSON.
CONCATENATE L_S_ADDRESS-FIRSTNAME
L_S_ADDRESS-LASTNAME
INTO V_NAME_PERSON
SEPARATED BY SPACE.

LW_FINAL4-ASHTP = V_NAME_PERSON.
ENDIF.

SORT LT_KNA11 BY KUNNR.
CLEAR : LW_KNA11.
READ TABLE LT_KNA11 INTO LW_KNA11 WITH KEY KUNNR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL4-SHTPT = LW_KNA11-NAME1.
ENDIF.

APPEND LW_FINAL4 TO LT_FINAL4.
CLEAR: LW_FINAL4.

ELSEIF LW_VBRK-SPART = '97'.

IF ( LW_VBRK-KDGRP = '41' OR LW_VBRK-KDGRP = '44' OR LW_VBRK-KDGRP = '45'
OR LW_VBRK-KDGRP = '46' OR LW_VBRK-KDGRP = '47' OR LW_VBRK-KDGRP = '48' ).
LW_FINAL5-CGRPT = 'Direct'.
ELSEIF LW_VBRK-KDGRP = '42'.
LW_FINAL5-CGRPT = 'Trader'.
ELSEIF LW_VBRK-KDGRP = '43'.
LW_FINAL5-CGRPT = 'Gr CO'.
ENDIF.

LW_FINAL5-SOTPN = LW_VBRK-KUNAG.

SORT LT_KNA1 BY KUNNR.
CLEAR : LW_KNA1.
READ TABLE LT_KNA1 INTO LW_KNA1 WITH KEY KUNNR = LW_VBRK-KUNAG
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL5-SOTPT = LW_KNA1-NAME1.
ENDIF.

SORT LT_SOTP BY KUNNR SPART.
CLEAR : LW_SOTP.
READ TABLE LT_SOTP INTO LW_SOTP WITH KEY KUNNR = LW_VBRK-KUNAG
SPART = LW_VBRK-SPART
BINARY SEARCH.
IF SY-SUBRC = 0.
CLEAR : LW_ASOTP.
SELECT SINGLE * FROM V_USR_NAME INTO LW_ASOTP
WHERE BNAME = LW_SOTP-AM_SOTP.
IF SY-SUBRC = 0.
LW_FINAL5-ASOTP = LW_ASOTP-NAME_TEXT.
ENDIF.
ENDIF.

SORT LT_VBRP BY VBELN.
CLEAR : LW_VBRP.
READ TABLE LT_VBRP INTO LW_VBRP WITH KEY VBELN = LW_VBRK-VBELN
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL5-LOAPT = LW_VBRP-WERKS.

IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL5-TOTQT = LW_VBRP-NTGEW * -1.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL5-TOTQT = ''.
ELSE.
LW_FINAL5-TOTQT = LW_VBRP-NTGEW.
ENDIF.
IF P_DATE = LW_VBRK-FKDAT.
IF LW_VBRK-FKART = 'ZRE'.
LW_FINAL5-ODQTY = LW_VBRP-NTGEW * -1.
ELSEIF ( LW_VBRK-FKART = 'ZS1' OR LW_VBRK-FKART = 'ZDDB' OR LW_VBRK-FKART = 'ZDF2' OR
LW_VBRK-FKART = 'ZDTR' OR LW_VBRK-FKART = 'ZDFE' OR LW_VBRK-FKART = 'ZG2' OR
LW_VBRK-FKART = 'ZL2' OR LW_VBRK-FKART = 'ZBG2' OR LW_VBRK-FKART = 'ZBL2' OR
LW_VBRK-FKART = 'ZDS1' OR LW_VBRK-FKART = 'ZCS2' ).
LW_FINAL5-ODQTY = ''.
ELSE.
LW_FINAL5-ODQTY = LW_VBRP-NTGEW.
ENDIF.
ENDIF.
ENDIF.

SORT LT_T001W BY WERKS.
CLEAR LW_T001W.
READ TABLE LT_T001W INTO LW_T001W WITH KEY WERKS = LW_VBRP-WERKS
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL5-PLNTT = LW_T001W-NAME1.
ENDIF.

SORT LT_LIKP BY VBELN.
CLEAR : LW_LIKP.
READ TABLE LT_LIKP INTO LW_LIKP WITH KEY VBELN = LW_VBRP-VGBEL
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL5-SHTPN = LW_LIKP-KUNNR.
ENDIF.

SORT LT_SHTP BY SH_TO_PAR.
CLEAR : LW_SHTP.
READ TABLE LT_SHTP INTO LW_SHTP WITH KEY SH_TO_PAR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
USERNAME = LW_SHTP-AM_SHTP
IMPORTING
ADDRESS = L_S_ADDRESS
TABLES
RETURN = IT_RETURN.

CLEAR : V_NAME_PERSON.
CONCATENATE L_S_ADDRESS-FIRSTNAME
L_S_ADDRESS-LASTNAME
INTO V_NAME_PERSON
SEPARATED BY SPACE.

LW_FINAL5-ASHTP = V_NAME_PERSON.
ENDIF.

SORT LT_KNA11 BY KUNNR.
CLEAR : LW_KNA11.
READ TABLE LT_KNA11 INTO LW_KNA11 WITH KEY KUNNR = LW_LIKP-KUNNR
BINARY SEARCH.
IF SY-SUBRC = 0.
LW_FINAL5-SHTPT = LW_KNA11-NAME1.
ENDIF.

APPEND LW_FINAL5 TO LT_FINAL5.
CLEAR: LW_FINAL5.
ENDIF.

ENDLOOP.

IF NOT LT_FINAL1 IS INITIAL.
DELETE LT_FINAL1 WHERE SHTPN IS INITIAL.
ENDIF.

IF NOT LT_FINAL2 IS INITIAL.
DELETE LT_FINAL2 WHERE SHTPN IS INITIAL.
ENDIF.

IF NOT LT_FINAL3 IS INITIAL.
DELETE LT_FINAL3 WHERE SHTPN IS INITIAL.
ENDIF.

IF NOT LT_FINAL4 IS INITIAL.
DELETE LT_FINAL4 WHERE SHTPN IS INITIAL.
ENDIF.

IF NOT LT_FINAL5 IS INITIAL.
DELETE LT_FINAL5 WHERE SHTPN IS INITIAL.
ENDIF.

****************************First Sheet Final data*************************

CLEAR: LW_FINAL1.
LOOP AT LT_FINAL1 INTO LW_FINAL1.
COLLECT LW_FINAL1 INTO LT_FINAL1_TEMP.
ENDLOOP.

CLEAR: LT_FINAL1.
IF LT_FINAL1 IS INITIAL.
LT_FINAL1 = LT_FINAL1_TEMP.
ENDIF.

SORT LT_FINAL1 BY CGRPT.

CLEAR: LW_FINAL1.
LOOP AT LT_FINAL1 INTO LW_FINAL1.
LW_CUGRP-CGRPT = LW_FINAL1-CGRPT.
LW_CUGRP-TOTQT = LW_FINAL1-TOTQT.
APPEND LW_CUGRP TO LT_CUGRP.
CLEAR LW_CUGRP.
ENDLOOP.

SORT LT_CUGRP BY CGRPT.

CLEAR: LW_CUGRP.
LOOP AT LT_CUGRP INTO LW_CUGRP.
AT END OF CGRPT.
LW_CUGRP_TEMP-CGRPT = LW_CUGRP-CGRPT.
SUM.
LW_CUGRP_TEMP-TOTQT = LW_CUGRP-TOTQT.
APPEND LW_CUGRP_TEMP TO LT_CUGRP_TEMP.
CLEAR LW_CUGRP_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_CUGRP_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP_TEMP1>.
LV_TOTAL = LV_TOTAL + <FS_CUGRP_TEMP1>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_CUGRP_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_CUGRP_TEMP-CGRPT = 'TOTAL'.
LW_CUGRP_TEMP-TOTQT = LV_TOTAL.

INSERT LW_CUGRP_TEMP INTO LT_CUGRP_TEMP INDEX LV_COUNT.

SORT LT_FINAL1 BY PLNTT.

CLEAR: LW_FINAL1.
LOOP AT LT_FINAL1 INTO LW_FINAL1.
LW_LOAPT-PLNTT = LW_FINAL1-PLNTT.
LW_LOAPT-TOTQT = LW_FINAL1-TOTQT.
APPEND LW_LOAPT TO LT_LOAPT.
CLEAR LW_LOAPT.
ENDLOOP.

SORT LT_LOAPT BY PLNTT.

CLEAR: LW_LOAPT.
LOOP AT LT_LOAPT INTO LW_LOAPT.
AT END OF PLNTT.
LW_LOAPT_TEMP-PLNTT = LW_LOAPT-PLNTT.
SUM.
LW_LOAPT_TEMP-TOTQT = LW_LOAPT-TOTQT.
APPEND LW_LOAPT_TEMP TO LT_LOAPT_TEMP.
CLEAR LW_LOAPT_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_LOAPT_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT_TEMP1>.
LV_TOTAL = LV_TOTAL + <FS_LOAPT_TEMP1>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_LOAPT_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_LOAPT_TEMP-PLNTT = 'TOTAL'.
LW_LOAPT_TEMP-TOTQT = LV_TOTAL.

INSERT LW_LOAPT_TEMP INTO LT_LOAPT_TEMP INDEX LV_COUNT.

CLEAR: LT_EXCEL1[], LW_EXCEL1. " First Sheet Final data

LOOP AT LT_FINAL1 ASSIGNING <FS_FINAL1>.

LW_EXCEL1-CGRPT = <FS_FINAL1>-CGRPT.
LW_EXCEL1-SOTPN = <FS_FINAL1>-SOTPN.
LW_EXCEL1-SOTPT = <FS_FINAL1>-SOTPT.
LW_EXCEL1-ASOTP = <FS_FINAL1>-ASOTP.
LW_EXCEL1-SHTPN = <FS_FINAL1>-SHTPN.
LW_EXCEL1-SHTPT = <FS_FINAL1>-SHTPT.
LW_EXCEL1-ASHTP = <FS_FINAL1>-ASHTP.
LW_EXCEL1-LOAPT = <FS_FINAL1>-LOAPT.
LW_EXCEL1-HSDQT = <FS_FINAL1>-HSDQT.
LW_EXCEL1-HFHSD = <FS_FINAL1>-HFHSD.
LW_EXCEL1-OTHQT = <FS_FINAL1>-OTHQT.
LW_EXCEL1-TOTQT = <FS_FINAL1>-TOTQT.
LW_EXCEL1-ODQTY1 = <FS_FINAL1>-ODQTY1.
LW_EXCEL1-ODQTY2 = <FS_FINAL1>-ODQTY2.
LW_EXCEL1-ODQTY3 = <FS_FINAL1>-ODQTY3.
LW_EXCEL1-ODQTY = <FS_FINAL1>-ODQTY.

APPEND LW_EXCEL1 TO LT_EXCEL1.
CLEAR LW_EXCEL1.
ENDLOOP.

CLEAR: LW_EXCEL1, LV_HSDQT, LV_HFHSD, LV_OTHQT, LV_TMQTY, LV_ODQTY1, LV_ODQTY2, LV_ODQTY3, LV_ODQTY, LV_COUNT.
LOOP AT LT_EXCEL1 ASSIGNING <FS_TOQTY1>.
LV_HSDQT = LV_HSDQT + <FS_TOQTY1>-HSDQT.
LV_HFHSD = LV_HFHSD + <FS_TOQTY1>-HFHSD.
LV_OTHQT = LV_OTHQT + <FS_TOQTY1>-OTHQT.
LV_TMQTY = LV_TMQTY + <FS_TOQTY1>-TOTQT.
LV_ODQTY1 = LV_ODQTY1 + <FS_TOQTY1>-ODQTY1.
LV_ODQTY2 = LV_ODQTY2 + <FS_TOQTY1>-ODQTY2.
LV_ODQTY3 = LV_ODQTY3 + <FS_TOQTY1>-ODQTY3.
LV_ODQTY = LV_ODQTY + <FS_TOQTY1>-ODQTY.
ENDLOOP.

LV_COUNT = LINES( LT_EXCEL1 ).

LV_COUNT = LV_COUNT + 1.

LW_EXCEL1-HSDQT = LV_HSDQT.
LW_EXCEL1-HFHSD = LV_HFHSD.
LW_EXCEL1-OTHQT = LV_OTHQT.
LW_EXCEL1-TOTQT = LV_TMQTY.
LW_EXCEL1-ODQTY1 = LV_ODQTY1.
LW_EXCEL1-ODQTY2 = LV_ODQTY2.
LW_EXCEL1-ODQTY3 = LV_ODQTY3.
LW_EXCEL1-ODQTY = LV_ODQTY.

INSERT LW_EXCEL1 INTO LT_EXCEL1 INDEX LV_COUNT.

LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP1>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL1 ASSIGNING <FS_EXCEL1> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL1>-CGRPT1 = <FS_CUGRP1>-CGRPT.
<FS_EXCEL1>-TOTQT1 = <FS_CUGRP1>-TOTQT.
ENDIF.
ENDLOOP.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT1>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL1 ASSIGNING <FS_EXCEL1> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL1>-PLNTT = <FS_LOAPT1>-PLNTT.
<FS_EXCEL1>-TOTQT2 = <FS_LOAPT1>-TOTQT.
ENDIF.
ENDLOOP.

**************************Second Sheet Final data********************************************

CLEAR: LW_FINAL2.
LOOP AT LT_FINAL2 INTO LW_FINAL2.
COLLECT LW_FINAL2 INTO LT_FINAL2_TEMP.
ENDLOOP.

CLEAR: LT_FINAL2.
IF LT_FINAL2 IS INITIAL.
LT_FINAL2 = LT_FINAL2_TEMP.
ENDIF.

SORT LT_FINAL2 BY CGRPT.

CLEAR: LW_FINAL2, LT_CUGRP, LW_CUGRP.
LOOP AT LT_FINAL2 INTO LW_FINAL2.
LW_CUGRP-CGRPT = LW_FINAL2-CGRPT.
LW_CUGRP-TOTQT = LW_FINAL2-TOTQT.
APPEND LW_CUGRP TO LT_CUGRP.
CLEAR LW_CUGRP.
ENDLOOP.

SORT LT_CUGRP BY CGRPT.

CLEAR: LW_CUGRP, LW_CUGRP_TEMP, LT_CUGRP_TEMP.
LOOP AT LT_CUGRP INTO LW_CUGRP.
AT END OF CGRPT.
LW_CUGRP_TEMP-CGRPT = LW_CUGRP-CGRPT.
SUM.
LW_CUGRP_TEMP-TOTQT = LW_CUGRP-TOTQT.
APPEND LW_CUGRP_TEMP TO LT_CUGRP_TEMP.
CLEAR LW_CUGRP_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_CUGRP_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP_TEMP2>.
LV_TOTAL = LV_TOTAL + <FS_CUGRP_TEMP2>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_CUGRP_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_CUGRP_TEMP-CGRPT = 'TOTAL'.
LW_CUGRP_TEMP-TOTQT = LV_TOTAL.

INSERT LW_CUGRP_TEMP INTO LT_CUGRP_TEMP INDEX LV_COUNT.

SORT LT_FINAL2 BY PLNTT.

CLEAR: LW_FINAL2, LW_LOAPT, LT_LOAPT.
LOOP AT LT_FINAL2 INTO LW_FINAL2.
LW_LOAPT-PLNTT = LW_FINAL2-PLNTT.
LW_LOAPT-TOTQT = LW_FINAL2-TOTQT.
APPEND LW_LOAPT TO LT_LOAPT.
CLEAR LW_LOAPT.
ENDLOOP.

SORT LT_LOAPT BY PLNTT.

CLEAR: LW_LOAPT, LW_LOAPT_TEMP, LT_LOAPT_TEMP.
LOOP AT LT_LOAPT INTO LW_LOAPT.
AT END OF PLNTT.
LW_LOAPT_TEMP-PLNTT = LW_LOAPT-PLNTT.
SUM.
LW_LOAPT_TEMP-TOTQT = LW_LOAPT-TOTQT.
APPEND LW_LOAPT_TEMP TO LT_LOAPT_TEMP.
CLEAR LW_LOAPT_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_LOAPT_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT_TEMP2>.
LV_TOTAL = LV_TOTAL + <FS_LOAPT_TEMP2>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_LOAPT_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_LOAPT_TEMP-PLNTT = 'TOTAL'.
LW_LOAPT_TEMP-TOTQT = LV_TOTAL.

INSERT LW_LOAPT_TEMP INTO LT_LOAPT_TEMP INDEX LV_COUNT.

CLEAR: LT_EXCEL2[], LW_EXCEL2. " Second Sheet Final data

LOOP AT LT_FINAL2 ASSIGNING <FS_FINAL2>.

LW_EXCEL2-CGRPT = <FS_FINAL2>-CGRPT.
LW_EXCEL2-SOTPN = <FS_FINAL2>-SOTPN.
LW_EXCEL2-SOTPT = <FS_FINAL2>-SOTPT.
LW_EXCEL2-ASOTP = <FS_FINAL2>-ASOTP.
LW_EXCEL2-SHTPN = <FS_FINAL2>-SHTPN.
LW_EXCEL2-SHTPT = <FS_FINAL2>-SHTPT.
LW_EXCEL2-ASHTP = <FS_FINAL2>-ASHTP.
LW_EXCEL2-LOAPT = <FS_FINAL2>-LOAPT.
LW_EXCEL2-HSDQT = <FS_FINAL2>-HSDQT.
LW_EXCEL2-HFHSD = <FS_FINAL2>-HFHSD.
LW_EXCEL2-OTHQT = <FS_FINAL2>-OTHQT.
LW_EXCEL2-TOTQT = <FS_FINAL2>-TOTQT.
LW_EXCEL2-ODQTY1 = <FS_FINAL2>-ODQTY1.
LW_EXCEL2-ODQTY2 = <FS_FINAL2>-ODQTY2.
LW_EXCEL2-ODQTY3 = <FS_FINAL2>-ODQTY3.
LW_EXCEL2-ODQTY = <FS_FINAL2>-ODQTY.

APPEND LW_EXCEL2 TO LT_EXCEL2.
CLEAR LW_EXCEL2.
ENDLOOP.

CLEAR: LW_EXCEL2, LV_HSDQT, LV_HFHSD, LV_OTHQT, LV_TMQTY, LV_ODQTY1, LV_ODQTY2, LV_ODQTY3, LV_ODQTY, LV_COUNT.
LOOP AT LT_EXCEL2 ASSIGNING <FS_TOQTY2>.
LV_HSDQT = LV_HSDQT + <FS_TOQTY2>-HSDQT.
LV_HFHSD = LV_HFHSD + <FS_TOQTY2>-HFHSD.
LV_OTHQT = LV_OTHQT + <FS_TOQTY2>-OTHQT.
LV_TMQTY = LV_TMQTY + <FS_TOQTY2>-TOTQT.
LV_ODQTY1 = LV_ODQTY1 + <FS_TOQTY2>-ODQTY1.
LV_ODQTY2 = LV_ODQTY2 + <FS_TOQTY2>-ODQTY2.
LV_ODQTY3 = LV_ODQTY3 + <FS_TOQTY2>-ODQTY3.
LV_ODQTY = LV_ODQTY + <FS_TOQTY2>-ODQTY.
ENDLOOP.

LV_COUNT = LINES( LT_EXCEL2 ).

LV_COUNT = LV_COUNT + 1.

LW_EXCEL2-HSDQT = LV_HSDQT.
LW_EXCEL2-HFHSD = LV_HFHSD.
LW_EXCEL2-OTHQT = LV_OTHQT.
LW_EXCEL2-TOTQT = LV_TMQTY.
LW_EXCEL2-ODQTY1 = LV_ODQTY1.
LW_EXCEL2-ODQTY2 = LV_ODQTY2.
LW_EXCEL2-ODQTY3 = LV_ODQTY3.
LW_EXCEL2-ODQTY = LV_ODQTY.

INSERT LW_EXCEL2 INTO LT_EXCEL2 INDEX LV_COUNT.

CLEAR LV_TABIX.
LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP2>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL2 ASSIGNING <FS_EXCEL2> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL2>-CGRPT1 = <FS_CUGRP2>-CGRPT.
<FS_EXCEL2>-TOTQT1 = <FS_CUGRP2>-TOTQT.
ENDIF.
ENDLOOP.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT2>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL2 ASSIGNING <FS_EXCEL2> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL2>-PLNTT = <FS_LOAPT2>-PLNTT.
<FS_EXCEL2>-TOTQT2 = <FS_LOAPT2>-TOTQT.
ENDIF.
ENDLOOP.

************************Third Sheet Final data******************

CLEAR: LW_FINAL3.
LOOP AT LT_FINAL3 INTO LW_FINAL3.
COLLECT LW_FINAL3 INTO LT_FINAL3_TEMP.
ENDLOOP.

CLEAR: LT_FINAL3.
IF LT_FINAL3 IS INITIAL.
LT_FINAL3 = LT_FINAL3_TEMP.
ENDIF.

SORT LT_FINAL3 BY CGRPT.

CLEAR: LW_FINAL3, LT_CUGRP, LW_CUGRP.
LOOP AT LT_FINAL3 INTO LW_FINAL3.
LW_CUGRP-CGRPT = LW_FINAL3-CGRPT.
LW_CUGRP-TOTQT = LW_FINAL3-TOTQT.
APPEND LW_CUGRP TO LT_CUGRP.
CLEAR LW_CUGRP.
ENDLOOP.

SORT LT_CUGRP BY CGRPT.

CLEAR: LW_CUGRP, LW_CUGRP_TEMP, LT_CUGRP_TEMP.
LOOP AT LT_CUGRP INTO LW_CUGRP.
AT END OF CGRPT.
LW_CUGRP_TEMP-CGRPT = LW_CUGRP-CGRPT.
SUM.
LW_CUGRP_TEMP-TOTQT = LW_CUGRP-TOTQT.
APPEND LW_CUGRP_TEMP TO LT_CUGRP_TEMP.
CLEAR LW_CUGRP_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_CUGRP_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP_TEMP3>.
LV_TOTAL = LV_TOTAL + <FS_CUGRP_TEMP3>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_CUGRP_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_CUGRP_TEMP-CGRPT = 'TOTAL'.
LW_CUGRP_TEMP-TOTQT = LV_TOTAL.

INSERT LW_CUGRP_TEMP INTO LT_CUGRP_TEMP INDEX LV_COUNT.

SORT LT_FINAL2 BY PLNTT.

CLEAR: LW_FINAL3, LW_LOAPT, LT_LOAPT.
LOOP AT LT_FINAL3 INTO LW_FINAL3.
LW_LOAPT-PLNTT = LW_FINAL3-PLNTT.
LW_LOAPT-TOTQT = LW_FINAL3-TOTQT.
APPEND LW_LOAPT TO LT_LOAPT.
CLEAR LW_LOAPT.
ENDLOOP.

SORT LT_LOAPT BY PLNTT.

CLEAR: LW_LOAPT, LW_LOAPT_TEMP, LT_LOAPT_TEMP.
LOOP AT LT_LOAPT INTO LW_LOAPT.
AT END OF PLNTT.
LW_LOAPT_TEMP-PLNTT = LW_LOAPT-PLNTT.
SUM.
LW_LOAPT_TEMP-TOTQT = LW_LOAPT-TOTQT.
APPEND LW_LOAPT_TEMP TO LT_LOAPT_TEMP.
CLEAR LW_LOAPT_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_LOAPT_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT_TEMP3>.
LV_TOTAL = LV_TOTAL + <FS_LOAPT_TEMP3>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_LOAPT_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_LOAPT_TEMP-PLNTT = 'TOTAL'.
LW_LOAPT_TEMP-TOTQT = LV_TOTAL.

INSERT LW_LOAPT_TEMP INTO LT_LOAPT_TEMP INDEX LV_COUNT.

CLEAR: LT_EXCEL3[]. " Third Sheet Final data

LOOP AT LT_FINAL3 ASSIGNING <FS_FINAL3>.

LW_EXCEL3-CGRPT = <FS_FINAL3>-CGRPT.
LW_EXCEL3-SOTPN = <FS_FINAL3>-SOTPN.
LW_EXCEL3-SOTPT = <FS_FINAL3>-SOTPT.
LW_EXCEL3-ASOTP = <FS_FINAL3>-ASOTP.
LW_EXCEL3-SHTPN = <FS_FINAL3>-SHTPN.
LW_EXCEL3-SHTPT = <FS_FINAL3>-SHTPT.
LW_EXCEL3-ASHTP = <FS_FINAL3>-ASHTP.
LW_EXCEL3-LOAPT = <FS_FINAL3>-LOAPT.
LW_EXCEL3-TOTQT = <FS_FINAL3>-TOTQT.
LW_EXCEL3-ODQTY = <FS_FINAL3>-ODQTY.

APPEND LW_EXCEL3 TO LT_EXCEL3.
CLEAR LW_EXCEL3.
ENDLOOP.

CLEAR: LW_EXCEL3, LV_TMQTY, LV_ODQTY, LV_COUNT.
LOOP AT LT_EXCEL3 ASSIGNING <FS_TOQTY3>.
LV_TMQTY = LV_TMQTY + <FS_TOQTY3>-TOTQT.
LV_ODQTY = LV_ODQTY + <FS_TOQTY3>-ODQTY.
ENDLOOP.

LV_COUNT = LINES( LT_EXCEL3 ).

LV_COUNT = LV_COUNT + 1.

LW_EXCEL3-TOTQT = LV_TMQTY.
LW_EXCEL3-ODQTY = LV_ODQTY.

INSERT LW_EXCEL3 INTO LT_EXCEL3 INDEX LV_COUNT.

CLEAR LV_TABIX.
LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP3>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL3 ASSIGNING <FS_EXCEL3> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL3>-CGRPT1 = <FS_CUGRP3>-CGRPT.
<FS_EXCEL3>-TOTQT1 = <FS_CUGRP3>-TOTQT.
ENDIF.
ENDLOOP.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT3>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL3 ASSIGNING <FS_EXCEL3> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL3>-PLNTT = <FS_LOAPT3>-PLNTT.
<FS_EXCEL3>-TOTQT2 = <FS_LOAPT3>-TOTQT.
ENDIF.
ENDLOOP.

************************Fourth Sheet Final data******************

CLEAR: LW_FINAL4.
LOOP AT LT_FINAL4 INTO LW_FINAL4.
COLLECT LW_FINAL4 INTO LT_FINAL4_TEMP.
ENDLOOP.

CLEAR: LT_FINAL4.
IF LT_FINAL4 IS INITIAL.
LT_FINAL4 = LT_FINAL4_TEMP.
ENDIF.

SORT LT_FINAL4 BY CGRPT.

CLEAR: LW_FINAL4, LT_CUGRP, LW_CUGRP.
LOOP AT LT_FINAL4 INTO LW_FINAL4.
LW_CUGRP-CGRPT = LW_FINAL4-CGRPT.
LW_CUGRP-TOTQT = LW_FINAL4-TOTQT.
APPEND LW_CUGRP TO LT_CUGRP.
CLEAR LW_CUGRP.
ENDLOOP.

SORT LT_CUGRP BY CGRPT.

CLEAR: LW_CUGRP, LW_CUGRP_TEMP, LT_CUGRP_TEMP.
LOOP AT LT_CUGRP INTO LW_CUGRP.
AT END OF CGRPT.
LW_CUGRP_TEMP-CGRPT = LW_CUGRP-CGRPT.
SUM.
LW_CUGRP_TEMP-TOTQT = LW_CUGRP-TOTQT.
APPEND LW_CUGRP_TEMP TO LT_CUGRP_TEMP.
CLEAR LW_CUGRP_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_CUGRP_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP_TEMP4>.
LV_TOTAL = LV_TOTAL + <FS_CUGRP_TEMP4>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_CUGRP_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_CUGRP_TEMP-CGRPT = 'TOTAL'.
LW_CUGRP_TEMP-TOTQT = LV_TOTAL.

INSERT LW_CUGRP_TEMP INTO LT_CUGRP_TEMP INDEX LV_COUNT.

SORT LT_FINAL2 BY PLNTT.

CLEAR: LW_FINAL4, LW_LOAPT, LT_LOAPT.
LOOP AT LT_FINAL4 INTO LW_FINAL4.
LW_LOAPT-PLNTT = LW_FINAL4-PLNTT.
LW_LOAPT-TOTQT = LW_FINAL4-TOTQT.
APPEND LW_LOAPT TO LT_LOAPT.
CLEAR LW_LOAPT.
ENDLOOP.

SORT LT_LOAPT BY PLNTT.

CLEAR: LW_LOAPT, LW_LOAPT_TEMP, LT_LOAPT_TEMP.
LOOP AT LT_LOAPT INTO LW_LOAPT.
AT END OF PLNTT.
LW_LOAPT_TEMP-PLNTT = LW_LOAPT-PLNTT.
SUM.
LW_LOAPT_TEMP-TOTQT = LW_LOAPT-TOTQT.
APPEND LW_LOAPT_TEMP TO LT_LOAPT_TEMP.
CLEAR LW_LOAPT_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_LOAPT_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT_TEMP4>.
LV_TOTAL = LV_TOTAL + <FS_LOAPT_TEMP4>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_LOAPT_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_LOAPT_TEMP-PLNTT = 'TOTAL'.
LW_LOAPT_TEMP-TOTQT = LV_TOTAL.

INSERT LW_LOAPT_TEMP INTO LT_LOAPT_TEMP INDEX LV_COUNT.

CLEAR: LT_EXCEL4[]. " Fourth Sheet Final data

LOOP AT LT_FINAL4 ASSIGNING <FS_FINAL4>.

LW_EXCEL4-CGRPT = <FS_FINAL4>-CGRPT.
LW_EXCEL4-SOTPN = <FS_FINAL4>-SOTPN.
LW_EXCEL4-SOTPT = <FS_FINAL4>-SOTPT.
LW_EXCEL4-ASOTP = <FS_FINAL4>-ASOTP.
LW_EXCEL4-SHTPN = <FS_FINAL4>-SHTPN.
LW_EXCEL4-SHTPT = <FS_FINAL4>-SHTPT.
LW_EXCEL4-ASHTP = <FS_FINAL4>-ASHTP.
LW_EXCEL4-LOAPT = <FS_FINAL4>-LOAPT.
LW_EXCEL4-TOTQT = <FS_FINAL4>-TOTQT.
LW_EXCEL4-ODQTY = <FS_FINAL4>-ODQTY.

APPEND LW_EXCEL4 TO LT_EXCEL4.
CLEAR LW_EXCEL4.
ENDLOOP.

CLEAR: LW_EXCEL4, LV_TMQTY, LV_ODQTY, LV_COUNT.
LOOP AT LT_EXCEL4 ASSIGNING <FS_TOQTY4>.
LV_TMQTY = LV_TMQTY + <FS_TOQTY4>-TOTQT.
LV_ODQTY = LV_ODQTY + <FS_TOQTY4>-ODQTY.
ENDLOOP.

LV_COUNT = LINES( LT_EXCEL4 ).

LV_COUNT = LV_COUNT + 1.

LW_EXCEL4-TOTQT = LV_TMQTY.
LW_EXCEL4-ODQTY = LV_ODQTY.

INSERT LW_EXCEL4 INTO LT_EXCEL4 INDEX LV_COUNT.

CLEAR LV_TABIX.
LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP4>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL4 ASSIGNING <FS_EXCEL4> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL4>-CGRPT1 = <FS_CUGRP4>-CGRPT.
<FS_EXCEL4>-TOTQT1 = <FS_CUGRP4>-TOTQT.
ENDIF.
ENDLOOP.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT4>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL4 ASSIGNING <FS_EXCEL4> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL4>-PLNTT = <FS_LOAPT4>-PLNTT.
<FS_EXCEL4>-TOTQT2 = <FS_LOAPT4>-TOTQT.
ENDIF.
ENDLOOP.

************************Fifth Sheet Final data******************

CLEAR: LW_FINAL5.
LOOP AT LT_FINAL5 INTO LW_FINAL5.
COLLECT LW_FINAL5 INTO LT_FINAL5_TEMP.
ENDLOOP.

CLEAR: LT_FINAL5.
IF LT_FINAL5 IS INITIAL.
LT_FINAL5 = LT_FINAL5_TEMP.
ENDIF.

SORT LT_FINAL5 BY CGRPT.

CLEAR: LW_FINAL5, LT_CUGRP, LW_CUGRP.
LOOP AT LT_FINAL5 INTO LW_FINAL5.
LW_CUGRP-CGRPT = LW_FINAL5-CGRPT.
LW_CUGRP-TOTQT = LW_FINAL5-TOTQT.
APPEND LW_CUGRP TO LT_CUGRP.
CLEAR LW_CUGRP.
ENDLOOP.

SORT LT_CUGRP BY CGRPT.

CLEAR: LW_CUGRP, LW_CUGRP_TEMP, LT_CUGRP_TEMP.
LOOP AT LT_CUGRP INTO LW_CUGRP.
AT END OF CGRPT.
LW_CUGRP_TEMP-CGRPT = LW_CUGRP-CGRPT.
SUM.
LW_CUGRP_TEMP-TOTQT = LW_CUGRP-TOTQT.
APPEND LW_CUGRP_TEMP TO LT_CUGRP_TEMP.
CLEAR LW_CUGRP_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_CUGRP_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP_TEMP5>.
LV_TOTAL = LV_TOTAL + <FS_CUGRP_TEMP5>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_CUGRP_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_CUGRP_TEMP-CGRPT = 'TOTAL'.
LW_CUGRP_TEMP-TOTQT = LV_TOTAL.

INSERT LW_CUGRP_TEMP INTO LT_CUGRP_TEMP INDEX LV_COUNT.

SORT LT_FINAL5 BY PLNTT.

CLEAR: LW_FINAL5, LW_LOAPT, LT_LOAPT.
LOOP AT LT_FINAL5 INTO LW_FINAL5.
LW_LOAPT-PLNTT = LW_FINAL5-PLNTT.
LW_LOAPT-TOTQT = LW_FINAL5-TOTQT.
APPEND LW_LOAPT TO LT_LOAPT.
CLEAR LW_LOAPT.
ENDLOOP.

SORT LT_LOAPT BY PLNTT.

CLEAR: LW_LOAPT, LW_LOAPT_TEMP, LT_LOAPT_TEMP.
LOOP AT LT_LOAPT INTO LW_LOAPT.
AT END OF PLNTT.
LW_LOAPT_TEMP-PLNTT = LW_LOAPT-PLNTT.
SUM.
LW_LOAPT_TEMP-TOTQT = LW_LOAPT-TOTQT.
APPEND LW_LOAPT_TEMP TO LT_LOAPT_TEMP.
CLEAR LW_LOAPT_TEMP.
ENDAT.
ENDLOOP.

CLEAR: LW_LOAPT_TEMP, LV_TOTAL, LV_COUNT.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT_TEMP5>.
LV_TOTAL = LV_TOTAL + <FS_LOAPT_TEMP5>-TOTQT.
ENDLOOP.

LV_COUNT = LINES( LT_LOAPT_TEMP ).

LV_COUNT = LV_COUNT + 1.

LW_LOAPT_TEMP-PLNTT = 'TOTAL'.
LW_LOAPT_TEMP-TOTQT = LV_TOTAL.

INSERT LW_LOAPT_TEMP INTO LT_LOAPT_TEMP INDEX LV_COUNT.

CLEAR: LT_EXCEL5[]. " Fifth Sheet Final data

LOOP AT LT_FINAL5 ASSIGNING <FS_FINAL5>.

LW_EXCEL5-CGRPT = <FS_FINAL5>-CGRPT.
LW_EXCEL5-SOTPN = <FS_FINAL5>-SOTPN.
LW_EXCEL5-SOTPT = <FS_FINAL5>-SOTPT.
LW_EXCEL5-ASOTP = <FS_FINAL5>-ASOTP.
LW_EXCEL5-SHTPN = <FS_FINAL5>-SHTPN.
LW_EXCEL5-SHTPT = <FS_FINAL5>-SHTPT.
LW_EXCEL5-ASHTP = <FS_FINAL5>-ASHTP.
LW_EXCEL5-LOAPT = <FS_FINAL5>-LOAPT.
LW_EXCEL5-TOTQT = <FS_FINAL5>-TOTQT.
LW_EXCEL5-ODQTY = <FS_FINAL5>-ODQTY.

APPEND LW_EXCEL5 TO LT_EXCEL5.
CLEAR LW_EXCEL5.
ENDLOOP.

CLEAR: LW_EXCEL5, LV_TMQTY, LV_ODQTY, LV_COUNT.
LOOP AT LT_EXCEL5 ASSIGNING <FS_TOQTY5>.
LV_TMQTY = LV_TMQTY + <FS_TOQTY5>-TOTQT.
LV_ODQTY = LV_ODQTY + <FS_TOQTY5>-ODQTY.
ENDLOOP.

LV_COUNT = LINES( LT_EXCEL5 ).

LV_COUNT = LV_COUNT + 1.

LW_EXCEL5-TOTQT = LV_TMQTY.
LW_EXCEL5-ODQTY = LV_ODQTY.

INSERT LW_EXCEL5 INTO LT_EXCEL5 INDEX LV_COUNT.

CLEAR LV_TABIX.
LOOP AT LT_CUGRP_TEMP ASSIGNING <FS_CUGRP5>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL5 ASSIGNING <FS_EXCEL5> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL5>-CGRPT1 = <FS_CUGRP5>-CGRPT.
<FS_EXCEL5>-TOTQT1 = <FS_CUGRP5>-TOTQT.
ENDIF.
ENDLOOP.

LOOP AT LT_LOAPT_TEMP ASSIGNING <FS_LOAPT5>.
CLEAR LV_TABIX.
LV_TABIX = SY-TABIX.
READ TABLE LT_EXCEL5 ASSIGNING <FS_EXCEL5> INDEX LV_TABIX.
IF SY-SUBRC IS INITIAL.
<FS_EXCEL5>-PLNTT = <FS_LOAPT5>-PLNTT.
<FS_EXCEL5>-TOTQT2 = <FS_LOAPT5>-TOTQT.
ENDIF.
ENDLOOP.

******************** End of Excel Data ***********************

*-- Writing the XML Header tags into a global internal table
MOVE C_HEADER1 TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_HEADER01 TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_HEADER2 TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_HEADER3 TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_HEADER4 TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_HEADER5 TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

*-- Creating a global style in which we mention about font that will be followed across the workbook.
MOVE C_STYLESBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_STYLEBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_FONT TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_STYLEED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

WA_XML-LINE = C_STYLEID.
APPEND WA_XML TO I_XML.
WA_XML-LINE = C_HDCOL.
APPEND WA_XML TO I_XML.
WA_XML-LINE = C_HDALG.
APPEND WA_XML TO I_XML.
WA_XML-LINE = C_STYLEED.
APPEND WA_XML TO I_XML.

MOVE C_STYLESED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

******** First Sheet ***********************
*-- Preparing 'Header' sheet
CONCATENATE C_WRKSHTBE 'HSD & HFHSD' C_WRKSHTTG
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

MOVE C_TABLEBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="110"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="110"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="60"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

CLEAR: LW_EXCEL1.
LOOP AT LT_EXCEL1 INTO LW_EXCEL1.

IF SY-TABIX = 1.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Customer Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Loading Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'HSD-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'HFHSD-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'OTHERS-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Total-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'HSD-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'HFHSD-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'OTHERS-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Total-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
ENDIF.

*-- Writing the XML code to start a row in the table inserted above to the particular sheet.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-CGRPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-SOTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-SOTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-ASOTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-SHTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-SHTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-ASHTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-LOAPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-HSDQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-HFHSD C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-OTHQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-TOTQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-ODQTY1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-ODQTY2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-ODQTY3 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-ODQTY C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-LV_FLAG1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-CGRPT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-TOTQT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-LV_FLAG2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-PLNTT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL1-TOTQT2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

*-- Writing the XML code for ending a row after writing data for all the columns.
MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

ENDLOOP.

*-- Close the table and the sheet.
MOVE C_TABLEED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_WRKSHTED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

******** Second Sheet ********************
*-- Preparing 'Header' sheet
CONCATENATE C_WRKSHTBE 'BITUMEN' C_WRKSHTTG
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

MOVE C_TABLEBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="110"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="110"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="100"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="60"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

CLEAR: LW_EXCEL2.
LOOP AT LT_EXCEL2 INTO LW_EXCEL2.

IF SY-TABIX = 1.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Customer Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Loading Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'VG30-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'VG40-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'OTHERS-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Total-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'VG30-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'VG40-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'OTHERS-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Total-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
ENDIF.

*-- Writing the XML code to start a row in the table inserted above to the particular sheet.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-CGRPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-SOTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-SOTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-ASOTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-SHTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-SHTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-ASHTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-LOAPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-HSDQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-HFHSD C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-OTHQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-TOTQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-ODQTY1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-ODQTY2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-ODQTY3 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-ODQTY C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-LV_FLAG1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-CGRPT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-TOTQT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-LV_FLAG2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-PLNTT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL2-TOTQT2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

*-- Writing the XML code for ending a row after writing data for all the columns.
MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

ENDLOOP.

*-- Close the table and the sheet.
MOVE C_TABLEED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_WRKSHTED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

******* Third Sheet *************
*-- Preparing 'Header' sheet
CONCATENATE C_WRKSHTBE 'PETCOKE' C_WRKSHTTG
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

MOVE C_TABLEBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="60"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

CLEAR: LW_EXCEL3.
LOOP AT LT_EXCEL3 INTO LW_EXCEL3.

IF SY-TABIX = 1.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Customer Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Loading Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Monthly-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Yesterday-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
ENDIF.

*-- Writing the XML code to start a row in the table inserted above to the particular sheet.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-CGRPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-SOTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-SOTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-ASOTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-SHTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-SHTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-ASHTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-LOAPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-TOTQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-ODQTY C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-LV_FLAG1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-CGRPT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-TOTQT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-LV_FLAG2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-PLNTT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL3-TOTQT2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

*-- Writing the XML code for ending a row after writing data for all the columns.
MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

ENDLOOP.

*-- Close the table and the sheet.
MOVE C_TABLEED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_WRKSHTED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

******* Fourth Sheet *************
*-- Preparing 'Header' sheet
CONCATENATE C_WRKSHTBE 'SULPHUR' C_WRKSHTTG
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

MOVE C_TABLEBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="60"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

CLEAR: LW_EXCEL4.
LOOP AT LT_EXCEL4 INTO LW_EXCEL4.

IF SY-TABIX = 1.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Customer Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Loading Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Monthly-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Yesterday-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
ENDIF.

*-- Writing the XML code to start a row in the table inserted above to the particular sheet.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-CGRPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-SOTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-SOTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-ASOTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-SHTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-SHTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-ASHTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-LOAPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-TOTQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-ODQTY C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-LV_FLAG1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-CGRPT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-TOTQT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-LV_FLAG2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-PLNTT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL4-TOTQT2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

*-- Writing the XML code for ending a row after writing data for all the columns.
MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

ENDLOOP.

*-- Close the table and the sheet.
MOVE C_TABLEED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_WRKSHTED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

******* Fifth Sheet *************

*-- Preparing 'Header' sheet
CONCATENATE C_WRKSHTBE 'BOTTOM ASH' C_WRKSHTTG
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

MOVE C_TABLEBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="60"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="40"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="120"/>'.
APPEND WA_XML TO I_XML.

WA_XML-LINE = '<Column ss:Width="80"/>'.
APPEND WA_XML TO I_XML.

CLEAR: LW_EXCEL5.
LOOP AT LT_EXCEL5 INTO LW_EXCEL5.

IF SY-TABIX = 1.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Customer Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Sold-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Sold-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Ship-To-Party' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Area Manager Ship-To' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Loading Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Monthly-' P_DATE2 C_DTCELL INTO WA_XML-LINE. "'Monthly Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Yesterday-' P_DATE1 C_DTCELL INTO WA_XML-LINE. "'Yesterday Qty'
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Type' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR ' ' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Plant' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_HDR 'Qty' C_DTCELL INTO WA_XML-LINE.
APPEND WA_XML TO I_XML.

MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
ENDIF.

*-- Writing the XML code to start a row in the table inserted above to the particular sheet.
MOVE C_ROWBE TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-CGRPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-SOTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-SOTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-ASOTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-SHTPN C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-SHTPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-ASHTP C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-LOAPT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-TOTQT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-ODQTY C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-LV_FLAG1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-CGRPT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-TOTQT1 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-LV_FLAG2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-PLNTT C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

CONCATENATE C_CELLBE C_DATABE LW_EXCEL5-TOTQT2 C_DATAED C_CELLED
INTO WA_XML-LINE
SEPARATED BY ' '.
APPEND WA_XML TO I_XML.

*-- Writing the XML code for ending a row after writing data for all the columns.
MOVE C_ROWED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

ENDLOOP.

*-- Close the table and the sheet.
MOVE C_TABLEED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.
MOVE C_WRKSHTED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

*******************************************************

*-- After transferring all the data in to the XML form we need to end the XML workbook created.
MOVE C_WRKBKED TO WA_XML-LINE.
APPEND WA_XML TO I_XML.

*-- Convert the XML data into Binary data
LOOP AT I_XML INTO WA_XML.
CONCATENATE V_STRING WA_XML GC_CRLF INTO V_STRING.
ENDLOOP.

TRY.
CL_BCS_CONVERT=>STRING_TO_SOLIX(
EXPORTING
IV_STRING = V_STRING
IV_CODEPAGE = '4103' " '4110' suitable for MS Excel, leave empty
IV_ADD_BOM = 'X' "for other doc types
IMPORTING
ET_SOLIX = I_BINARY_CONTENT
EV_SIZE = V_SIZE ).
CATCH CX_BCS.
MESSAGE E445(SO).
ENDTRY.

*-- Send email
TRY.

*-- Create persistent send request
SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

*-- Create and set document with attachment
* Create document object from internal table with text
APPEND 'Dear Sir,' TO MAIN_TEXT.
APPEND 'Please find the daily bulk invoice report attachment.' TO MAIN_TEXT.
DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = 'RAW'
I_TEXT = MAIN_TEXT
I_SUBJECT = 'Daily Bulk Invoice Report' ).

*-- Add the spread sheet as attachment to document object

DOCUMENT->ADD_ATTACHMENT(
I_ATTACHMENT_TYPE = 'XLS'
I_ATTACHMENT_SUBJECT = 'Invoice Daily Bulk Report'
I_ATTACHMENT_SIZE = V_SIZE
I_ATT_CONTENT_HEX = I_BINARY_CONTENT ).
* i_attachment_header = lt_att_head ).

*-- Add document object to send request
SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).

*-- Add sender to send request
LV_MAIL = 'sales@xyz.com'.
LO_SENDER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( LV_MAIL ).
SEND_REQUEST->SET_SENDER( LO_SENDER ).

*-- Add recipient (e-mail address) 'TO'
CLEAR: LW_MAIL.
LOOP AT LT_MAIL INTO LW_MAIL.
IF NOT LW_MAIL-TO_MAIL_ID IS INITIAL.
CLEAR: MAILTO.
MAILTO = LW_MAIL-TO_MAIL_ID.
RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
SEND_REQUEST->ADD_RECIPIENT(
I_RECIPIENT = RECIPIENT
I_EXPRESS = 'X' ).
ENDIF.
ENDLOOP.

*-- Add recipient (e-mail address) 'CC'
CLEAR: LW_MAIL.
LOOP AT LT_MAIL INTO LW_MAIL.
IF NOT LW_MAIL-CC_MAIL_ID IS INITIAL.
CLEAR: MAILTO.
MAILTO = LW_MAIL-CC_MAIL_ID.
RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( MAILTO ).
SEND_REQUEST->ADD_RECIPIENT(
I_RECIPIENT = RECIPIENT
I_COPY = 'X' ).
ENDIF.
ENDLOOP.

*-- Set send immediately flag
SEND_REQUEST->SET_SEND_IMMEDIATELY( 'X' ).

*-- Send document
SENT_TO_ALL = SEND_REQUEST->SEND( I_WITH_ERROR_SCREEN = 'X' ).
COMMIT WORK.
IF SENT_TO_ALL IS INITIAL.
MESSAGE I500(SBCOMS) WITH MAILTO.
ELSE.
MESSAGE S022(SO).
ENDIF.

*-- Exception handling
CATCH CX_BCS INTO BCS_EXCEPTION.
MESSAGE I865(SO) WITH BCS_EXCEPTION->ERROR_TYPE.

ENDTRY.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

You are still sending an attachment containing Excel 2003 XML format (AKA XMLSS). No matter which extension you assign to this attachment, it definitely doesn't contain Excel Office Open XML format (when you save from Excel to xlsx).

If their smart phones only read Office Open XML format, then you must rewrite your program to build the file in Excel Office Open XML format -> use abap2xlsx

0
avatar image
Former Member Dec 26, 2016 at 03:20 PM
0

hi sandra,

Thank u for ur reply. I shared my full report. Basically what declarations i need to change. If there is any sample programs it would be great. I have 5 sheets in my excel report. Can you share code for atleast one sheet so that i can understand this logic and then make necessary changes for other sheet.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Basically everything must be rewritten. See the example in my first answer (which was a complement of Jelena's one).

0