我正在编写一个程序(在C#中),如果服务器版本较高,它将能够从服务器替换本地工作簿,然后打开它。为此,我试图阅读Custom Property" Revision Number"本地和服务器副本。问题是该工作簿包含在打开时启动的宏,并且我不想运行任何宏来检查修订版代码。那么有没有一种方法来读取excel 2007 xlsm文件的修订版号而不实际打开它?如果没有,有没有办法在C#中打开工作簿而不执行它的宏?
答案 0 :(得分:0)
实际上我尝试了tkacprow的建议来使用OpenXML并且它有效。我花了一段时间来制作一个有效的代码,我昨天才开始工作。 Fratyx,你的小费看起来也很有趣 - 我会记住这一点。这是一个有效的代码:
public string GetVersion(string fileName)
{
string propertyValue = string.Empty;
try
{
using (var wb = SpreadsheetDocument.Open(fileName, false))
{
const string corePropertiesSchema = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
const string dcPropertiesSchema = "http://purl.org/dc/elements/1.1/";
const string dcTermsPropertiesSchema = "http://purl.org/dc/terms/";
// Get the core properties part (core.xml).
CoreFilePropertiesPart xCoreFilePropertiesPart;
xCoreFilePropertiesPart = wb.CoreFilePropertiesPart;
// Manage namespaces to perform XML XPath queries.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("cp", corePropertiesSchema);
nsManager.AddNamespace("dc", dcPropertiesSchema);
nsManager.AddNamespace("dcterms", dcTermsPropertiesSchema);
// Get the properties from the package.
XmlDocument xdoc = new XmlDocument(nt);
// Load the XML in the part into an XmlDocument instance.
xdoc.Load(xCoreFilePropertiesPart.GetStream());
string searchString = string.Format("//cp:coreProperties/{0}", "cp:version");
XmlNode xNode = xdoc.SelectSingleNode(searchString, nsManager);
if (!(xNode == null))
{
//Console.WriteLine(" version is " + xNode.InnerText);
propertyValue = xNode.InnerText;
}
}
}
catch (OpenXmlPackageException e)
{
throw new ApplicationException(String.Format("Incorrect Format detected in a file: {0}" , fileName),e.GetBaseException());
}
return propertyValue;
}