0

我正在尝试通过打开 SAP 业务之一的对话框来读取 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 表?任何帮助表示赞赏。

4

1 回答 1

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);

它对我有用。

于 2014-01-07T06:30:11.450 回答