在vb.net中将xml转换为excel时出错 [英] Error while converting xml to excel in vb.net
问题描述
你好,我正在完成我的大学任务,我已经被分配到使用vb.net将xml文件转换为excel文件..我按照我上网的代码但我是在运行时遇到错误,而不是在编译过程中遇到错误..我非常喜欢vb.net ,所以如果any1可以帮助我,我会非常感激..
基本上我需要使用filedialog将xml文件导入内存并将其转换为Excel工作表并制作一个按钮来保存excel工作表..(我尝试使用datagrid方式,但是我的xml由父表继承的多个子表组成..因此无法这样做)
有人能帮帮我吗?我真的很喜欢它,如果我能尽快得到解决方案或任何帮助,我还有3天时间来提交作业!
Hello, I am working on my college assignment and I have been assigned to Convert an xml file into and excel file using vb.net.. I followed a code i got on internet but I am facing an error while running it and not during its compilation .. I am pretty much new to vb.net , so i would be pretty grateful if any1 could help me with it..
Basically I need to import xml file into memory using filedialog and convert it into excel sheet and make a button to save the excel sheet.. ( I tried using the datagrid way, but my xml consists of multiple child tables being inherited by parent tables.. hence failed doing it that way)
Can anybody help me? I would really like it if I could get the solution or any help soon, I have 3 days left to submit my assignment!
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
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
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xmlFile = XmlReader.Create("e:\product.xml", New XmlReaderSettings())
ds.ReadXml(xmlFile, XmlReadMode.IgnoreSchema)
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
xlWorkSheet.SaveAs("ConvertedExcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Excel file created , you can find the file in the Application Directory")
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
End Class
推荐答案
看看这里: Workbooks.OpenXML方法 [ ^ ]。 Excel允许您打开XML文件并 SaveAs [ ^ ] Excel文件 [ ^ ]使用可用格式之一[ ^ ]。
Have a look here: Workbooks.OpenXML method [^]. Excel enables you to open XML file and SaveAs[^] Excel file[^] using one of available format[^].
这篇关于在vb.net中将xml转换为excel时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!