C
C#13mo ago
Salakhosh

How can i import excel file to dataTable using C#(WPF application) and oledb.

Hello so i have task where i have to browse an excel file and import its data to a data table but when im using oledb and reach query part it give me that Sheet1 doesnt exist and honestly idk what should i write. This is what i reached so far private void Browse_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Excel Files|xlsx,xls"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { PathBox.Text = ofd.FileName; string path = ofd.FileName; string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={path};Extended Properties='Excel 12.0 Xml;HDR=YES;'"; using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); DataTable dataTable = new DataTable(); // Assuming the data is in the first worksheet string query = "SELECT * FROM [Sheet1$]"; using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, connection)) { adapter.Fill(dataTable); } connection.Close(); } }
0 Replies
No replies yetBe the first to reply to this messageJoin