Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XML Data Mapping in Excel 2003 : Saving the workbook to xml data file


Author: Mahesh ChandraMouli MVP

View products that this article applies to.

Resolution

Pre-requisite:
Make sure there is no other Excel instance in your system before you run the sample code. For more information about how to make Office application quite after automation, please refer to the following KB:317109 - Office application does not quit after automation from Visual Studio .NET client
Excel 2003 should be installed on the machine where this application is running.

Before performing save operation, the cells should be mapped to the proper XML node first. Here are the steps,
1. Open the excel file
2. Create an XML map object and add the schema to the XXML map object
3. Check if there has already been ListObject in the workbook, if not add a listobject to the workbook. When a mapping is done between cells and xml node, there����
��� should be a ListObject.
4. Map each cell to the appropriate node using the range object.
5. Check whether this excel can be exportable using the IsExportable property of the map object.
6. Save the excel file as �SaveAsXMLData� and it creates a XML data file.


// Map special Range to the schema.
Excel.Worksheet sheet = (Excel.Worksheet)oExcel.ActiveSheet;
sheet.ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange, sheet.get_Range(sheet.Cells[1,1], sheet.Cells[2,7]), oOpt, Excel.XlYesNoGuess.xlYes, oOpt);
sheet.ListObjects[1].ListColumns[1].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/Column1", oOpt, true);
sheet.ListObjects[1].ListColumns[2].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/ Column2", oOpt, true);
sheet.ListObjects[1].ListColumns[3].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/ Column3", oOpt, true);
sheet.ListObjects[1].ListColumns[4].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/ Column4", oOpt, true);
sheet.ListObjects[1].ListColumns[5].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/ Column5", oOpt, true);
sheet.ListObjects[1].ListColumns[6].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/ Column6", oOpt, true);
sheet.ListObjects[1].ListColumns[7].XPath.SetValue(oExcel.ActiveWorkbook.XmlMaps[1], "/Root/Row/ Column7", oOpt, true);



↑ Back to the top


More information

================
Using the Excel 2003 Object Model to Add XML Data Integration
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_xl2003_ta/html/odc_xl_Excel2003XMLIntro.asp

XPath Object
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl11/html/xlobjXPath_HV01030950.asp

ListObject Object
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl11/html/xlobjListObject_HV01031546.asp

↑ Back to the top


Properties

COMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

↑ Back to the top


Community solutions content disclaimer

Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.

↑ Back to the top


Keywords: KB555320, kbhowto, kbpubtypecca, kbpubmvp

↑ Back to the top

Article Info
Article ID : 555320
Revision : 1
Created on : 5/27/2005
Published on : 5/27/2005
Exists online : False
Views : 256