导出excel 时判断本地excel的版本
public bool ExistsRegedit()
{
bool ifused = false;
RegistryKey rk = Registry.LocalMachine;
RegistryKey akey = rk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\11.0\\Word\\InstallRoot\\");
RegistryKey akeytwo = rk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\12.0\\Word\\InstallRoot\\");
//检查本机是否安装Office2003
if (akey != null)
{
string file03 = akey.GetValue("Path").ToString();
if (File.Exists(file03 + "Excel.exe"))
{
ifused = true;
}
}
//检查本机是否安装Office2007
if (akeytwo != null)
{
string file07 = akeytwo.GetValue("Path").ToString();
if (File.Exists(file07 + "Excel.exe"))
{
ifused = true;
}
}
return ifused;
}
RegistryKey bkey = rk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\9.0\\Word\\InstallRoot\\");
//检查本机是否安装Office2003
if (bkey != null)
{
string file00 = bkey.GetValue("Path").ToString();
if (File.Exists(file00 + "Excel.exe"))
{
ifused = true;
}
}
//动态拼接连接字符串
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];
//dt 就是读取的数据集合
浙公网安备 33010602011771号