- This topic explains about, how the create an Excel file from XML file using VB programming.
- How to add Excel Object model to your application: Select Project/Add Reference. Click the COM tab and select “Microsoft Excel 12.0 Object Library” (or whatever version you have on your system).
- Tools needed: Button1 (Button1).
XML (Extensible Markup Language) – Create an Excel file from XML file using VB
- Extensible Markup Language (XML) is a markup language created to structure, store, and transport data by defining a set of rules for encoding documents in a format that is both human-readable and machine-readable.
- The design goals of XML emphasize simplicity, generality, and usability over the Internet.
- XML is a textual data format with strong support via Unicode for the languages of the world.
- XML is developed by World Wide Web Consortium.
- Hundreds of XML-based languages have been developed, including RSS, Atom, SOAP, and XHTML.
Microsoft Excel – Create an Excel file from XML file using VB
- Microsoft Excel is a spreadsheet application developed by Microsoft.
- Microsoft Excel features calculation, graphing tools, pivot tables, and a macro programming language called Visual Basic for Applications.
Create an Excel file from XML file using VB – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
Imports System.Xml Imports System.Data Imports ExcelDoc = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ExcelApp As ExcelDoc.Application Dim ExcelWorkBook As ExcelDoc.Workbook Dim ExcelWorkSheet As ExcelDoc.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim ds As New DataSet Dim xmlFile As XmlReader Dim i, j As Integer ExcelApp = New ExcelDoc.ApplicationClass ExcelWorkBook = ExcelApp.Workbooks.Add(misValue) ExcelWorkSheet = ExcelWorkBook.Sheets("sheet1") 'Location where the XML file located xmlFile = XmlReader.Create("C:/Product.xml", New XmlReaderSettings()) ds.ReadXml(xmlFile) For i = 0 To ds.Tables(0).Rows.Count - 1 For j = 0 To ds.Tables(0).Columns.Count - 1 ExcelWorkSheet.Cells(i + 1, j + 1) = _ ds.Tables(0).Rows(i).Item(j) Next Next 'Excel page will be saved in "myDocuments Folder" ExcelWorkSheet.SaveAs("xmlNewexcel.xlsx") ExcelWorkBook.Close() ExcelApp.Quit() releaseObject(ExcelApp) releaseObject(ExcelWorkBook) releaseObject(ExcelWorkSheet) 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 |
Create an Excel file from XML file using VB






