无法创建文件 - excel 到数据集

Failed to create file - excel to dataset

我正在使用 .xlsx 文件并想将详细信息导入数据集。 但它在代码 MyCommand.Fill(DtSet);

上给出错误 "failure creating file"

请查看下面的完整代码

 System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.DataSet DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1 + ";Extended Properties=Excel 12.0;");
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1]", MyConnection);
            MyCommand.TableMappings.Add("Table", "TestTable");
            DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet);

textBox1 很可能是您的 TextBox 控件。要使用它的值,您应该使用 .Text 属性

所以,而不是 Data Source=" + textBox1 + ";Extended Properties

使用 Data Source=" + textBox1.Text + ";Extended Properties