【问题标题】:Excel-DNA save custom data into worksheet (not cells)Excel-DNA 将自定义数据保存到工作表中(不是单元格)
【发布时间】:2015-05-18 00:52:43
【问题描述】:

我希望有一种方法可以将 Excel-DNA 插件中的自定义数据保存到工作簿中。

用户可以通过自定义 Excel-DNA 表单输入的内容。

我意识到这可以保存到单元格中,但我不希望用户看到或更改数据..

是否有将自定义 XML 文件附加到工作簿或添加隐藏工作表的方法?

詹姆斯

【问题讨论】:

    标签: c# excel excel-dna


    【解决方案1】:

    您可以使用 Workbook.CustomXMLParts 执行此操作:请参阅
    Custom XML Parts Overview

    【讨论】:

      【解决方案2】:

      对于那些感兴趣的人,可以通过 Excel-DNA 功能区按钮执行以下操作:

      Microsoft.Office.Interop.Excel.Application excelApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;
      Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.ActiveWorkbook;
      
      System.Collections.IEnumerator enumerator = workbook.CustomXMLParts.SelectByNamespace("http://schemas.microsoft.com/vsto/samplestest").GetEnumerator();
      enumerator.Reset();
      
      
      if (!(enumerator.MoveNext())) {
        string xmlString1 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
          "<employees xmlns=\"http://schemas.microsoft.com/vsto/samplestest\">" +
          "<employee>" +
          "<name>Surender GGG</name>" +
          "<hireDate>1999-04-01</hireDate>" +
          "<title>Manager</title>" +
          "</employee>" +
          "</employees>";
      
      
      
        Office.CustomXMLPart employeeXMLPart = workbook.CustomXMLParts.Add(xmlString1);
      
      } else {
      
        Office.CustomXMLPart a = (Office.CustomXMLPart)enumerator.Current;
      
        a.NamespaceManager.AddNamespace("x", "http://schemas.microsoft.com/vsto/samplestest");
                 MessageBox.Show(a.SelectSingleNode("/x:employees/x:employee/x:name").Text);
        MessageBox.Show(a.XML);
      }
      

      第一次添加xml文档,第二次检索它。您可以保存工作簿并重新打开以确认它仍然存在。

      詹姆斯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-29
        • 2011-05-12
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        相关资源
        最近更新 更多