简体   繁体   English

如何以编程方式将XML Map添加到Excel 2010电子表格中?

[英]How do I add an XML Map programmatically to an Excel 2010 Spreadsheet?

I have a basic XML file, which I need to add as an XML Map to an Excel 2010 Worksheet. 我有一个基本的XML文件,我需要将其作为XML Map添加到Excel 2010工作表中。

How do I do this programmatically? 我该如何以编程方式执行此操作? I would prefer a solution which uses Microsoft OpenXML SDK. 我更喜欢使用Microsoft OpenXML SDK的解决方案。

You will need to add a CustomXmlMappingsPart to your WorkbookPart and populate it with the schema for your XML. 您需要将CustomXmlMappingsPart添加到WorkbookPart并使用XML的架构填充它。 Then you will need to add a ConnectionsPart that is linked to your xml file. 然后,您需要添加链接到xml文件的ConnectionsPart

The below xml lives on my desktop in the file note.xml: 以下xml位于我的桌面上的note.xml文件中:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

This is the code to generate the xmlmappings 这是生成xmlmappings的代码

CustomXmlMappingsPart part = workbookPart.AddNewPart<CustomXmlMappingsPart>("rId8");

CustomXmlMappingsPart part = new 
MapInfo mapInfo1 = new MapInfo(){ SelectionNamespaces = "" };

Schema schema1 = new Schema(){ Id = "Schema1" };

OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><xsd:element nillable=\"true\" name=\"note\"><xsd:complexType><xsd:sequence minOccurs=\"0\"><xsd:element minOccurs=\"0\" nillable=\"true\" type=\"xsd:string\" name=\"to\" form=\"unqualified\" /><xsd:element minOccurs=\"0\" nillable=\"true\" type=\"xsd:string\" name=\"from\" form=\"unqualified\" /><xsd:element minOccurs=\"0\" nillable=\"true\" type=\"xsd:string\" name=\"heading\" form=\"unqualified\" /><xsd:element minOccurs=\"0\" nillable=\"true\" type=\"xsd:string\" name=\"body\" form=\"unqualified\" /></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>");

schema1.Append(openXmlUnknownElement1);

Map map1 = new Map(){ ID = (UInt32Value)1U, Name = "note_Map", RootElement = "note", SchemaId = "Schema1", ShowImportExportErrors = false, AutoFit = true, AppendData = false, PreserveAutoFilterState = true, PreserveFormat = true };
DataBinding dataBinding1 = new DataBinding(){ FileBinding = true, ConnectionId = (UInt32Value)1U, DataBindingLoadMode = (UInt32Value)1U };

map1.Append(dataBinding1);

mapInfo1.Append(schema1);
mapInfo1.Append(map1);

part.MapInfo = mapInfo1;

This code generates the connection and adds it to the WorkbookPart : 此代码生成连接并将其添加到WorkbookPart

ConnectionsPart part = wookbookPart.AddNewPart<ConnectionsPart>("rId5");

Connections connections1 = new Connections();

Connection connection1 = new Connection(){ Id = (UInt32Value)1U, Name = "note", Type = (UInt32Value)4U, RefreshedVersion = 0, Background = true };
WebQueryProperties webQueryProperties1 = new WebQueryProperties(){ XmlSource = true, SourceData = true, Url = "C:\\Users\\Thomas\\Desktop\\note.xml", HtmlTables = true, HtmlFormat = HtmlFormattingValues.All };

connection1.Append(webQueryProperties1);

connections1.Append(connection1);

part.Connections = connections1;

I generated this code with the Open XML SDK 2.0 Productivity Tool. 我使用Open XML SDK 2.0 Productivity Tool生成了此代码。 I compared a plain xlsx file with one that I added my simple XML map to. 我将一个简单的xlsx文件与我添加了简单XML映射的文件进行了比较。 Hope this helps. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式将XML插入Excel工作表中? - How do I programmatically insert XML into an Excel worksheet? 如何在电子表格中使用Open XML电子表格“取消折叠”单元格? - How do I have Open XML spreadsheet “uncollapse” cells in a spreadsheet? 使用Open XML读取Excel电子表格时,如何确定表格所在的工作表? - Using Open XML to read an Excel spreadsheet, how do I determine the sheet that a Table is on? 以编程方式将工作表添加到Excel时,如何设置工作表的顺序? - How do I set the order of Worksheets as I add them to Excel Programmatically? 借助Sharepoint 2010 Excel Services,如何从具有版本控制的电子表格中获取值 - With Sharepoint 2010 Excel Services how can I get values from a spreadsheet with version control 如何从Excel电子表格中读取单个列? - How do I read in a single column from an Excel spreadsheet? 如何将C#对象列表导出到Excel电子表格? - How do I export a C# object list to an excel spreadsheet? 如何按名称获取和比较Excel电子表格中单元格的内容? - How do I get and compare the contents of a cell in an Excel spreadsheet by name? 如何使用Windows应用程序驱动程序测试Excel电子表格 - How do I test an excel spreadsheet with WIndows Application driver 使用OpenXML如何在Excel电子表格中获取现有样式的StyleIndex - With OpenXML how do I get the StyleIndex of an existing style in an excel spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM