Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

EXCEL With XML

$
0
0

Hi Friends

 

I would like to share some knowledge on how to generate an EXCEL file with designs and features of our interest.

 

I am working for a manufacturing company that naturally concentrates on dealers.So we have to develop applications that allow dealers(users) to upload and to download huge amount of data of various kinds in database/local system.

 

This is the reason we have to provide the provision of uploading and downloading excel sheet.I came across this method of creating an excel file and felt it as very helpful and easy to use.

 

Navigation guide
Introduction To Interface
        Document Creation
Properties Creation
         Column Creation
           Data Passing
            Downloading
             Uploading
              Examples

 

Introduction To Interface


The Interface we use to design Excel using XML is IF_IXML_ELEMENT.

 

Read following link to understand this interface in detail:SAP Library - XML- Library

 

 

 

Document Creation


A) The code shown below is a default code is written to create an excel document using XML method in ABAP.


a) Creating the DOM Object Model.


   l_document = l_ixml->create_document( ).


b) Create Root Node Workbook.


   l_element_root  = l_document->create_simple_element( name = 'Workbook'  parent = l_document ).
   l_element_root->set_attribute( name = 'xmlns'  value = 'urn:schemas-microsoft-com:office:spreadsheet' ).


c) Create Name Space for spread sheet.
  

   ns_attribute = l_document->create_namespace_decl( name = 'ss'  prefix = 'xmlns'  uri = 'urn:schemas-microsoft-com:office:spreadsheet' ).
   l_element_root->set_attribute_node( ns_attribute ).


d) Create Name Space for excel.


   ns_attribute = l_document->create_namespace_decl( name = 'x'  prefix = 'xmlns'  uri = 'urn:schemas-microsoft-com:office:excel' ).
   l_element_root->set_attribute_node( ns_attribute ).




Properties Creation


B) To set the properties like font size,styles,alignments,protection etc...we have to use following code.



a) Create node for document properties.
  

   r_element_properties = l_document->create_simple_element( name = 'TEST_NODE'  parent = l_element_root ).
   l_value = sy-uname.
   l_document->create_simple_element( name = 'Author'  value = l_value  parent = r_element_properties  ).



b) Creating Styles.


   r_styles = l_document->create_simple_element( name = 'Styles'  parent = l_element_root  ).


  • Style with ID as 'Header'


   r_style  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
   r_style->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Header' ).


Cell Output Representation:

 

Header

 

 

c) Setting Cell color with style 'Header'.

  • We can set the interior color of a cell.
  • We have options of Pattern for the color as Solid,stripes etc.
  • In below example we use SILVER color code.
  • And color 'Pattern' is 'Solid'. 


Example:

r_format  = l_document->create_simple_element( name = 'Interior' parent = r_style  ).
r_format->set_attribute_ns( name = 'Color'   prefix = 'ss'  value = '#C0C0C0' ).
r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss'  value = 'Solid' ).


Cell Output Representation:

Exterior colour.PNG

Solid Sliver Cell

Horzstripes cell.PNG

Horizontal Stripes Cell




d) Setting Font style of 'Header'


  • We can set the font size by using IDs Bold,Italic and Underline.
  • If value = '1' then the font style will be applied.
  • If value = '0' then the font style will not be applied.
  • Text color also can be specified with ID = 'Color' and value = 'HTML color code'.
  • In our below example we have Color code for 'RED'.


Example:

r_format  = l_document->create_simple_element( name = 'Font'  parent = r_style  ).
r_format->set_attribute_ns( name = 'Color'   prefix = 'ss'  value = '#FA0606' ).
r_format->set_attribute_ns( name = 'Italic'  prefix = 'ss'  value = '1' ).


Cell Output Representation:

Interior colour.PNG

    Italic Red Text

Bold font.PNG

    Bold Red text



d) Alignment settings of 'Header'.


  • Horizontal and Vertical alignments of text can be set.
  • Here in below example we are setting horizontal alignment as 'Center'.
  • And vertical alignment is set as 'Center'.
  • 'Wrap Text' property is active as value = '1'.

 

Example:

r_format  = l_document->create_simple_element( name = 'Alignment'  parent = r_style  ).
r_format->set_attribute_ns( name = 'horizontal'  prefix = 'ss'  value = 'Center' ).

r_format->set_attribute_ns( name = 'Vertical'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'WrapText'  prefix = 'ss'  value = '1' ).


Cell Output Representation:



VH Center alignment.PNG

vertical =  'Center' and horizontal = 'Center'


VH right alignment.PNG

vertical =  'Center' and horizontal = 'Right'


 



















Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>