![](/img/trans.png)
[英]How do I open and populate a .XML file in a sheet in Excel using VB.Net?
[英]How to create excel sheet from xml file using vb.net
第一次這樣做,就像我在標題中說的那樣,我想在excel中導入xml文件。 我有一個pdf表單,其中包含6行單選按鈕和1個文本字段(注釋),提交時給了我這個xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<form1
><Table1
><HeaderRow xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/><Row1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/><Row2 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/><Row3 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/><Row4 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/><Row5 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/></Table1
><Table2
><Row1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/></Table2
><Table3
><Row1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/><Row1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"
/></Table3
><QualityWork
>1</QualityWork
><Ontime
>2</Ontime
><QualityReport
>3</QualityReport
><Needs
>4</Needs
><Comm
>5</Comm
><Global
>6</Global
><Comments
>This is a comment</Comments
></form1
>
我正在嘗試創建一個vb.net程序,該程序將使用xml文件中的數據創建一個Excel工作表。
我嘗試使用此vb.net代碼
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filePath = create_xml.Text()
Dim m_xmld As XmlDocument
'Create the XML Reader
m_xmld = New XmlDocument()
m_xmld.Load(filePath)
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_nodelist = m_xmld.SelectNodes("/form1")
Dim total_untokenized = ""
For Each m_node In m_nodelist
total_untokenized = m_node.ChildNodes.Item(0).InnerText
comments = m_node.ChildNodes.Item(1).InnerText
Next
Dim total_tokenized As Integer
total_tokenized = 0
For i = 1 To 6
total_tokenized = total_tokenized + Strings.Mid(total_untokenized, i, 1)
Next
Dim total_col = 7
Dim MyArrayList = New ArrayList(total_col)
MyArrayList.Add(comments)
For i = 1 To 6
MyArrayList.Add(Strings.Mid(total_untokenized, i, 1))
Next
MyArrayList.Add(total_tokenized)
Dim MyArrayList2 = New ArrayList(total_col)
MyArrayList2.Add("QualityWork")
MyArrayList2.Add("Ontime")
MyArrayList2.Add("QualityReport")
MyArrayList2.Add("Needs")
MyArrayList2.Add("Comm")
MyArrayList2.Add("Global")
MyArrayList2.Add("Comments")
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
For col = 0 To total_col - 1
oSheet.Cells(1, col + 1) = MyArrayList2.Item(col)
Next
oSheet.Rows("1:1").Font.Bold = True
For col = 0 To total_col - 1
oSheet.Cells(2, col + 1) = MyArrayList.Item(col)
Next
End Sub
但是這個m_nodelist
似乎是空的,我總是在這一行出現錯誤total_tokenized = total_tokenized + Strings.Mid(total_untokenized, i, 1)
EDIT1:我已經在下面發布了修復方法
這是這樣做的一種方式...
Dim m_xmld As XmlDocument
'Create the XML Reader
m_xmld = New XmlDocument()
m_xmld.Load(filePath)
Dim QualityWork = m_xmld.GetElementsByTagName("QualityWork").Item(0).InnerText
Dim Ontime = m_xmld.GetElementsByTagName("Ontime").Item(0).InnerText
Dim QualityReport = m_xmld.GetElementsByTagName("QualityReport").Item(0).InnerText
Dim Needs = m_xmld.GetElementsByTagName("Needs").Item(0).InnerText
Dim Comm = m_xmld.GetElementsByTagName("Comm").Item(0).InnerText
Dim Glob = m_xmld.GetElementsByTagName("Global").Item(0).InnerText
Dim Comments = m_xmld.GetElementsByTagName("Comments").Item(0).InnerText
我可以通過這樣更改vb.net來修復它。 通過查看其他答案,我知道必須有更簡單的方法來執行此操作,但是由於我是初學者,所以我不想從頭開始重新啟動並陷入其他問題。 我能夠修改已經擁有的代碼。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If create_xml.Text() = "" Then
MsgBox("You have not specified an XML file!")
Exit Sub
End If
Dim filePath = create_xml.Text()
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim ds As New DataSet
Dim xmlFile As XmlReader
Dim i, j As Integer
Dim totalCol As Integer = 7
Dim MyArrayList2 = New ArrayList(totalCol)
MyArrayList2.Add("QualityWork")
MyArrayList2.Add("Ontime")
MyArrayList2.Add("QualityReport")
MyArrayList2.Add("Needs")
MyArrayList2.Add("Comm")
MyArrayList2.Add("Global")
MyArrayList2.Add("Comments")
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xmlFile = XmlReader.Create(filePath, New XmlReaderSettings())
ds.ReadXml(xmlFile)
For i = 0 To MyArrayList2.Count - 1
xlWorkSheet.Cells(1, i + 1) = MyArrayList2.Item(i)
Next
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 1 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Cells(2, j) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
' Fix first row
xlWorkSheet.Activate()
xlWorkSheet.Application.ActiveWindow.SplitRow = 1
xlWorkSheet.Application.ActiveWindow.FreezePanes = True
' Now apply autofilter
Dim firstRow As Excel.Range = xlWorkSheet.Rows(1)
firstRow.AutoFilter(1,
Type.Missing,
Excel.XlAutoFilterOperator.xlAnd,
Type.Missing,
True)
xlWorkSheet.SaveAs("F:\xml2excel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.