提示未在本地计算机上注册 Microsoft.ACE.OLEDB.12.0 提供程序

连接access数据库出现如下错误提示:

此种错误有两种原因:

【1】软件编译版本64位与32位的问题

解决方法:

此时可能是因为在VS中编译选择的处理器为“Any Cpu”,可以将其改为“x86”,然后重新编译打包。

 

【2】office版本问题,对于office2007的连接字符串与其它版本的连接字符串是不同的,因为使用的数据库引擎不同。

【3】微软从XP开始在系统中已经集成了JET引擎,不需要安装Office,因此将解决方案平台选择为X86,然后将连接字符串改成"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Datas.mdb;Jet OLEDB:Database Password=scut_robot",无论对于哪个版本的access都可以使用。在使用OLEDB时候也同样改成JET引擎。

  1. C#保存excel文件时提示文件格式与扩展名不匹配

如果保存生成的excel文件提示如下错误,

可以通过设置保存excel格式来解决。

【例】

public static string xlsFilePath = @"D:\";//excel文件存放路径

 

        /// <summary>

        /// 导出数据到excel文件

        /// </summary>

        /// <param name="dt">要导出的数据集</param>

        /// <returns>生成的文件名</returns>

        static public string ExporeToExcel(DataTable dt)

        {

            MSExcel.Application excelApp = null;

            MSExcel.Workbooks wbks = null;

            MSExcel._Workbook wbk = null;

            try

            {

                string strDir = Directory.GetCurrentDirectory();

                excelApp = new MSExcel.Application();

                excelApp.Visible = false;//是打开不可见

                wbks = excelApp.Workbooks;

                wbk = wbks.Add(true);

 

                String version = excelApp.Version;//获取你使用的excel 的版本号

                int FormatNum;//保存excel文件的格式

                if (Convert.ToDouble(version) < 12)//You use Excel 97-2003

                {

                    FormatNum = -4143;

                }

                else//you use excel 2007 or later

                {

                    FormatNum = 56;

                }

               

                object Nothing = Missing.Value;

                MSExcel._Worksheet whs;

                whs = (MSExcel._Worksheet)wbk.Sheets[1];//获取第一张工作表

                whs.Activate();

 

                //写入标题行

                int rowIndex=1;

                for (int col = 0; col < dt.Columns.Count; col++)

                {

                    whs.Cells[rowIndex, col+1] = dt.Columns[col].Caption.ToString();

                }

                rowIndex++;

                //写入数据内容

                foreach (DataRow row in dt.Rows)

                {

                    for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)

                    {

                        whs.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();

                    }

                    rowIndex++;

                }

 

                excelApp.DisplayAlerts = false;

                //保存excel文件

               

                string newFileName = xlsFilePath + "导出的excel文件" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";

                wbk.SaveAs(newFileName, FormatNum);//保存时候设置保存格式

                //关闭文件

                wbk.Close(false, Nothing, Nothing);

                return newFileName;

              

            }

            catch (Exception e)

            {

                throw e;

            }

            finally

            {

                //wbks.Close();//关闭工作簿

                excelApp.Quit();//关闭excel应用程序

                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);//释放excel进程

                excelApp = null;

            }

          

        }

猜你喜欢

转载自blog.csdn.net/huzhizhewudi/article/details/84330421