excel导入

开发一个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;
            }
        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值