假设您在 ALV Grid 中显示数据,并且在您写帖子时已准备好会话。然后下面的代码会将数据从 SAP 复制到 excel 中。您必须根据需要调整代码
Dim wks As Worksheet
Set wks = " your worksheet here ..."
Dim Table As Object
Dim cols As Long
Dim rows As Long
Dim i As Long, j As Long
Set Table = Session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")
rows = Table.RowCount - 1
cols = Table.ColumnCount - 1
Dim columns As Object
Set columns = Table.ColumnOrder
Dim arrCol() As Variant
ReDim arrCol(cols)
For j = 0 To cols
arrCol(j) = (CStr(columns(j)))
Next
With wks
.Range(.Cells(1, 1), .Cells(1, cols + 1)).Value = arrCol()
End With
For i = 0 To rows
For j = 0 To cols
arrCol(j) = Table.GetCellValue(i, CStr(columns(j)))
Next
With wks
.Range(.Cells(i + 2, 1), .Cells(i + 2, cols + 1)).Value = arrCol()
End With
If i Mod 10 = 0 Then
Table.SetCurrentCell i, CStr(columns(0))
DoEvents
End If
Next
End Sub
如果不使用 griv 视图控件,上述代码将失败。“会话”必须是指向 FBL3N 且网格视图打开的有效 SAP Guisession。在我上面提供的链接中,您会看到这样做很热。