ASKER
ASKER
ASKER
ASKER
--
Private Function XmlToExcel(ByVal InputXmlFile As String, ByVal OutputExcelFile As String)
Dim excelFile As New Excel.Application
excelFile.Workbooks.Open(InputXmlFile)
excelFile.Workbooks.Item(1).SaveAs(OutputExcelFile,Excel.XlFileFormat.xlWorkbookDefault)
excelFile.Workbooks.Close()
SetAttr(InputXmlFile, vbNormal)
excelFile.Quit()
excelFile = Nothing
Dim f2 As New System.IO.FileInfo(OutputExcelFile)
SetAttr(XLSFile, FileAttribute.Normal)
Return True
End Function
--
Imports System.Xml
Imports System.Data
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.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.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xmlFile = XmlReader.Create("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
xlWorkSheet.Cells(i + 1, j + 1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
xlWorkSheet.SaveAs("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
End Class
Private Function XmlToExcel(ByVal XMLFile As String, ByVal XLSXFile As String)
Dim ex As New Excel.Application
Dim f As New System.IO.FileInfo(XLSFile)
If f.Exists Then
SetAttr(XLSXFile, vbNormal)
f.Delete()
End If
Dim fxml As New System.IO.FileInfo(XMLFile)
If fxml.Exists Then
ex.Workbooks.Open(XMLFile)
ex.Workbooks.Item(1).CheckCompatibility = True
ex.DisplayAlerts = False
ex.Workbooks.Item(1).SaveAs(XLSXFile,Excel.XlFileFormat.xlWorkbookDefault)
ex.DisplayAlerts = False
ex.Workbooks.Close()
SetAttr(XMLFile, vbNormal)
fxml.Delete()
Else
MessageBox.Show("XML File does not exists")
End If
Dim f2 As New System.IO.FileInfo(XLSXFile)
If f2.Exists Then
SetAttr(XLSFile, FileAttribute.Normal)
End If
ex.Quit()
ex = Nothing
MessageBox.Show("Exported Successfully to Excel")
Return True
End Function
--Code in Export Button.
Private Sub btnExport_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnExport.ItemClick
Dim fn As String = ""
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.InitialDirectory = "I:\"
saveFileDialog1.Title = "Save as Excel Files"
saveFileDialog1.CheckPathExists = True
saveFileDialog1.DefaultExt = "xml"
saveFileDialog1.Filter = "Excel Files (*.xls)|"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
saveFileDialog1.AddExtension = True
If (saveFileDialog1.ShowDialog() = DialogResult.OK) Then
fn = saveFileDialog1.FileName
End If
Dim dt As DataTable = dv.ToTable
ExportToXml(fn, dt)
Dim fnnew As String = fn.Substring(0, fn.Length - 3)
fnnew = fnnew & "xlsx"
XmlToExcel(fn, fnnew)
End Sub
ASKER
ASKER
ASKER
--
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
Dim Xmlfile = 'C:\XmlFileName.xml'
Dim ExcelFile = 'C:\ExcelFile.xls'
XmlToExcel(Xmlfile, ExcelFile)
End Sub
Private Function XmlToExcel(ByVal XMLFile As String, ByVal XLSXFile As String)
Dim ex As New Application
Dim f As New System.IO.FileInfo(XLSFile) ** Error
If f.Exists Then
SetAttr(XLSXFile, vbNormal)
f.Delete()
End If
Dim fxml As New System.IO.FileInfo(XMLFile)
If fxml.Exists Then
ex.Workbooks.Open(XMLFile)
ex.Workbooks.Item(1).CheckCompatibility = True
ex.DisplayAlerts = False
ex.Workbooks.Item(1).SaveAs(XLSXFile, Excel.XlFileFormat.xlWorkbookDefault) * Error (Excel not defined)
ex.DisplayAlerts = False
ex.Workbooks.Close()
SetAttr(XMLFile, vbNormal)
fxml.Delete()
Else
MessageBox.Show("XML File does not exists")
End If
Dim f2 As New System.IO.FileInfo(XLSXFile)
If f2.Exists Then
SetAttr(XLSFile, FileAttribute.Normal)
End If
ex.Quit()
ex = Nothing
MessageBox.Show("Exported Successfully to Excel")
Return True
End Function
--
ASKER
ASKER
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
Dim Xmlfile AS String = 'C:\XmlFileName.xml'
Dim ExcelFile AS String = 'C:\ExcelFile.xls'
XmlToExcel(Xmlfile, ExcelFile)
End Sub
Private Function XmlToExcel(ByVal XMLFile As String, ByVal XLSXFile As String)
Dim ex As New Application
Dim f As New System.IO.FileInfo(XLSFile) ** Error
If f.Exists Then
SetAttr(XLSXFile, vbNormal)
f.Delete()
End If
Dim fxml As New System.IO.FileInfo(XMLFile)
If fxml.Exists Then
ex.Workbooks.Open(XMLFile)
ex.Workbooks.Item(1).CheckCompatibility = True
ex.DisplayAlerts = False
ex.Workbooks.Item(1).SaveAs(XLSXFile, Excel.XlFileFormat.xlWorkbookDefault) * Error (Excel not defined)
ex.DisplayAlerts = False
ex.Workbooks.Close()
SetAttr(XMLFile, vbNormal)
fxml.Delete()
Else
MessageBox.Show("XML File does not exists")
End If
Dim f2 As New System.IO.FileInfo(XLSXFile)
If f2.Exists Then
SetAttr(XLSFile, FileAttribute.Normal)
End If
ex.Quit()
ex = Nothing
MessageBox.Show("Exported Successfully to Excel")
Return True
End Function
ASKER
xlApp = New Excel.ApplicationClass
To this
xlApp = New Excel.Application
It should work with no issues, I tested on my system.
ASKER
ASKER
ASKER
So the computer you are developing on is a 32 bit machine, correct?
Visual Studio IDE is set for the Solution Platform as x86 and is NOT set to "Any CPU", correct?
ASKER
ASKER
So the computer you are developing on is a 32 bit machine, correct?You answer should have been No and not Yes as you stated.
ASKER
ASKER
ASKER
ASKER
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
Include System.XML as a Namespace.
Open in new window