简体   繁体   English

如何使用vb.net从xml文件创建Excel工作表

[英]How to create excel sheet from xml file using vb.net

First time doing this, like I said in the title I want to import an xml file in excel. 第一次这样做,就像我在标题中说的那样,我想在excel中导入xml文件。 I have a pdf form containing 6 rows of radiobuttons and 1 text field(comments) on submit it gives me this xml file: 我有一个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
>

I'm trying to create a vb.net program that will create an excel sheet with the data from the xml file. 我正在尝试创建一个vb.net程序,该程序将使用xml文件中的数据创建一个Excel工作表。

I tried using this vb.net code 我尝试使用此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 

But this m_nodelist seems to stay empty, I always get an error at this line total_tokenized = total_tokenized + Strings.Mid(total_untokenized, i, 1) 但是这个m_nodelist似乎是空的,我总是在这一行出现错误total_tokenized = total_tokenized + Strings.Mid(total_untokenized, i, 1)

EDIT1: I've post how I fixed it below EDIT1:我已经在下面发布了修复方法

This is one way of doing it... 这是这样做的一种方式...

   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

Why wouldn't you use this simple method: 您为什么不使用这种简单的方法:

 Workbooks.OpenXML

see here 这里

I was able to fix it by changing my vb.net like this. 我可以通过这样更改vb.net来修复它。 By looking at the other answers I know there must be easier ways to do this but since I'm a beginner I did not want to restart from the beginning and get stuck somewhere else. 通过查看其他答案,我知道必须有更简单的方法来执行此操作,但是由于我是初学者,所以我不想从头开始重新启动并陷入其他问题。 I was able to modify the code I already had. 我能够修改已经拥有的代码。

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.

相关问题 如何使用VB.Net在Excel的工作表中打开和填充.XML文件? - How do I open and populate a .XML file in a sheet in Excel using VB.Net? 如何从 vb.net 中的数据库创建 XML 文件 - how to create XML file from database in vb.net 如何使用 VB.net 代码从 XML 文件创建 XSD 文件 - How to create a XSD file from a XML file using VB.net code 使用XML Literals + VB.NET从Excel导出为XML - Export from Excel into XML using XML Literals + VB.NET 使用 vb.net 中的 XML 从 datagridview 导出到 excel - Export from datagridview to excel using XML in vb.net 如何使用vb.net检索具有合并单元格的复杂Excel文件并另存为xml文件? - How to retrieve complex excel file with merged cells and save as xml file using vb.net? VB.Net:使用控制台将Excel中的数据自动保存为XML文件 - VB.Net: Auto-save data from excel as XML file using console 如何使用VB.NET中的openxml方法从具有所有设置(格式和对齐方式)的现有XML文件数据创建Word文件? - How to create a word file from an existing XML file data with all the settings(format & alignments) using openxml method in VB.NET? 如何在 VB.NET 中创建和填充 xml 文件? - How to create and fill an xml file in VB.NET? 如何在VB.Net中的xml文件中创建多个条目? - How to create multiple entries in an xml file in VB.Net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM