3
我有一個基本的XML文件,我需要將其作爲XML映射添加到Excel 2010工作表中。如何以編程方式將XML映射添加到Excel 2010電子表格中?
如何以編程方式執行此操作?我更喜歡使用Microsoft OpenXML SDK的解決方案。
我有一個基本的XML文件,我需要將其作爲XML映射添加到Excel 2010工作表中。如何以編程方式將XML映射添加到Excel 2010電子表格中?
如何以編程方式執行此操作?我更喜歡使用Microsoft OpenXML SDK的解決方案。
您需要將CustomXmlMappingsPart
添加到WorkbookPart
中,並使用XML的模式填充它。然後,您需要添加一個鏈接到您的xml文件的ConnectionsPart
。
如下XML活在我的桌面上的文件note.xml:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
這是產生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;
此代碼生成的連接的代碼,並將其添加到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;
我用Open XM生成了這段代碼L SDK 2.0生產力工具。我將一個普通的xlsx文件與我添加了簡單的XML映射的文件進行了比較。希望這可以幫助。
你能澄清你的意思是'映射'一個XML文件到Excel? – 2011-11-17 16:43:18