2 weeks ago
Hello Experts,
I have a scenario of getting an XLSX file from an SFTP (sender) using SAP CPI and sending to SAP S4. Here I need to get the sheet name before conversion to xml but I don't know how to do this reading to an Excel file.
Kindly share the groovy script code to read this before using converter class to convert to xml.
Hello,
Try the below groovy code that reads an Excel file and retrieves the sheet names before converting it to XML. This code uses Apache POI library, which is commonly used to work with Excel files in Java and Groovy.
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.ss.usermodel.WorkbookFactory
import java.io.InputStream
def getSheetNamesFromExcel(InputStream inputStream) {
// Create a workbook instance from the Excel file
def workbook = WorkbookFactory.create(inputStream)
// Get the sheet names
def sheetNames = []
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
sheetNames.add(workbook.getSheetName(i))
}
workbook.close()
return sheetNames
}
// Example usage: Assume 'body' contains the Excel file input stream
def excelInputStream = message.getBody(InputStream)
def sheetNames = getSheetNamesFromExcel(excelInputStream)
// Log the sheet names
sheetNames.each { sheetName ->
messageLog.addAttachmentAsString("Sheet Name", sheetName, "text/plain")
}
Make sure to import Apache POI libraries (e.g., poi-ooxml, poi).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
67 | |
11 | |
10 | |
10 | |
9 | |
9 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.