You need to import data from the excel file into the datagridview, but it cannot find the file, although the path is correct. It gives an error: the object was not found by the microsoft office access database engine. Where is the mistake? Thanks in advance itself Excel http://imgur.com/W0ENhDF
using System.Data; using System.Threading.Tasks; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using System.IO; using System.Reflection; using System.Data.OleDb; namespace WindowsFormsApplication5 { public partial class Form1 : Form { DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(); public Form1() { InitializeComponent(); } private void Connection_Click(object sender, EventArgs e) { string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); MessageBox.Show(path.ToString()); path = Path.Combine(path, "textfortest.xlsx"); string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+@"; Extended Properties=""Excel 12.0 Macro;HDR=Yes;ImportMixedTypes=Text;TypeGuessRowsIMEX=1;TypeGuessRows=0"""; OleDbConnection conn = new OleDbConnection(ConnectionString); string Strcmd = "select * from [Лист1$A1:D9]"; OleDbCommand cmd = new OleDbCommand(Strcmd, conn); try { conn.Open(); ds.Clear(); adapter.SelectCommand = cmd; adapter.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { conn.Close(); } } } }