2014-09-24 30 views
0

我正在編寫一個程序(用C#),如果服務器版本較高,將能夠從服務器中替換本地工作簿,然後將其打開。爲此,我試圖讀取本地和服務器副本的自定義屬性「修訂號」。問題在於工作簿包含在公開時啓動的宏,我不想運行任何宏來檢查修訂代碼。那麼有沒有辦法在不打開它的情況下閱讀excel 2007 xlsm文件的修訂版號?如果沒有,有沒有辦法在C#中打開工作簿,而不是執行它的宏?無需打開或運行宏即可閱讀Excel 2007工作簿自定義屬性

+1

您是否檢出ClosedXML(或OpenXML)? Excel文件(2007和>)也是XML。所以通過這些庫打開它們不會執行任何宏 – 2014-09-24 20:56:23

+1

看看:[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

回答

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; 

    } 
相關問題