How to transfer Xml data to microsoft excel 2002 by using visual c # .net
Article ID: 307029Last Review: July 14, 2004Revision: 6.1
This Article Was previously distribLished Under Q307029
For a Microsoft Visual Basic .NET VERSION OF This Article, See
307021.
In this task
Summary
• Generate XML from a dataset for use in Excel 2002 or Excel 2003 • Format the xml use a stylesheet • Use code to open the transformed XML • References
On this Page
Summaryreferences
Summary
Excel 2002 introduces functionality for opening files in the Extensible Markup Language (XML) format. An XML file that is well-formed can be opened directly in Excel 2002 or Excel 2003 by using either the user interface or code.
With Visual C # .NET, you can take advantage of Excel's XML functionality to seamlessly transfer data to a workbook to present the data with formatting and an arrangement of your choice. This article demonstrates how to accomplish this task.
Back to the top
Generate XML from a dataset for use in Excel 2002 or Excel 2003this Section Illustrates How To Create A
DataSet Object And Export The Data That Contains To An Xml File By Using T
Writexml method. The Xml File That IS generated can be be opened Directly in Excel. For illustration purposes, the
DataSet Object Is Created from The Microsoft Access Northwind Sample Database by Using The Jet OLEDB Provider. However, Similar Code Works with any
DataSet Object That You Create with Visual C # .NET.
1.Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Select Windows Application from the Visual C # Projects types. Form1 is created by default.2.On the View menu, select Toolbox to display the Toolbox And Add a Button to Form1.3.double-Click Button1.double-Click Button1.4.add The Following Using Directives To The Top of Form1.cs: Using System.Data.OLDB; Using System.xml;
5.add The Following Private Member Variable To The Form1 Class: Private String StrConn = "Provider = Microsoft.jet.Oledb.4.0; Data Source ="
"C: // program files // microsoft office // office10 // Samples //"
"Northwind.mdb;";
Note: you may need to modify the path to northwind.mdb in the connection .6.add The following code to the button1_click handler: // connect to the data source.
OLEDBCONNECTION OBJCONN = New OLEDBCONNECTION (STRCONN);
Try
{
Objconn.open ();
// Fill a Dataset with Records from the customer.
OLEDBCOMMAND OBJCMD = New OLEDBCOMMAND (
"Select Customerid, CompanyName, ContactName,"
"Country, Phone from Customers", Objconn;
OLEDBDataAdapter Objadapter = new oledbdataadapter ();
Objadapter.selectCommand = Objcmd;
DataSet objDataSet = new dataset ();
Objadapter.Fill (ObjDataSet);
// Create the fileestream to write with.
System.IO.FileStream Fs = new system.io.filestream
"C: //customers.xml", system.io.filemode.create;
// Create an XmlTextWriter for the filestream.
System.xml.xmlTextWriter Xtw = new system.xml.xmlTextWriter (
FS, System.Text.Encoding.Unicode;
// add processing instructions to the beginning, of the xml file, one
// of which indeicates a style sheet.xtw.writeprocessinginstruction ("XML", "Version = '1.0');
7.press f5 to build and rain the program1 xml file, the version form1 to end . Female has observed How The Xml Has Been Parsed Into Rows and Column in The New Workbook, Close The File and Quit Excel.
Back to the top
Format the XML Using A Stylesheetthis Step Shows You How To Use A Stylesheet (XSL) To Transform How XML Data IS Formatted and Arranged In an Excel Workbook.
1.using any html editor or a text editor (Such as notepad.exe), save the folowing xsl as c: /customers.xsl:
2.uncomment the folload of code in the button1_click handler: xtw.writeprocessinginstruction ("XML-Stylesheet",
"Type = 'text / xsl' href = 'customers.xsl');
This line of code writes a processing instruction to the XML file that Excel uses to locate the stylesheet (Customers.xsl) .3.Press F5 to build and run the program.4.Click Button1 to create the XML file, then close Form1 to end the program.5.Start Excel 2002 or Excel 2003 and open the C: /Customers.xml output file.6.Because Excel sees the processing instruction for the stylesheet in the XML, you receive a dialog box prompt when you open the file . In the Import XML dialog box, select Open the file with the following stylesheet applied. In the list, select Customers.xsl and click OK. Note that the XML data is formatted and that the columns have been arranged according to the stylesheet.7 .Close the file and quit excel.back to the top
Use Code to Open the Transformed XMLUp to this point, you have opened the XML file by using the user interface in Excel. This section demonstrates how to automate Excel to open the workbook programmatically. The following sample illustrates how to open the transformed XML without user Intervention by first transforming the xml in the
DataSet Object To HTML.
1.Add a Reference to the Microsoft Excel 10.0 Object Library or Microsoft Excel 11.0 Object Library. To do this, Follow There Steps:
a. On the Project menu, click Add Reference.b. On the COM tab, locate Microsoft Excel 10.0 Object Library or Microsoft Excel 11.0 Object Library and click Select.c. Click OK in the Add References dialog box to accept your selection. If YOU Receive a Prompt To Generate Wrappers for the Library That You SELECTED, CLICK YES.2.Add The Following Using Directives To The Top of Form1.cs: Using Excel = Microsoft.Office.Interop.Excel;
3.In the Visual C # .NET project, add another button to Form1.4.Double-click Button2 When the code window for the form appears, add the following code to the Button2_Click handler:. // Connect to the data source.OleDbConnection Objconn = New OLEDBCONNECTION (STRCONN);
Objconn.open ();
// Fill a Dataset with Records from the customer.
OLEDBCOMMAND OBJCMD = New OLEDBCOMMAND (
"Select Customerid, CompanyName, ContactName,"
"Country, Phone from Customers", Objconn;
OLEDBDataAdapter Objadapter = new oledbdataadapter ();
Objadapter.selectCommand = Objcmd;
DataSet objDataSet = new dataset ();
Objadapter.Fill (ObjDataSet);
// Create the fileestream to write with.
System.IO.FileStream Fs = new system.io.filestream
"C: //customers.htm", system.IO.filemode.create;
// Create an XmlTextWriter for the filestream.
System.xml.xmlTextWriter Xtw = new system.xml.xmlTextWriter (
FS, System.Text.Encoding.Unicode;
// Transform the xml using the stylesheet.
XmlDataDocument Xmldoc = New XmlDataDocument (ObjDataSet);
System.xml.xsl.xsltransform xsltran = new system.xml.xsl.xsltransform ();
XSLTRAN.LOAD ("C: //customers.xsl");
Xsltran.Transform (XMLDoc, NULL, XTW);
// Open the HTML File in Excel.
Excel.Application OEXCEL = New Excel.Application ();
OEXCEL.VISIBLE = True;
OEXCEL.USERCONTROL = TRUE;
Excel.Workbooks Obools = OEXCEL.WORKBOOKS;
Object OOPT = system.reflection.Missing.Value; // for optional arguments
5.press f5 to build and rain the program.6.click button2 to open the transformed XML in Excel.
NOTE: While the Excel 2002 and Excel 2003 Object Model does expose anOpenXML method that enables you to programmatically open an XML file with stylesheets applied, the previous sample does not call this method due to a known problem with using this method from an Automation client. THE
OpenXML Method Works As Expected When It Is Called from An Excel Macro; However, When this Method Is Called from An Automation Client, The
STYLESHEET Parameter Is Ignored. For Additional Information, Click The Article Number Below To View The Article In The Microsoft Knowledge Base:
307230 BUG: Stylesheets Parameter of the OpenXML Method Ignored When Automating Excel 2002
Back to the top
References
For More Information, See The Following Knowledge Base Articles:
288215 INFO: Microsoft Excel 2002 and XML
302084 How to Automate Microsoft Excel from Visual C # .NET
301216 How to Populate A Dataset Object from a Database by Using Visual Basic .NET
306023 How To Transfer Data To An Excel Workbook by Using Visual C # .NET