This tutorial shows how to read data from an excel worksheet and
display the result on the SAP
screen.
We have a duty roster that maps malicious codes to the responsible employee.
We want to determine an employee according to the code that has been typed in.
We add a new inputfield, a pushbutton and an outputfield to
the SAP screen:
GuiXT
inputfield (20,1) "Read user to code from excel-file" (20,35) _
size="25" name="code" default="TSC99" -upperCase
inputfield (20,70) size="15" name="usertocode" -upperCase -noLabel
pushbutton (20,62) "ok" process="read_excel_va03.txt"
In the
InputScript "read_excel_va03.txt" we call a VB.NET function and pass
to it the
name of the file and the code as a parameter:
GuiXT
callvb utilities.exceldotnet.TestReadCellsExcel _
"D:\Excel\duty_rota.xlsx" "&V[code]"
return
Imports Microsoft.Office.Interop
Imports guinet
Public Class exceldotnet
Public Function TestReadCellsExcel(ByVal filename As String,
ByVal code As String) As String
Dim returncode = "1"
Dim oXL As New Excel.Application
Dim g As New guixt
Try
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = CreateObject("Excel.Application")
oXL.Visible = False
oWB = oXL.Workbooks.Open(filename)
oSheet = oWB.ActiveSheet
Dim range As Excel.Range = oSheet.UsedRange
Dim c As Integer = 1
For Each r In range.Rows
If oSheet.Cells(c, 1).Value.ToString = code Then
g.SetVariable("usertocode", oSheet.Cells(c, 2).Value.ToString)
returncode = "0"
Exit For
End If
c += 1
Next
Catch
oXL.ActiveWorkbook.Close(False)
oXL.Quit()
Return "Exception"
End Try
oXL.ActiveWorkbook.Close(False)
oXL.Quit()
Return returncode
End Function
End Class