简体   繁体   English

将XML嵌入Excel工作表

[英]Embed XML into Excel worksheet

I do have an XML file which contains several calculated values along with a list of items such as: 我确实有一个XML文件,其中包含几个计算出的值以及一系列项目,例如:

<?xml version="1.0" encoding="utf-8"?>
<XmlContent>
  <Elements>
    <CreationDate>...</CreationDate>
    <Filename>....</Filename>
  </Elements>
  <PersonItems>
    <PersonItem>
      <FirstName>...</FirstName>
      <LastName>...</LastName>
      <Speed>...</Speed>
    </PersonItem>
    <PersonItem>
      <FirstName>...</FirstName>
      <LastName>...</LastName>
      <Speed>...</Speed>
    </PersonItem>
    [...]
  </PersonItems>
</XmlContent>

Now the values should be presented in an Excel sheet using Excel 2007 OpenXmlFormat. 现在,应该使用Excel 2007 OpenXmlFormat将值显示在Excel工作表中。 The calculated values should be mapped to a specific cell within the worksheet along PersonItems should be bound to a table within the same worksheet. 计算值应该映射到工作表内的特定单元格,而PersonItems应该绑定到同一工作表内的表。

Is there a way to embed the XML file into the worksheet package and bind the values to the appropriate fields by using the c# Package API as a CustomXmlPart ? 是否可以通过使用c#Package API作为CustomXmlPart将XML文件嵌入到工作表包中并将值绑定到适当的字段?

I found an example on Channel9 where Matthew Scott made something similar with Word 2007 by using the Word Content Control Toolkit . 我在Channel9上找到了一个示例,其中Matthew Scott通过使用Word Content Control Toolkit与Word 2007进行了类似的操作。 However, this only works with Word. 但是,这仅适用于Word。

Is there something similar for Excel? Excel有类似的东西吗?

Or is there even a better approach for solving this task? 还是有更好的方法来解决此任务?

Well, after research and playing around with XML mapping I came to the conclusion that there is not "easy" way to achieve that what I am longing to do. 好吧,在研究和研究XML映射之后,我得出的结论是,没有一种“简单”的方法可以实现我渴望做的事情。

When importing XML Content within the Excel application, Excel creates an XML mapping definition and stores it within the Package. 在Excel应用程序中导入XML内容时,Excel将创建XML映射定义并将其存储在Package中。 The XML content itself will be splitted by using the mapping definition and merged within the contents of the Excel file. XML内容本身将通过使用映射定义进行拆分,并合并到Excel文件的内容中。 This means, that the XML file itself vanishes right after import and can no longer be used. 这意味着XML文件本身在导入后即消失,无法再使用。

However, the intentional scenario is possbible with Word 2007+ as you can see in the links above. 但是,如您在上面的链接中所见,Word 2007+可能会出现这种情况。

Maybe this information is helpful for anybody who has a similar task like me. 也许这些信息对像我这样有类似任务的人很有帮助。

最简单的方法是解析xml文件(SAX,DOM),获取相应的属性和值,然后将其写入excel文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM