Excel中的CustomXmlParts

问题描述 投票:0回答:2

我已经在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如何工作?它存储在哪里?

c# excel openxml
2个回答
0
投票
  1. 您不能在Excel本身中查看此信息。您可以使用VSTO创建一个简单的CustomXMLParts查看器应用程序。
  2. CustomXMLPart存储在Excel文件本身中。这意味着,当您复制Excel工作簿或在其他地方打开它时,自定义XML驻留在该工作簿中。

0
投票

对于任何阅读者,您都可以。右键单击excel文件并使用压缩软件(例如winrar或zip)打开

问候

© www.soinside.com 2019 - 2024. All rights reserved.