其實我試過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;
}
您是否檢出ClosedXML(或OpenXML)? Excel文件(2007和>)也是XML。所以通過這些庫打開它們不會執行任何宏 – 2014-09-24 20:56:23
看看:[http://stackoverflow.com/questions/16301530/vba-open-a-workbook-from-vba-and-disable-workbook-open-code ](http://stackoverflow.com/questions/16301530/vba-open-a-workbook-from-vba-and-disable-workbook-open-code) – Fratyx 2014-10-02 11:11:37