在 Excel 文件中存储 XML 数据的最佳方式是什么

发布于 2024-10-07 07:00:20 字数 188 浏览 8 评论 0原文

我正在寻找一种在 Excel 文件中存储 XML 数据的方法。 数据应该对用户完全隐藏,它不应该在单元格或注释中,甚至不应该隐藏。 此外,当用户使用 Excel 打开并保存文件时,应保留数据。 我并不是在寻找一种将单元格映射到外部 XML 数据的方法。 XML 数据应位于 xlsx 文件内。

将使用 C# 工具而不是 Excel 本身输入数据。

I am looking for a way to store XML data in an Excel file.
The data should be completely hidden to the user, it should not be in a cell or comment, even hidden.
Also, the data should be preserved when the user opens and then saves the file with Excel.
I am not looking for a way to map cells to external XML data. The XML data should be inside the xlsx file.

The data will be entered using a C# tool, not Excel itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘年祭陌 2024-10-14 07:00:20

.xlsx 文件实际上只是一个压缩存档(zip 文件),因此如果您确实想添加隐藏文件,那么您只需将 .xml 文件添加到存档中即可。这样 Excel 甚至不会知道它的存在。

将 .xlsx 文件重命名为 .zip,解压它,添加您的文件,然后选择 .zip 文件的内容并重新存档。重命名为 .xlsx,您将在其中包含隐藏的 .xml 文件。
(注意:不要压缩顶级文件夹,只压缩内容)

您可以在 C# 中使用 SharpZipLib 等 zip 库来执行此操作:
http://www.sharpdevelop.net/OpenSource/SharpZipLib/

更新:< /em> 如果用户从 Excel 中保存文件,则不会保留此“隐藏”文件。对于这种情况,我能想到的最好的想法是调用代码作为嵌入到工作表中的 VBA 宏的一部分。

此链接包含有关操作 Office 包的各个部分的有用信息:http://msdn .microsoft.com/en-us/library/aa982683.aspx

core.xml 和 app.xml(位于 docProps 文件夹中)包含文档属性,可能是存储其他 xml 信息的好位置。

The .xlsx file is actually just a compression archive (zip file) so if you truly want to add a hidden file then you could just add a .xml file to the archive. That way Excel wouldn't even know it was there.

Rename a .xlsx file to .zip, extract it, add your file, then select the contents of the .zip file and re-archive them. Rename to .xlsx and you'll have your hidden .xml file inside there.
(NOTE: Do not zip the top-level folder, only the contents)

You can do this in C# using a zip library like SharpZipLib:
http://www.sharpdevelop.net/OpenSource/SharpZipLib/

UPDATE: This "hidden" file will not be preserved if the user saves the file from within Excel. The best idea I can come up with for that scenario is to invoke the code as part of a VBA macro embedded in the sheet.

This link contains useful information about manipulating the parts of an Office package: http://msdn.microsoft.com/en-us/library/aa982683.aspx

The core.xml and app.xml (in the docProps folder) contain the document properties and might be a good location to store additional xml information.

绝不放开 2024-10-14 07:00:20

我遇到了同样的问题,这是我的代码来处理它,使用 Microsoft 的自定义 XML零件。 (您可以在我的代码注释中找到所有必要的解释)。

//Deletes all the previously added parts and adds a new part 
//containing the string argument which has to be in XML format. 


public void addCustomXMLPart(string test)
{
    IEnumerator e = Xlworkbook.CustomXMLParts.GetEnumerator();
    e.Reset();
    CustomXMLPart p;
    //The !p.BuiltIn is because before our customXMLPart there are some
    // Excel BuiltIns of them and if we try to delete them we will get an exception.
    while (e.MoveNext())
    {
        p = (CustomXMLPart)e.Current;
        if (p != null && !p.BuiltIn) 
            p.Delete();
    }
    Xlworkbook.CustomXMLParts.Add(test, Type.Missing);
}

关于上面使用的xlworkbook对象:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Workbook XlWorkbook = (Excel.Workbook)
  (Excel.Application)Marshal.GetActiveObject("Excel.Application")).ActiveWorkbook;

I had the same issue and here is my code to deal with it, using Microsoft's Custom XML Parts. (You may find all the necessary explanations in my comments in the code).

//Deletes all the previously added parts and adds a new part 
//containing the string argument which has to be in XML format. 


public void addCustomXMLPart(string test)
{
    IEnumerator e = Xlworkbook.CustomXMLParts.GetEnumerator();
    e.Reset();
    CustomXMLPart p;
    //The !p.BuiltIn is because before our customXMLPart there are some
    // Excel BuiltIns of them and if we try to delete them we will get an exception.
    while (e.MoveNext())
    {
        p = (CustomXMLPart)e.Current;
        if (p != null && !p.BuiltIn) 
            p.Delete();
    }
    Xlworkbook.CustomXMLParts.Add(test, Type.Missing);
}

About the xlworkbook object used above:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Workbook XlWorkbook = (Excel.Workbook)
  (Excel.Application)Marshal.GetActiveObject("Excel.Application")).ActiveWorkbook;
这样的小城市 2024-10-14 07:00:20

Excel 是一个用于存储、操作和查看数据的应用程序,因此,它并非旨在存储任意不可读的数据。但是,正如您所提到的,您可以在单独的工作表上使用隐藏字段来放置一些信息。

Excel is an application for storing, manipulating, and viewing data, and thusly, it is not designed to store arbitrary unreadable data. However, as you alluded to, you could use hidden fields on a separate sheet to put some information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文