PC Review


Reply
Thread Tools Rate Thread

How to convert and xml to excel ?

 
 
nimi
Guest
Posts: n/a
 
      31st Jul 2009
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/For...?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
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      31st Jul 2009
Can you record a macro will importing the file manually. Try modifing your
code to match the recorded macro. If yo still have problems poost the
recorded macro.

"nimi" wrote:

> 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/For...?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

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[VBA] How to convert an Excel woorkbook completely from Excel 2003to Excel 2007? Peter Müller Microsoft Excel Programming 3 30th Jan 2011 07:03 PM
excel 2007 convert one sheet into multiples sheets (for excel 2003Compatibility) recif20002002@yahoo.fr Microsoft Excel Programming 2 17th Jul 2009 10:33 AM
Convert Excel workbook to Excel - Test Paramaters and data anna_717717 Microsoft Access Database Table Design 0 6th May 2008 07:11 PM
How do I convert a Macintosh version 1 excel file to excel 2003 =?Utf-8?B?cm95Njc4?= Microsoft Excel Misc 1 28th Mar 2005 01:18 AM
How to convert excel Docs to pocket excel on my mobile device =?Utf-8?B?TmV3IFVzZXIgYWdhaW4=?= Microsoft Excel New Users 1 8th Mar 2005 07:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:37 AM.