VBA读excel写xml

Posted xuemanjiangnan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于VBA读excel写xml的主要内容,如果未能解决你的问题,请参考以下文章

EXCEL VBA 读AS400数据中文乱码问题

如何在Excel VBA 中读写word文档 步骤

使用 VBA 将 Excel 公式写入单元格

Excel EXCEL中怎样用VBA来写图表

java读取excel文件,按照指定的日期条件读数据,然后导出txt存储并以其日期命令

想在excel里用VBA,点击按钮复制一行数据到另外一行,可是不知道该怎么写