cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Converting Excel File to XML format in SAP CPI using JAR files and groovy script

RoohiJain
Discoverer
4,583

Hello Sapians,

Sometimes we encountered an issue where the third party is sending the file in Excel format and we have to read that file, convert to XML and process in SAP CPI. As we are familiar we don't have any direct function available as of now to convert excel file to XML. To overcome this issue, below are the step by step process to implement in CPI to read and convert Excel file.

This converter is based on Apache POI 3.17 library. In order for conversion the following JAR files need to be referenced/imported into the project.

 

The library files can be downloaded from above mentioned links or Apache website.

Sample Excel file in .xsls format.

Roohi2_1-1743748913964.png

To import the JAR files, Go to 

<<IFlow>> --> Click on blank panel (you will get Integration Flow) --> References --> Add --> Archive

Roohi2_3-1743749540523.png

Select the required Jar files from your system and click on Add.

Import all the above mentioned JAR files to your Iflow.

Roohi2_5-1743749801640.png

Sender Adapter is configured with HTTPS adapter.

Roohi2_2-1743749307459.png

Add the content modifier, as we have to configure some parameters in exchange properties.

Roohi2_16-1743751820619.png

 

 

Content Modifier properties :

Roohi2_9-1743750019467.png

1. converterClass : com.equalize.converter.core.Excel2XMLConverter 

2. sheetName : << Specify the sheet Name that need to be process from your Excel File>>

3. processFieldNames : <<The header fields  will be processed from the file>>

4. documentName : << Specify the name in which the XML header tag to be generated>>

5. documentNamespace : << Specify the name in which the XML namespace to be generated>>

 

To understand more about parameters that can be added, you can check below parameter reference.

Below is a list of the parameters for configuration Excel to XML conversion. Certain parameters will automatically inherit the default values if it is not configured.

Parameter Name Allowed values Default value Remarks

converterClassPI - com.equalize.xpi.af.modules.excel.Excel2XMLTransformer
CPI - com.equalize.converter.core.Excel2XMLConverter
 Required field. Determines conversion class.
sheetName  The name of the active Excel sheet to extract. Either sheetName or sheetIndex must be populated.
sheetIndexInteger values beginning from 0 The index of the active Excel sheet to extract (starts from 0). Either sheetName or sheetIndex must be populated.
skipEmptyRowsY, NYEmpty rows to be skipped or not
rowOffsetInteger values beginning from 10Starting row to begin extracting content from (i.e. 0 = start from first row, 1 = start from second row). If processFieldNames = 'fromFile' and rowOffset = 0, first line will be automatically be headerRow+1
headerRowInteger values beginning from 10Available only when processFieldNames = 'fromFile'. Determines which row to retrieve header column names from (0-based so 0 is first row, 1 is second row)
columnOffsetInteger values beginning from 10Starting column to begin extracting content from (i.e. 0 = start from first column, 1 = start from second column)
processFieldNamesfromFile, fromConfiguration, notAvailable Required field. Determines the naming of each column of the rows, and the number of columns to extract:

    • fromFile = Column names and number of columns are determined from header line of the sheet

    • fromConfiguration = Column names and number of columns are determined from parameter fieldNames

    • notAvailable = Column names will be set as ColumnX, where X = 1,2,3,4. Number of columns will be determined from parameter columnCount


fieldNames  Name of columns. Required field when processFieldNames = 'fromConfiguration'
onlyValidCharsInXMLNameY, NNAvailable only when processFieldNames = 'fromFile'. Removes invalid XML characters from column names retrieved from header row of sheet
columnCountInteger values beginning from 1 Number of columns for extraction. Required field when processFieldNames = 'notAvailable'
recordName RecordXML element name for row of record in output
documentName  Required field. Document name of root element of XML output
documentNamespace  Required field. Namespace of root element of XML output
formattingexcel, rawexcelControls how the cell contents are formatted in XML output

    • excel = Cells are displayed the same way as Excel formatting of corresponding cell

    • raw = Raw value of cells are displayed


evaluateFormulasY, NYControls how cell contents with formulas are displayed in XML output

    • YES = Cells are displayed with result of formula evalution

    • NO = Cells are displayed with actual formula


emptyCellOutputsuppress, defaultValuesuppressControls how empty cells are displayed in XML output

    • suppress = Empty cells are not displayed (no corresponding XML tags for empty cells)

    • defaultValue = Empty cells will be displayed with default value


emptyCellDefaultValue <blank>If emptyCellOutput = 'defaultValue', all empty cells will be populated with value in this parameter
indentFactorInteger values beginning from 10Determines the number of indentation spaces for each level in the XML output
debugY, NNDisplays contents in Audit Log of each cell extracted. WARNING: Use this only for debugging in non-productive systems

 

After that add the groovy script to convert the file to XML.

Roohi2_10-1743750845049.png

Groovy Script : 

-----------------------------------------------------------------------------------------------------

import com.sap.gateway.ip.core.customdev.util.Message
import com.equalize.cpi.converter.FormatConversionBean
 
def Message processData(Message message) {
 
def fcb = new FormatConversionBean(message.exchange, message.getProperties())
def output = fcb.convert()
message.setBody(output)
return message
}
 
-----------------------------------------------------------------------------------------------------

Roohi2_11-1743750963674.png

Add content modifier to read the XML generated.

Roohi2_12-1743751059318.png

 

Roohi2_13-1743751153495.png

To test the scenario, we are testing through postman.

Add you file in binary format.

Roohi2_14-1743751405679.png

Roohi2_15-1743751555790.png

This conclude the blog, the conversion of Excel to XML file😊.

In case of any queries, you can comment in the blog. Will try to resolve your concerns.

 

 

 

 

Accepted Solutions (0)

Answers (1)

Answers (1)

RoohiJain
Discoverer
0 Likes

Hi,

You can click on the link and download from the download option.

RoohiJain_0-1744200796017.png

 

If it is not working for you, then you can try to download from other available websites. It is available for free from many sites.