VBA读excel写xml

VBA

◆写文件
Dim sFile As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

sFile.WriteLine (String(8, " ") & "<Request>")

sFile.Close
Set sFile = Nothing
Set fso = Nothing

◆选择文件夹
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folder = .SelectedItems(1)

End If
End With
MsgBox folder & "/TaskInfo.xml"


◆循环/IF
Do While Cells(rowIndex, 2).Value <> ""
For compCol = 8 To 17 Step 1

If idStr < 10 Then
sFile.WriteLine (String(12, " ") & "<Complete id=""0" & idStr & """ />")
Else
sFile.WriteLine (String(12, " ") & "<Complete id=""" & idStr & """ />")
End If

Next compCol

Loop


◆完整


Sub CreateFile()

Dim sFile As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

folder = "D:"

With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folder = .SelectedItems(1)

End If
End With
MsgBox folder & "/TaskInfo.xml"

Set sFile = fso.CreateTextFile(folder & "/TaskInfo.xml", True)

sFile.WriteLine ("<?xml version=""1.0"" encoding=""UTF-8""?>")
sFile.WriteLine ("<Tasks>")

rowIndex = 4

Do While Cells(rowIndex, 2).Value <> ""
' タスクID
taskId = Cells(rowIndex, 2).Value

' 家電対象
obj = Cells(rowIndex, 4).Value

sFile.WriteLine (String(4, " ") & "<Task id=""" & taskId & """ obj=""" & obj & """>")

sFile.WriteLine (String(8, " ") & "<Request>")


For compCol = 8 To 17 Step 1
' 補完ID
compId = Cells(rowIndex, compCol).Value

If compId <> "" Then

idStr = compCol - 7
If idStr < 10 Then
sFile.WriteLine (String(12, " ") & "<Complete id=""0" & idStr & """ />")
Else
sFile.WriteLine (String(12, " ") & "<Complete id=""" & idStr & """ />")
End If

End If

Next compCol


sFile.WriteLine (String(8, " ") & "</Request>")

' URL
URL = Cells(rowIndex, 5).Value

' Path
Path = Cells(rowIndex, 6).Value

' Method
method = Cells(rowIndex, 7).Value

sFile.WriteLine (String(8, " ") & "<Api url=""" & URL & """ path=""" & Path & """ method=""" & method & """ >")

sFile.WriteLine (String(12, " ") & "<Input>")

' 入力定義
inputText = Cells(rowIndex, 18).Value

sFile.WriteLine (inputText)

sFile.WriteLine (String(12, " ") & "</Input>")

sFile.WriteLine (String(8, " ") & "</Api>")

sFile.WriteLine (String(8, " ") & "<Response>")

' 出力応答文種別
outputStatus = Cells(rowIndex, 19).Value

sFile.WriteLine (outputStatus)

sFile.WriteLine (String(8, " ") & "</Response>")

sFile.WriteLine (String(4, " ") & "</Task>")

sFile.WriteLine

rowIndex = rowIndex + 1

Loop


sFile.WriteLine ("</Tasks>")
sFile.Close
Set sFile = Nothing
Set fso = Nothing
End Sub


Sub DeleteFile()

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile ("D:/TestFile.txt")
End Sub

 

转载于:https://www.cnblogs.com/xuemanjiangnan/p/8378744.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值