写个导出SAP的md04表内容到EXCEl的脚本程序,用VBA编写
时间: 2024-06-05 10:06:13 浏览: 228
2019水晶报表测试过可以用
Sub ExportMD04TableToExcel()
'定义变量
Dim objSAP As Object
Dim objMD04 As Object
Dim objWorkbook As Workbook
Dim objWorksheet As Worksheet
Dim objRange As Range
Dim i As Integer
'创建SAP对象
Set objSAP = CreateObject("SAP.Functions")
'连接SAP
objSAP.Connection.System = "SAP系统名称"
objSAP.Connection.Client = "SAP客户端编号"
objSAP.Connection.User = "SAP用户名"
objSAP.Connection.Password = "SAP密码"
objSAP.Connection.Language = "EN"
objSAP.Connection.ApplicationServer = "SAP应用服务器名称"
objSAP.Connection.SystemNumber = "SAP系统编号"
objSAP.Connection.UseSAPLogonIni = False
objSAP.Connection.UseSAPGui = True
objSAP.Connection.Connect
'创建MD04对象
Set objMD04 = objSAP.Add("RFC_READ_TABLE")
'设置参数
objMD04.Exports("QUERY_TABLE") = "MD04"
objMD04.Exports("ROWCOUNT") = 0
'执行函数
objMD04.Call
'创建Excel对象
Set objWorkbook = Application.Workbooks.Add
Set objWorksheet = objWorkbook.Sheets(1)
'写入表头
objWorksheet.Cells(1, 1).Value = "Material"
objWorksheet.Cells(1, 2).Value = "Plant"
objWorksheet.Cells(1, 3).Value = "MRP Element"
objWorksheet.Cells(1, 4).Value = "Reqmt Date"
objWorksheet.Cells(1, 5).Value = "Open Qty"
'获取数据
For i = 1 To objMD04.Tables("DATA").RowCount
'写入数据
objWorksheet.Cells(i + 1, 1).Value = objMD04.Tables("DATA").Cell(i, "MATNR")
objWorksheet.Cells(i + 1, 2).Value = objMD04.Tables("DATA").Cell(i, "WERKS")
objWorksheet.Cells(i + 1, 3).Value = objMD04.Tables("DATA").Cell(i, "DISPO")
objWorksheet.Cells(i + 1, 4).Value = objMD04.Tables("DATA").Cell(i, "BDTER")
objWorksheet.Cells(i + 1, 5).Value = objMD04.Tables("DATA").Cell(i, "OPEN_REQ_QTY")
Next i
'自动调整列宽
objWorksheet.Columns.AutoFit
'保存Excel文件
objWorkbook.SaveAs "MD04 Table.xlsx"
'释放对象
Set objRange = Nothing
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objMD04 = Nothing
Set objSAP = Nothing
End Sub
阅读全文