Excel中的CustomXmlParts

时间:2013-09-13 09:16:04

标签: c# excel openxml

我在Excel中编写了一个加载项。我在功能区中单击按钮时添加了CustomXmlPart。添加CustomXmlPart后,我将其GUID保存在单独的工作表(XML)中,以便稍后重新启动xml。

以下是代码:

    private void btnAddXML_Click(object sender, RibbonControlEventArgs e)
    {
        Excel.Workbook WB = Globals.ThisAddIn.Application.ActiveWorkbook;
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"C:\Users\shree\Desktop\Correct.xml");
        Microsoft.Office.Core.CustomXMLPart TaggingXml = WB.CustomXMLParts.Add(xmlDoc.OuterXml);
        string strXmlGUID = TaggingXml.Id;
        Excel.Worksheet WS = WB.Sheets.Add(After: WB.Sheets[WB.Sheets.Count]);
        WS.Name = "XML";
        WS.Cells[1, 1] = strXmlGUID;
    }

    private void btnShowXML_Click(object sender, RibbonControlEventArgs e)
    {
        Excel.Workbook WB = Globals.ThisAddIn.Application.ActiveWorkbook;
        Excel.Worksheet WS = WB.Sheets["XML"];
        XmlDocument xmlDoc = new XmlDocument();
        Microsoft.Office.Core.CustomXMLPart TaggingXml = WB.CustomXMLParts.SelectByID(WS.Cells[1, 1].Value.ToString());
        StreamWriter sw = File.CreateText(@"C:\Users\shree\Desktop\New.xml");
        sw.Write(TaggingXml.XML);
        sw.Close();
        sw.Dispose();
        MessageBox.Show("Done");
    }

现在我的问题是如何在excel中查看此xml文档(如果可能)。如果我将此文件移动到另一个系统,我是否可以访问此xml,或者它本地存储在我的系统中。如果我用来读取xml的GUID与其他机器中某些其他对象的GUID冲突怎么办? CustomXmlPart如何工作?它存放在哪里?

2 个答案:

答案 0 :(得分:0)

  1. 您无法在Excel中查看此信息。您可以使用VSTO创建一个简单的CustomXMLParts查看器应用程序。
  2. CustomXMLPart存储在Excel文件中。这意味着当您复制Excel工作簿或在其他位置打开它时,自定义XML将驻留在该工作簿中。

答案 1 :(得分:0)

对于阅读的任何人,您都可以。右键单击excel文件,然后使用Winrar或zip等压缩软件打开

致谢