Professional Documents
Culture Documents
Excel Formatting
Excel Formatting
REPORT zbps_hoja_3.
* Type Pools
TYPE-POOLS ole2 .
*----------------------------------------------------------------------*
* CLASS lcl_data DEFINITION
*----------------------------------------------------------------------*
* Local Class Definition
*----------------------------------------------------------------------*
CLASS lcl_data DEFINITION FINAL.
PUBLIC SECTION.
*--------------------------------------------------------------------*
* Types
TYPES: BEGIN OF t_ska1,
ktopl TYPE ska1-ktopl,
saknr TYPE ska1-saknr,
xbilk TYPE ska1-xbilk,
END OF t_ska1.
TYPES:
BEGIN OF t_header,
field1 TYPE string,
field2 TYPE string,
field3 TYPE string,
field4 TYPE string,
field5 TYPE string,
field6 TYPE string,
field7 TYPE string,
field8 TYPE string,
field9 TYPE string,
END OF t_header.
*--------------------------------------------------------------------*
* Internal tables
DATA:
i_ska1 TYPE TABLE OF t_ska1,
i_skat TYPE TABLE OF t_skat,
i_output TYPE TABLE OF t_output,
i_header TYPE TABLE OF t_header,
i_header_1 TYPE TABLE OF t_header.
*--------------------------------------------------------------------*
* Work areas
DATA:
i_ska1_line TYPE t_ska1,
i_skat_line TYPE t_skat,
i_output_line TYPE t_output,
i_header_line TYPE t_header,
i_header_line3 TYPE t_header,
i_header_line4 TYPE t_header.
*--------------------------------------------------------------------*
* Data
DATA: g_ktopl TYPE ska1-ktopl,
g_saknr TYPE ska1-saknr,
g_bukrs TYPE skb1-bukrs,
g_gjahr TYPE skc1a-gjahr,
g_fullpath TYPE string,
g_name TYPE t7ru9a-regno.
DATA:
i_glt0 TYPE fagl_t_glt0,
i_glt0_line TYPE glt0.
*--------------------------------------------------------------------*
* Methods
METHODS:
validate_ktopl, " Validate Chart of Accounts
validate_sankr, " Validate G/L Account Number
validate_bukrs, " Validate Company Code
validate_gjahr, " Validate Fiscal Year
validate_month, " Validate Month
get_data, " Get Data
get_text, " Get short text
get_accounts, " Get Account Details
output_table, " Output table
download_data, " Download data
excl_format, " Format the Excl Sheet
header_data. " Header data
**********************************************************************
* Initialization *
**********************************************************************
INITIALIZATION.
CREATE OBJECT obj_data.
**********************************************************************
* Selection-screen *
**********************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
* Select options
SELECT-OPTIONS: s_ktopl FOR obj_data->g_ktopl,
s_saknr FOR obj_data->g_saknr MATCHCODE OBJECT sako,
s_bukrs FOR obj_data->g_bukrs OBLIGATORY,
s_gjahr FOR obj_data->g_gjahr OBLIGATORY NO INTERVALS.
PARAMETERS: p_monat TYPE bkpf-monat OBLIGATORY .
**********************************************************************
* Selection-screen Events *
**********************************************************************
AT SELECTION-SCREEN ON s_ktopl.
* Validate Chart of Accounts
CALL METHOD obj_data->validate_ktopl.
AT SELECTION-SCREEN ON s_saknr.
* Validate G/L Account Number
CALL METHOD obj_data->validate_sankr.
AT SELECTION-SCREEN ON s_bukrs.
* Validate Company Code
CALL METHOD obj_data->validate_bukrs.
AT SELECTION-SCREEN ON s_gjahr.
* Validate year
CALL METHOD obj_data->validate_gjahr.
AT SELECTION-SCREEN ON p_monat.
* Validate month
CALL METHOD obj_data->validate_month.
**********************************************************************
* Start of Selection *
**********************************************************************
START-OF-SELECTION.
* Data retrieval
CALL METHOD obj_data->get_data.
**********************************************************************
* End of Selection *
**********************************************************************
END-OF-SELECTION.
* Initial check
IF obj_data->i_glt0 IS INITIAL.
MESSAGE 'No data found for the Selection'(007) TYPE 'S'
DISPLAY LIKE 'E'.
RETURN.
ENDIF. " IF obj_data->i_glt0 IS INITIAL.
* For Populating Output table
CALL METHOD obj_data->output_table.
* For Download the Excl Format
CALL METHOD obj_data->download_data.
*----------------------------------------------------------------------*
* CLASS lcl_data IMPLEMENTATION
*----------------------------------------------------------------------*
* Local class Implementation
*----------------------------------------------------------------------*
CLASS lcl_data IMPLEMENTATION.
**********************************************************************
* Validate Chart of Accounts
**********************************************************************
METHOD validate_ktopl.
IF s_ktopl IS NOT INITIAL.
* Validate the Chart of Accounts in G/L Account Master (Chart of Accounts)
SELECT ktopl
INTO g_ktopl
FROM t004
UP TO 1 ROWS
WHERE ktopl IN s_ktopl.
ENDSELECT. " FROM t004
IF sy-subrc NE 0.
MESSAGE e000(oo) WITH 'Please enter a valid Chart of Accounts'(002).
ENDIF. " IF sy-subrc NE 0.
ENDIF. " IF s_ktopl IS NOT INITIAL.
IF s_gjahr CS '0123456789'.
MESSAGE e000(oo) WITH 'Please enter a valid year'(005).
ENDIF. " IF s_gjahr CS '0123456789'
ENDMETHOD. " Validate_gjahr
**********************************************************************
* Validate month
**********************************************************************
METHOD validate_month.
DATA:
r_ktopl_line TYPE fagl_range_rrcty,
r_sankr_lne TYPE fagl_range_racct.
* Range tables
APPEND s_bukrs TO r_bukrs.
APPEND s_gjahr TO r_gjahr.
IF s_ktopl IS INITIAL.
r_ktopl_line-sign = 'I'.
r_ktopl_line-option = 'NB'.
APPEND r_ktopl_line TO r_ktopl.
ELSE.
APPEND s_ktopl TO r_ktopl.
ENDIF. " IF s_ktopl IS INITIAL.
IF s_saknr IS INITIAL.
r_sankr_lne-sign = 'I'.
r_sankr_lne-option = 'NB'.
APPEND r_sankr_lne TO r_sankr.
ELSE.
APPEND s_saknr TO r_sankr.
ENDIF. " IF s_ktopl IS INITIAL.
DATA:
l_diff TYPE glt0-hsl01,
l_diff1 TYPE glt0-hsl01,
l_umxx TYPE glt0-hsl01.
*--------------------------------------------------------------------*
* Excl Sheet
CALL METHOD OF
l_application
'Workbooks' = l_workbook.
* Create new worksheet
SET PROPERTY OF l_application 'SheetsInNewWorkbook' = 1.
CALL METHOD OF
l_workbook
'Add'.
CALL METHOD OF
l_sheet
'Activate'.
*--------------------------------------------------------------------*
* First header data
l_rowindx = 1. "start at row 1 for headings
CALL METHOD OF
l_application
'Worksheets' = l_sheet
EXPORTING
#1 = 2.
CLEAR: l_colindx,i_header_line.
CLEAR i_header_line.
LOOP AT i_header_1 INTO i_header_line.
l_rowindx = l_rowindx + 1.
CLEAR l_colindx.
DO.
* Assign <fs> to table columns
ASSIGN COMPONENT sy-index OF STRUCTURE i_header_line TO <fs>.
IF sy-subrc NE 0.
EXIT.
ENDIF. " IF sy-subrc NE 0.
l_colindx = sy-index.
CALL METHOD OF
l_application
'Cells' = l_cell1
EXPORTING
#1 = l_rowindx
#2 = l_colindx.
CALL METHOD OF
l_application
'Cells' = l_cell2
EXPORTING
#1 = l_rowindx
#2 = 9.
CALL METHOD OF
l_application
'Range' = l_cells
EXPORTING
#1 = l_cell1
#2 = l_cell2.
CALL METHOD OF
l_cells
'Select'.
*--Merging
CALL METHOD OF
l_cells
'Merge'.
*--Setting title data
CALL METHOD OF
l_application
'Cells' = l_cell1
EXPORTING
#1 = l_rowindx
#2 = 1.
SET PROPERTY OF l_cell1 'Value'(014) = <fs> .
*--------------------------------------------------------------------*
* For empty row
* Fill columns for current row
CLEAR l_colindx.
l_rowindx = l_rowindx + 1.
CLEAR i_header_line.
DO.
* Assign <fs> to table columns
ASSIGN COMPONENT sy-index OF STRUCTURE i_header_line TO <fs>.
IF sy-subrc NE 0.
EXIT.
ENDIF. " IF sy-subrc NE 0.
l_colindx = sy-index.
CALL METHOD OF
l_sheet
'Cells' = l_cells
EXPORTING
#1 = l_rowindx
#2 = l_colindx.
l_index_r = l_rowindx + 1.
*--------------------------------------------------------------------*
* For Header line 3
CALL METHOD OF
l_application
'Worksheets' = l_sheet
EXPORTING
#1 = 3.
l_rowindx = l_rowindx + 1.
* Fill columns for current row
CLEAR l_colindx.
DO.
* Assign <fs> to table columns
ASSIGN COMPONENT sy-index OF STRUCTURE i_header_line3 TO <fs>.
IF sy-subrc NE 0.
EXIT.
ENDIF. " IF sy-subrc NE 0.
* l_colindx = sy-index.
IF sy-index EQ 1.
l_colindx = sy-index.
CALL METHOD OF
l_sheet
'Cells' = l_cells
EXPORTING
#1 = l_rowindx
#2 = l_colindx.
SET PROPERTY OF l_cells 'Value'(014) = <fs>.
ELSE.
l_colindx = l_colindx + 1.
l_index = l_colindx.
CALL METHOD OF
l_application
'Cells' = l_cell1
EXPORTING
#1 = l_rowindx
#2 = l_colindx.
l_colindx = l_colindx + 1.
CALL METHOD OF
l_application
'Cells' = l_cell2
EXPORTING
#1 = l_rowindx
#2 = l_colindx.
CALL METHOD OF
l_application
'Range' = l_cells
EXPORTING
#1 = l_cell1
#2 = l_cell2.
CALL METHOD OF
l_cells
'Select'.
*--Merging
CALL METHOD OF
l_cells
'Merge'.
l_rowindx = l_rowindx + 1.
CLEAR l_colindx.
DO.
* Assign <fs> to table columns
ASSIGN COMPONENT sy-index OF STRUCTURE i_header_line4 TO <fs>.
IF sy-subrc NE 0.
EXIT.
ENDIF. " IF sy-subrc NE 0.
l_colindx = sy-index.
CALL METHOD OF
l_sheet
'Cells' = l_cells
EXPORTING
#1 = l_rowindx
#2 = l_colindx.
*--------------------------------------------------------------------*
* Output data
CALL METHOD OF
l_application
'Worksheets' = l_sheet
EXPORTING
#1 = 3.
**********************************************************************
FREE l_range.
l_index_r = l_index_r + 1.
CALL METHOD OF l_application 'Cells' = l_cell1 "start cell
EXPORTING
#1 = 1 "down
#2 = 1. "across
CALL METHOD OF
l_application
'Range' = l_range
EXPORTING
#1 = l_cell1
#2 = l_cell2.
*--------------------------------------------------------------------*
* Modify properties of cell range
*--------------------------------------------------------------------*
* Set Columns to auto fit to width of text
CALL METHOD OF
l_application
'Columns' = l_column.
CALL METHOD OF
l_column
'Autofit'.
FREE OBJECT l_column.
*--------------------------------------------------------------------*
* border Properties
l_index_r = l_index_r - 1.
FREE l_range.
CALL METHOD OF l_application 'Cells' = l_cell1 "start cell
EXPORTING
#1 = l_index_r
#2 = 1.
CALL METHOD OF
l_application
'Range' = l_range
EXPORTING
#1 = l_cell1
#2 = l_cell2.
CALL METHOD OF
l_range
'BORDERS' = l_border
EXPORTING
#1 = '1'. "left
SET PROPERTY OF l_border 'LineStyle' = '1'. "line style solid, dashed...
SET PROPERTY OF l_border 'WEIGHT' = 2. "max = 4
FREE OBJECT l_border.
CALL METHOD OF
l_range
'BORDERS' = l_border
EXPORTING
#1 = '2'. "right
SET PROPERTY OF l_border 'LineStyle' = '1'.
SET PROPERTY OF l_border 'WEIGHT' = 2. "max = 4
FREE OBJECT l_border.
CALL METHOD OF
l_range
'BORDERS' = l_border
EXPORTING
#1 = '3'. "top
SET PROPERTY OF l_border 'LineStyle' = '1'.
SET PROPERTY OF l_border 'WEIGHT' = 2. "max = 4
FREE OBJECT l_border.
CALL METHOD OF
l_range
'BORDERS' = l_border
EXPORTING
#1 = '4'. "bottom
SET PROPERTY OF l_border 'LineStyle' = '1'.
SET PROPERTY OF l_border 'WEIGHT' = 2. "max = 4
FREE OBJECT l_border.
*--------------------------------------------------------------------*
* Modify properties of cell range
* SET FONT DETAILS of range
GET PROPERTY OF l_range 'Font'(015) = l_font.
SET PROPERTY OF l_font 'Size'(017) = 10.
*--------------------------------------------------------------------*
* Set Columns to auto fit to width of text
* CALL METHOD OF
* l_application
* 'Columns' = l_column.
* set PROPERTY OF l_column 'ColumnWidth' = '30'.
*
** CALL METHOD OF
** l_column
** 'Autofit'.
** FREE OBJECT l_column.
*--------------------------------------------------------------------*
* Save excel speadsheet to particular filename
CALL METHOD OF
l_sheet
'SaveAs'
EXPORTING
#1 = g_fullpath "'c:\temp\exceldoc2.xls' "filename
#2 = 1.
IF sy-subrc EQ 0.
MESSAGE s000(oo) WITH'Successfully download the Excel Sheet'(011).
ENDIF.
* Free
FREE OBJECT l_sheet.
FREE OBJECT l_workbook.
FREE OBJECT l_application.
ENDMETHOD. " Excl_format
**********************************************************************
* header data
**********************************************************************
METHOD header_data.
DATA l_index TYPE c.
* Header 1
DO 6 TIMES.
l_index = sy-index.
CONCATENATE 'HEADER' l_index INTO i_header_line-field1 SEPARATED BY space.
IF sy-index EQ 6.
i_header_line-field8 = 'Fecha :'(033).
i_header_line-field9 = sy-datum .
ENDIF.
APPEND i_header_line TO i_header.
ENDDO. " DO 6 TIMES.
CLEAR i_header_line.
APPEND i_header_line TO i_header.
* Header 2
CLEAR i_header_line.
i_header_line-field9 = 'Balance Tributario'(032).
APPEND i_header_line TO i_header_1.
CLEAR i_header_line.
i_header_line-field9 = 'Acumulado mes/a�o'(031).
APPEND i_header_line TO i_header_1.
CLEAR i_header_line.
CONCATENATE g_name s_gjahr-low INTO i_header_line-field9
SEPARATED BY `-`.
APPEND i_header_line TO i_header_1.
* Header 3
* Header 4
i_header_line4-field1 = ' '.
i_header_line4-field2 = 'D�bitos'(025).
i_header_line4-field3 = 'Cr�ditos'(024).
i_header_line4-field4 = 'Deudor'(023).
i_header_line4-field5 = 'Acreedor'(022).
i_header_line4-field6 = 'Activo'(021).
i_header_line4-field7 = 'Pasivo'(020).
i_header_line4-field8 = 'P�rdida'(019).
i_header_line4-field9 = 'Ganancia'(018).