C#判斷不同版本的Excel(轉)


1、判斷是否安裝的是Excel2000:

                 RegistryKey regk = Registry.LocalMachine;

 

RegistryKey akey =regk .OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\9.0\\Word\\InstallRoot\\"); //檢查本機是否安裝Office2000if(

 

akey !=null) { string file00 =akey .GetValue("Path").ToString(); if (File.Exists(file00 +"Excel.exe")) { ifused =true; } }

 2、判斷是否安裝的是Excel2003:

RegistryKey regk = Registry.LocalMachine;

RegistryKey bkey =regk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\11.0\\Word\\InstallRoot\\");

  //檢查本機是否安裝Office2003

if (bkey !=null) { string file00 =bkey.GetValue("Path").ToString();

if (File.Exists(file03 +"Excel.exe"))

                        {ifused=true;} } 

3、判斷是否安裝的是Excel2007: 

 RegistryKey regk = Registry.LocalMachine;

RegistryKey ckey = rk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\12.0\\Word\\InstallRoot\\");

   

 

if (akeytwo !=null) { string file07 =ckey.GetValue("Path").ToString(); if (File.Exists(file07 +"Excel.exe")) { ifused =true; } }

 4、2003或者2007下的動態拼接連接字符串:

   DataSet ds =new DataSet();

string strConn=""; if(ExistsRegedit03()) { strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strpath + ";" + "Extended Properties=Excel 8.0;"; }else if(ExistsRegedit07()) { strConn = "Provider=Microsoft.Ace.OLEDB.12.0;" + "Data Source=" + strpath + ";" + "Extended Properties=Excel 12.0;"; } OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [RelationerTable$]", strConn); myCommand.Fill(ds); DataTable dt = ds.Tables[0];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM