我做了一个excel数据导出工具,想发布一个能同时兼容32位和64位的exe,一开始用的anyCpu解决,但是在64位系统上安装了32位的office的话就会提示“未在本地计算机上注册Microsoft.Ace.OleDB.12.0提供程序”,应该是它在64位机器上就只会调用64版本的驱动,而实际只安装了32位的。  不知道有没有办法解决这个问题?

解决方案 »

  1.   

    嗯,这问题我也遇到了
    好像是说微软在64位机上没有继续去发展了,所以不能改用OleDB去读取
    只能说是用Excel.dll这个组件去读取,虽然慢,但是兼容64和32
    不说废话,发代码------ExcelPath  是Excel文件地址,下面那个杀死Excel进程的方法我注释了
    object missing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
                if (excel != null)
                {
                    excel.Visible = false; 
                    excel.UserControl = true;
                    // 以只读的形式打开EXCEL文件
                    Microsoft.Office.Interop.Excel.Workbook wb = excel.Application.Workbooks.Open(ExcelPath, missing, true, missing, missing, missing,
                     missing, missing, missing, true, missing, missing, missing, missing, missing);
                    //取得第一个工作薄
                    Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
                    DataTable dt = new DataTable("mytable");
                    dt.Columns.Add("商品名称(型号)", typeof(String));
                    dt.Columns.Add("品牌(厂商)", typeof(String));
                    dt.Columns.Add("批号", typeof(String));
                    dt.Columns.Add("封装", typeof(String));
                    dt.Columns.Add("供货量", typeof(String));
                    dt.Columns.Add("起订量", typeof(String));
                    dt.Columns.Add("批发价", typeof(String));
                    DataSet myDs = new DataSet();
                    myDs.Tables.Add(dt);
                    DataRow myRow;
                    myDs.Clear();                for (int i = 2; i <= 1600; i++) //第一行为标题,不读取
                    {
                        myRow = myDs.Tables["mytable"].NewRow();
                        for (int j = 1; j <= 7; j++)
                        {
                            Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)sheet.Cells[i, j];
                            if (r == null)
                            {
                                continue;
                            }
                            else
                            {
                                string strValue = r.Text.ToString();
                                myRow[j - 1] = strValue;
                            }
                        }
                        myDs.Tables["mytable"].Rows.Add(myRow);
                    }
                    excel.Quit();
                    /*杀掉进程
                    Process[] procs = Process.GetProcessesByName("EXCEL");
                    foreach (Process pro in procs)
                    {
                        pro.Kill();
                    }
                    GC.Collect();*/
                    return myDs.Tables[0];
                }
                return null;
      

  2.   

    Microsoft.Ace.OleDB.12.0
    这个是excel2007的LZ可以用这个试试
    Microsoft.Jet.OLEDB.4.0
      

  3.   

    是的,必须自己创建表,然后循环Excel,往里面填充数据。。
      

  4.   

    条件编译的问题而已。
                        string DB;
                        if (File.Exists(ConfigurationManager.ConnectionStrings["CMSDB"].ConnectionString))
                        {
                            DB = ConfigurationManager.ConnectionStrings["CMSDB"].ConnectionString;
                        }
                        else
                        {
                            DB = HttpServer.MapPath(ConfigurationManager.ConnectionStrings["CMSDB"].ConnectionString);
                        }
    #if X86
                        strConnection = string.Format("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = \"{0}\"", DB);
    #else
                        strConnection = string.Format("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = \"{0}\"", DB);
    #endif