开发一个winform程序,在开发环境没问题,实际给客户使用的时候发生错误如下
---------------------------出错---------------------------
Could not load file or assembly 'Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
---------------------------确定 ---------------------------
经过查询确实是客户机安装excel版本与当前版本不一致导致。
思路:通过判断版本excel版本,然后调用不同的版本exce.dll进行导入,但是程序不支持多个excel的引用,最终使用了
D:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel
支持 2003,2007,2010 多版本excel
程序如下
/// <summary>
/// 导入
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool Import(string filePath)
{
try
{
//string ss = GetOfficePath();
//Excel就好比一个数据源一般使用
//2003(Microsoft.Jet.Oledb.4.0)
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties = 'Excel 8.0;HDR=NO;IMEX=1'";
//2007(Microsoft.ACE.OLEDB.12.0)
//string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", filePath);
OleDbConnection con = new OleDbConnection(strConn);
con.Open();
string[] names = GetExcelSheetNames(filePath);
if (names.Length > 0)
{
List<CarData> cardatalist = new List<CarData>();
OrgCar.BLL.CarData CarDataBLL = new OrgCar.BLL.CarData();
#region
foreach (string name in names)
{
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = string.Format(" select * from [{0}$]", name);//[sheetName$]要如此格式
OleDbDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
CarData model = new CarData();
if (odr[0].ToString() == "车牌号码"
|| (string.IsNullOrEmpty(odr[0].ToString()) && string.IsNullOrEmpty(odr[1].ToString()) && string.IsNullOrEmpty(odr[2].ToString()))
)//过滤列头 按你的实际Excel文件,或者前三行都为空则不导入
continue;
//
//业务处理,数据库保存
CarDataBLL.Add(model);
}
odr.Close();
cmd.Clone();
}
#endregion
}
con.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "出错");
return false;
}
}