private DataSet importExcelToDataSet(string FilePath)
        {
            string strConn;
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
            DataSet myDataSet = new DataSet();
            try
            {
                myCommand.Fill(myDataSet);
            }
            catch (Exception ex)
            {
                //throw new InvalidFormatException("该Excel文件的工作表的名字不正确," + ex.Message);
                MessageBox.Show(ex.Message,"Error");
            }
            return myDataSet;
        }
这是2003的,读excel2010时的时候显示"外部表不是预期的格式"
怎么改成2010的啊???
还有InvalidFormatException 要用哪个using啊,没有提示...

解决方案 »

  1.   

    我用以下方法创建2003和2007的连接字符串,还没有装2010,不知道是不是应该和2008一样private string GetConnectString()
            {
                try
                {
                    if (!File.Exists(txtSourceFile.Text))
                        return string.Empty;                string strConn;
                    string excelFilename = txtSourceFile.Text;
                    if (excelFilename.EndsWith("xls", StringComparison.InvariantCultureIgnoreCase))
                    {
                        strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                        @"Data Source=" + excelFilename + ";" + "Extended Properties=\"Excel 8.0;HDR=No\"";
                    }
                    else if (excelFilename.EndsWith("xlsx", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //strConn = @"Data Source=" + _strExcelFilename +
                        //    ";Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
                        strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                            @"Data Source=" + excelFilename + ";" +
                            "Extended Properties=\"Excel 12.0 Xml;HDR=No\"";
                    }
                    else
                        return string.Empty;
                    return strConn;
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.ToString());
                    return string.Empty;
                }
            }
      

  2.   

    office2007装完以后创建目录:C:\Program Files\Microsoft Office\Office12
    所有连接字符串里面有12.0
    2010安装完以后目录里面是多少? 应该可以替换吧
      

  3.   

    读取不同版本的Excel必须得用不同的驱动吗,不是可以向下兼容吗?
      

  4.   

    建立一个.udl文件,打开看一下就知道了,Microsoft Office 12.0 Access Database Engine OLE DB Provider是单独列开的===================================================刚才试了下,Microsoft.ACE.OLEDB.12.0连低版本似乎可以
    但是我不知道你想说的是向下兼容还是向上兼容……
      

  5.   

    strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                            @"Data Source=" + excelFilename + ";" +
                            "Extended Properties=\"Excel 12.0 Xml;HDR=No\"";
    我把这个改成14.0,下载了AccessDatabaseEngine.exe,安装了,但为什么还提示,未找到Microsoft.ACE.oleDB.14.0提供程序啊...
      

  6.   


    楼主 用12就行了吗?需要添加哪些dll