.xlsx文件实际上只是一个压缩存档(zip文件),所以如果你真的想添加一个隐藏文件,那么你只需要将一个.xml文件添加到存档中.那样Excel甚至不知道它在那里.
将.xlsx文件重命名为.zip,解压缩,添加文件,然后选择.zip文件的内容并重新存档.重命名为.xlsx,你将拥有隐藏的.xml文件.(注意:不要压缩顶级文件夹,只压缩内容)
您可以使用像SharpZipLib这样的zip库在C#中执行此操作:http: //www.sharpdevelop.net/OpenSource/SharpZipLib/
更新:如果用户从Excel中保存文件,则不会保留此"隐藏"文件.我可以为该场景提出的最好的想法是将代码作为嵌入在工作表中的VBA宏的一部分来调用.
此链接包含有关操作Office包的各个部分的有用信息:http://msdn.microsoft.com/en-us/library/aa982683.aspx
core.xml和app.xml(在docProps文件夹中)包含文档属性,可能是存储其他xml信息的好位置.
我有同样的问题,这是我使用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);
}
Run Code Online (Sandbox Code Playgroud)
关于xlworkbook上面使用的对象:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Workbook XlWorkbook = (Excel.Workbook)
(Excel.Application)Marshal.GetActiveObject("Excel.Application")).ActiveWorkbook;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6786 次 |
| 最近记录: |