找不到可安装的ISAM - 通过SAP B1读取Excel表格

时间:2013-06-03 09:00:44

标签: c# excel sap

我正在尝试通过打开SAP business one中的对话框来阅读Excel工作表(.csv)。我之前没有尝试过,在尝试阅读Excel工作表时收到以下错误:

    private void GetFile()
    {
        using (GetFileNameClass oGetFileName = new GetFileNameClass())
        {
            oGetFileName.Filter = "Excel files (*.csv)|*.csv";
            oGetFileName.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            Thread threadGetExcelFile = new Thread(new ThreadStart(oGetFileName.GetFileName));
            threadGetExcelFile.SetApartmentState(ApartmentState.STA);

            try
            {
                threadGetExcelFile.Start();
                while (!threadGetExcelFile.IsAlive) ; // Wait for thread to get started
                Thread.Sleep(1); // Wait a sec more
                threadGetExcelFile.Join(); // Wait for thread to end

                var fileName = string.Empty;
                fileName = oGetFileName.FileName;

                if (fileName != string.Empty)
                {
                    string connString = "";
                    System.Data.DataTable dt = new System.Data.DataTable();                  

                    connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
                    connString += fileName;
                    connString += ";Extended Properties=Excel 12.0 Xml;HDR=YES;IMEX=1;";

                    OleDbConnection myConnection = new OleDbConnection(connString);

                    if (myConnection.State != ConnectionState.Open)
                        myConnection.Open();

                    string sql = "SELECT * From [Sheet1$]";   <-------Error thrown here

                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, myConnection))
                    {
                        adapter.Fill(dt);
                    }
                }
            }
            catch (RulesException ex)
            {
                SboConnection.SboApplication.SetStatusBarMessage(ex.GetErrorMessages(), SAPbouiCOM.BoMessageTime.bmt_Medium, true);
            }
        }
    }

在sql语句中出现错误“找不到可安装的ISAM”。如何解决这个问题并阅读excel表?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

连接字符串中的实际情况,最后需要两个; (分号)。所以* connString * 必须如下:

var connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;';", saved_FileName);

它对我有用。