N
nimi
hI all,
I am facing an issuw while converting the xml to an excel.
I am expecting an excel(see below image), i am able to see that if i open
the xml file in the excel manually.
but when i do programatically its not giving the output as i expected.
I refered the thread, but i couldnt solve this
http://social.msdn.microsoft.com/Fo...b1e-67b4-4190-8f59-d1530efa2821?prof=required
Please have a look at my code.
private
void
btnConvert_Click(object
sender, EventArgs e)
{
try
{
LoadXMLFile(txtFile.Text, txtSaveLoctaion.Text);
MessageBox.Show("XML file has been successfully converted to
XML"
);
lblSavedLocation.Text = txtSaveLoctaion.Text;
lnkLocation.Visible = true
;
lblSavedLocation.Visible = true
;
//txtFile.Text=string.Empty;
//txtSaveLoctaion.Text = string.Empty;
}
catch
(Exception ex)
{
MessageBox.Show("XML file convertion to EXCEL failed,
Details:"
+ ex.ToString());
}
}
static
void
LoadXMLFile(string
inFileName, string
outFileName)
{
Excel.Application APexcel = null
;
Excel.Workbook MyBook = null
;
Excel.Worksheet MySheet = null
;
try
{
object
Missing = System.Reflection.Missing.Value;
APexcel = new
Microsoft.Office.Interop.Excel.Application();
APexcel.Visible = false
;
//MyBook = APexcel.Workbooks._OpenXML(inFileName,
XlXmlLoadOption.xlXmlLoadImportToList);
MyBook =
APexcel.Workbooks._OpenXML(inFileName,XlXmlLoadOption.xlXmlLoadImportToList);
MySheet = (Excel.Worksheet)MyBook.ActiveSheet;
MyBook.SaveAs(outFileName,
Excel.XlFileFormat.xlOpenXMLWorkbook, null
, null
, false
, false
, Excel.XlSaveAsAccessMode.xlShared, false
, false
, null
, null
, null
);
MyBook.Close(true
, Missing, Missing);
APexcel.Quit();
releaseObject(MySheet);
releaseObject(MyBook);
releaseObject(APexcel);
}
catch
(Exception ex)
{
throw
new
Exception(ex.ToString());
}
}
private
static
void
releaseObject(object
obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null
;
}
catch
(Exception ex)
{
obj = null
;
MessageBox.Show("Exception Occured while releasing object "
+ ex.ToString());
}
finally
{
GC.Collect();
}
}
I expect this output.
A B C
1 ID_PRODUCT DESC PRICE
2 AS2F2 Hannibal by Tomas Harris $30.99
3 FGH83 Harry Potter and the Deathly Hallows by J. K. Rowling. $14.99
4 IW472 DUNE by Frank Herbert $16.99
my output(pasted from a different thread)
A B C D
1 /LIST
2 /BOOK/DESC /BOOK/ID_PRODUCT /BOOK/PRICE /BOOK/PRICE/#agg
3 Hannibal by Tomas Harris AS2F2 $30.99 $30.99
4 Harry Potter and the Deathly Hallows by J. K. Rowling. FGH83 $14.99
$14.99
5 DUNE by Frank Herbert IW472 $16.99 $16.99
I am facing an issuw while converting the xml to an excel.
I am expecting an excel(see below image), i am able to see that if i open
the xml file in the excel manually.
but when i do programatically its not giving the output as i expected.
I refered the thread, but i couldnt solve this
http://social.msdn.microsoft.com/Fo...b1e-67b4-4190-8f59-d1530efa2821?prof=required
Please have a look at my code.
private
void
btnConvert_Click(object
sender, EventArgs e)
{
try
{
LoadXMLFile(txtFile.Text, txtSaveLoctaion.Text);
MessageBox.Show("XML file has been successfully converted to
XML"
);
lblSavedLocation.Text = txtSaveLoctaion.Text;
lnkLocation.Visible = true
;
lblSavedLocation.Visible = true
;
//txtFile.Text=string.Empty;
//txtSaveLoctaion.Text = string.Empty;
}
catch
(Exception ex)
{
MessageBox.Show("XML file convertion to EXCEL failed,
Details:"
+ ex.ToString());
}
}
static
void
LoadXMLFile(string
inFileName, string
outFileName)
{
Excel.Application APexcel = null
;
Excel.Workbook MyBook = null
;
Excel.Worksheet MySheet = null
;
try
{
object
Missing = System.Reflection.Missing.Value;
APexcel = new
Microsoft.Office.Interop.Excel.Application();
APexcel.Visible = false
;
//MyBook = APexcel.Workbooks._OpenXML(inFileName,
XlXmlLoadOption.xlXmlLoadImportToList);
MyBook =
APexcel.Workbooks._OpenXML(inFileName,XlXmlLoadOption.xlXmlLoadImportToList);
MySheet = (Excel.Worksheet)MyBook.ActiveSheet;
MyBook.SaveAs(outFileName,
Excel.XlFileFormat.xlOpenXMLWorkbook, null
, null
, false
, false
, Excel.XlSaveAsAccessMode.xlShared, false
, false
, null
, null
, null
);
MyBook.Close(true
, Missing, Missing);
APexcel.Quit();
releaseObject(MySheet);
releaseObject(MyBook);
releaseObject(APexcel);
}
catch
(Exception ex)
{
throw
new
Exception(ex.ToString());
}
}
private
static
void
releaseObject(object
obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null
;
}
catch
(Exception ex)
{
obj = null
;
MessageBox.Show("Exception Occured while releasing object "
+ ex.ToString());
}
finally
{
GC.Collect();
}
}
I expect this output.
A B C
1 ID_PRODUCT DESC PRICE
2 AS2F2 Hannibal by Tomas Harris $30.99
3 FGH83 Harry Potter and the Deathly Hallows by J. K. Rowling. $14.99
4 IW472 DUNE by Frank Herbert $16.99
my output(pasted from a different thread)
A B C D
1 /LIST
2 /BOOK/DESC /BOOK/ID_PRODUCT /BOOK/PRICE /BOOK/PRICE/#agg
3 Hannibal by Tomas Harris AS2F2 $30.99 $30.99
4 Harry Potter and the Deathly Hallows by J. K. Rowling. FGH83 $14.99
$14.99
5 DUNE by Frank Herbert IW472 $16.99 $16.99